1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Auth do
@moduledoc :false
alias ExOvh.Ovh.OpenstackApi.Webstorage.Cache, as: WebStorageCache
@methods [:get, :post, :put, :delete]
@timeout 10_000
############################
# Public
############################
@spec prepare_request(client :: atom, query :: ExOvh.Client.raw_query_t, service :: String.t)
:: ExOvh.Client.query_t
def prepare_request(client, query)
def prepare_request(client, {method, uri, params} = query, service) when method in [:get, :head, :delete] do
uri = WebStorageCache.get_swift_endpoint(client, service) <> uri
if params !== :nil and params !== "", do: uri = uri <> "?" <> URI.encode_query(params)
options = %{ headers: headers(client, service), timeout: @timeout }
{method, uri, options}
|> Og.log_return(:debug)
end
def prepare_request(client, {method, uri, params} = query, service) when method in [:post, :put] do
uri = WebStorageCache.get_swift_endpoint(client, service) <> uri
if params !== "" and params !== :nil and is_map(params), do: params = Poison.encode!(params)
options = %{ body: params, headers: headers(client, service), timeout: @timeout }
{method, uri, options}
|> Og.log_return(:debug)
end
############################
# Private
############################
defp headers(client, service) do
%{
"Content-Type": "application/json; charset=utf-8",
"X-Auth-Token": WebStorageCache.get_credentials_token(client, service)
}
end
end