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.TokenCache
alias ExOvh.Hubic.Defaults
###################
# Public
###################
@doc "Api for requests to the hubic custom api"
@spec request(method :: atom, uri :: String.t, params :: map, retries :: integer) :: map
def request(method, uri, params \\ :nil), do: request(ExOvh, method, uri, params, 0)
def request(client, method, uri, params, retries \\ 0) do
{method, uri, options} = Auth.prep_request(client, method, uri, params)
resp = HTTPotion.request(method, uri, options)
resp =
%{
body: resp.body |> Poison.decode!(),
headers: resp.headers |> Enum.into(%{}),
status_code: resp.status_code
}
|> LoggingUtils.log_return(:debug)
body = resp |> Map.get(:body)
if Map.has_key?(body, "error") do
error = Map.get(body, "error") <> " :: " <> Map.get(body, "error_description")
if body["error"] === "invalid_token" do
# Restart the gen_server to recuperate state
GenServer.call(TokenCache, :stop)
# Try request one more time
unless retries >= 1 do
request(method, uri, body, 1)
end
else
error
end
else
body
end
end
###################
# Private
###################
end