Packages

UPYun sdk for Elixir.

Current section

Files

Jump to
upyun lib util.ex
Raw

lib/util.ex

defmodule UPYun.Util do
def request(method, url, headers, body \\ nil) when body == nil or is_map(body) do
default_headers = %{
'accept' => 'application/vnd.hex.beta+elixir',
'accept-encoding' => 'gzip',
'user-agent' => user_agent()}
headers = Dict.merge(default_headers, headers)
http_opts = [ssl: ssl_opts(url), relaxed: true]
opts = [body_format: :binary]
url = String.to_char_list(url)
cond do
body ->
request = {url, Map.to_list(headers), 'application/vnd.hex+elixir', body}
method in [:put, :post] ->
request = {url, Map.to_list(headers), 'application/vnd.hex+elixir', '%{}'}
true ->
request = {url, Map.to_list(headers)}
end
case :httpc.request(method, request, http_opts, opts, :hex) do
{:ok, response} ->
handle_response(response)
{:error, reason} ->
{:http_error, reason}
end
end
end