Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AliyunApiGatewaySdk

AliyunApiGatewaySdk is based on Req
It can simply call an API from Alibaba API Gateway by using an AppCode

Getting started

Option 1: Using Application Config

Add the following to config/config.exs:

config :aliyun_api_gateway_sdk,
        HOST: "<HOST>",
        API_APP_KEY: "<YOUR API_APP_KEY>",
        API_APP_SECRET: "<YOUR API_APP_SECRET>",
        API_ENV: "<YOUR API_ENV>"

Option 2: Using Direct Config (No Application Config Needed)

You can pass configuration directly to each request:

config = %{
  "HOST" => "<HOST>",
  "API_APP_KEY" => "<YOUR API_APP_KEY>",
  "API_APP_SECRET" => "<YOUR API_APP_SECRET>",
  "API_ENV" => "<YOUR API_ENV>"
}
AliyunApiGatewaySdk.get("/api/doc", config)

Perform a GET request

# get/1
AliyunApiGatewaySdk.get("/api/doc")

# get/2 - with query parameters
query = %{"id" => "1"}
AliyunApiGatewaySdk.get("/api/doc", query)

# get/2 - with custom config
config = %{
  "HOST" => "https://api.example.com",
  "API_APP_KEY" => "your_key",
  "API_APP_SECRET" => "your_secret",
  "API_ENV" => "RELEASE"
}
AliyunApiGatewaySdk.get("/api/doc", config)

# get/3 - with query parameters and headers
query = %{"id" => "1"}
headers = %{"Authorization" => "JWT <YOUR TOKEN>"}
AliyunApiGatewaySdk.get("/api/doc", query, headers)

# get/3 - with query parameters and custom config
query = %{"id" => "1"}
config = %{
  "HOST" => "https://api.example.com",
  "API_APP_KEY" => "your_key",
  "API_APP_SECRET" => "your_secret",
  "API_ENV" => "RELEASE"
}
AliyunApiGatewaySdk.get("/api/doc", query, config)

# get/4 - with query parameters, headers, and custom config
query = %{"id" => "1"}
headers = %{"Authorization" => "JWT <YOUR TOKEN>"}
config = %{
  "HOST" => "https://api.example.com",
  "API_APP_KEY" => "your_key",
  "API_APP_SECRET" => "your_secret",
  "API_ENV" => "RELEASE"
}
AliyunApiGatewaySdk.get("/api/doc", query, headers, config)

Perform a POST request

# post/1
AliyunApiGatewaySdk.post("/api/doc")

# post/2 - form data (default)
body = %{"username" => "root", "password" => "root"}
AliyunApiGatewaySdk.post("/api/login", body)

# post/3 - JSON data
body = %{"title" => "My Programming Motto", "body" => "Programming is thinking, not typing"}
AliyunApiGatewaySdk.post("/api/doc/articles", body, :json)

# post/3 - with custom headers
body = %{"title" => "My Programming Motto"}
headers = %{"Authorization" => "JWT <YOUR TOKEN>"}
AliyunApiGatewaySdk.post("/api/doc/articles", body, headers)

# post/4 - with headers and JSON
body = %{"title" => "My Programming Motto"}
headers = %{"Authorization" => "JWT <YOUR TOKEN>"}
AliyunApiGatewaySdk.post("/api/doc/articles", body, headers, :json)

Perform a PUT request

# put/1
AliyunApiGatewaySdk.put("/api/resource/1")

# put/2 - form data (default)
body = %{"name" => "updated"}
AliyunApiGatewaySdk.put("/api/resource/1", body)

# put/3 - JSON data
body = %{"name" => "updated"}
AliyunApiGatewaySdk.put("/api/resource/1", body, :json)

# put/3 - with custom headers
body = %{"name" => "updated"}
headers = %{"Authorization" => "JWT <YOUR TOKEN>"}
AliyunApiGatewaySdk.put("/api/resource/1", body, headers)

# put/4 - with headers and JSON
body = %{"name" => "updated"}
headers = %{"Authorization" => "JWT <YOUR TOKEN>"}
AliyunApiGatewaySdk.put("/api/resource/1", body, headers, :json)

Perform a PATCH request

# patch/1
AliyunApiGatewaySdk.patch("/api/resource/1")

# patch/2 - form data (default)
body = %{"name" => "partial update"}
AliyunApiGatewaySdk.patch("/api/resource/1", body)

# patch/3 - JSON data
body = %{"name" => "partial update"}
AliyunApiGatewaySdk.patch("/api/resource/1", body, :json)

# patch/3 - with custom headers
body = %{"name" => "partial update"}
headers = %{"Authorization" => "JWT <YOUR TOKEN>"}
AliyunApiGatewaySdk.patch("/api/resource/1", body, headers)

# patch/4 - with headers and JSON
body = %{"name" => "partial update"}
headers = %{"Authorization" => "JWT <YOUR TOKEN>"}
AliyunApiGatewaySdk.patch("/api/resource/1", body, headers, :json)

Perform a DELETE request

# delete/1
AliyunApiGatewaySdk.delete("/api/resource/1")

# delete/2 - with query parameters
query = %{"id" => "1"}
AliyunApiGatewaySdk.delete("/api/resource", query)

# delete/3 - with query parameters and headers
query = %{"id" => "1"}
headers = %{"Authorization" => "JWT <YOUR TOKEN>"}
AliyunApiGatewaySdk.delete("/api/resource", query, headers)

Installation

If available in Hex, the package can be installed by adding aliyun_api_gateway_sdk to your list of dependencies in mix.exs:

def deps do
  [
    {:aliyun_api_gateway_sdk, "~> 0.1.0"}
  ]
end

Raw API Request (aliyun_api_request)

If you need more control, you can use the raw aliyun_api_request function directly:

# With application config
AliyunApiGatewaySdk.aliyun_api_request("GET", "/api/resource", %{}, %{}, Core.get_config())

# With custom config
config = %{
  "HOST" => "https://api.example.com",
  "API_APP_KEY" => "your_key",
  "API_APP_SECRET" => "your_secret",
  "API_ENV" => "RELEASE"
}
AliyunApiGatewaySdk.aliyun_api_request("POST", "/api/resource", %{"Content-Type" => "application/json"}, %{key: "value"}, config)

Documentation

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/aliyun_api_gateway_sdk.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages