Hash :
0633245f
Author :
Date :
2016-02-07T18:41:33
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 49 50 51 52 53 54
defmodule ExOvh.Hubic.Request do
alias Poison
alias HTTPotion
alias ExOvh.Hubic.Auth
alias ExOvh.Hubic.Cache, as: TokenCache
alias ExOvh.Hubic.Openstack.Cache
alias ExOvh.Hubic.Defaults
###################
# Public
###################
@doc "Api for requests to the hubic custom api"
@spec request(query :: ExOvh.Client.raw_query_t)
:: {:ok, ExOvh.Client.response_t} | {:error, ExOvh.Client.response_t}
def request({method, uri, params} = query), do: request(ExOvh, query, 0)
@spec request(client :: atom, query :: ExOvh.Client.raw_query_t, retries :: integer)
:: {:ok, ExOvh.Client.response_t} | {:error, ExOvh.Client.response_t}
def request(client, {method, uri, params} = query, retries \\ 0) do
{method, uri, options} = Auth.prep_request(client, query)
resp = HTTPotion.request(method, uri, options)
resp =
%{
body: resp.body |> Poison.decode!(),
headers: resp.headers,
status_code: resp.status_code
}
if resp.status_code >= 100 and resp.status_code < 300 do
{:ok, resp}
else
if Map.has_key?(resp.body, "error") do
#error = Map.get(body, "error") <> " :: " <> Map.get(body, "error_description")
if resp.body["error"] === "invalid_token" do
GenServer.call(TokenCache, :stop) # Restart the gen_server to recuperate state
unless retries >= 1, do: request(query, 1) # Try request one more time
else
{:error, resp}
end
else
{:error, resp}
end
end
end
###################
# Private
###################
end