Hash :
dc63c2ec
Author :
Date :
2016-04-24T14:34:18
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
defimpl Openstex.Request, for: ExOvh.Ovh.Query do
@moduledoc :false
alias Openstex.{Auth, Response}
alias ExOvh.Ovh.Query
# Public
@spec request(Query.t, Keyword.t, atom) :: {:ok, Response.t} | {:error, Response.t}
def request(query, opts, client) do
Og.context(__ENV__, :debug)
q = Auth.prepare_request(query, opts, client) |> Map.from_struct()
options = set_opts(q.options, opts)
case HTTPoison.request(q.method, q.uri, q.body, q.headers, options) do
{:ok, resp} ->
body = parse_body(resp)
resp = %Response{ body: body, headers: resp.headers |> Enum.into(%{}), status_code: resp.status_code }
if resp.status_code >= 100 and resp.status_code < 300 do
{:ok, resp}
else
{:error, resp}
end
{:error, resp} ->
{:error, %HTTPoison.Error{reason: resp.reason}}
end
end
# private
def parse_body(resp) do
try do
resp.body |> Poison.decode!()
rescue
_ ->
resp.body
end
end
defp set_opts(query_opts, opts), do: Keyword.merge(query_opts, opts)
end