Current section

Files

Jump to
aliyun_api lib aliyun_api.ex
Raw

lib/aliyun_api.ex

defmodule AliyunAPI do
use Application
require Logger
def http_request(url, method, opts \\ []) do
headers = opts[:headers] || []
options =
[
timeout: AliyunAPI.Env.get_http_timeout(),
recv_timeout: AliyunAPI.Env.get_http_recv_timeout()
]
|> Keyword.merge(Keyword.get(opts, :options, []))
Logger.info("#{method}: #{url} with headers: #{headers |> inspect()}")
HTTPoison.request(method, url, opts[:body] || "", headers, options)
end
def start(_type, _args) do
import Supervisor.Spec, warn: false
Confex.resolve_env!(:aliyun_api)
opts = [strategy: :one_for_one, name: AliyunAPI.Supervisor]
Supervisor.start_link([], opts)
end
def if_call(q, condition \\ true, true_fn)
def if_call(q, condition, true_fn) do
if condition, do: true_fn.(q), else: q
end
# https://help.aliyun.com/document_detail/44434.html
def url_encode(text) do
"#{text}"
|> URI.encode_www_form()
|> String.replace("*", "%2A")
|> String.replace("+", "%20")
|> String.replace("%7E", "~")
end
def get_header(headers, key) do
key = key |> to_string() |> String.downcase()
headers
|> Enum.filter(fn {k, _v} ->
key == k |> to_string() |> String.downcase()
end)
|> Enum.map(fn {_k, v} ->
v
end)
end
def to_array(any, opts \\ [])
def to_array(nil, _opts), do: []
def to_array(array, opts) when is_list(array) do
if opts[:uniq] do
array |> Enum.uniq()
else
array
end
end
def to_array(object, opts), do: [object] |> to_array(opts)
def filter_params(arguments) do
arguments
|> Enum.filter(fn {_key, value} ->
"#{value}" |> String.length() > 0
end)
|> Map.new()
end
end