AliyunApiGatewaySdk is based on Req
It can simply call an API from Alibaba API Gateway by using an AppCode
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>"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)# 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)# 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)# 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)# 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)# 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)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"}
]
endIf 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 can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/aliyun_api_gateway_sdk.