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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
defmodule ExOvh.Client do
alias ExOvh.Defaults
@type method_t :: atom()
@type path_t :: String.t
@type params_t :: map() | :nil
@type options_t :: map() | :nil
@type raw_query_t :: { method_t, path_t, params_t }
@type query_t :: { method_t, path_t, options_t }
@type response_t :: %{ body: map() | String.t, headers: map(), status_code: integer() }
defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
@otp_app opts[:otp_app] || :ex_ovh
if(@otp_app !== :ex_ovh) do
def config(), do: Application.get_env(@otp_app, __MODULE__) |> Enum.into(%{})
else
def config(), do: Application.get_all_env(@otp_app) |> Enum.into(%{})
end
def start_link(opts \\ []) do
ExOvh.Supervisor.start_link(__MODULE__, config(), opts)
end
def ovh_request({method, uri, params} = query, opts \\ %{}) do
ExOvh.Ovh.Request.request(__MODULE__, query, opts)
end
def ovh_prepare_request({method, uri, params} = query, opts \\ %{}) do
ExOvh.Ovh.Auth.prepare_request(__MODULE__, query, opts)
end
def hubic_request({method, uri, params} = query, opts \\ %{}) do
ExOvh.Hubic.Request.request(__MODULE__, query, opts)
end
def hubic_prepare_request({method, uri, params} = query, opts \\ %{}) do
ExOvh.Hubic.Auth.prepare_request(__MODULE__, query, opts)
end
end
end
@doc """
Starts the ovh and the hubic supervisors.
"""
@callback start_link() :: :ok | {:error, {:already_started, pid}} | {:error, term}
@doc ~S"""
Gets the ovh and hubic config from the application environment.
Returns a map if the config is present in the config.exs file(s)
or
Returns :nil if the config is absent.
"""
@callback config() :: :nil | map
@doc """
Prepares all elements necessary for making a request to the ovh api.
Returns a tuple `{method, uri, options}` which is the `query_t` tuple.
With the returned query_t, a request can easily be made with
the `ovh_request` function or [HTTPotion](http://hexdocs.pm/httpotion/HTTPotion.html).
## Example
Making a request to the custom ovh api:
query = ExOvh.ovh_prepare_request({:get, "/cdn/webstorage", :nil}, %{})
Making a request to the openstack compliant ovh cdn webstorage service:
query = ExOvh.ovh_prepare_request({:get, "<account_name>", %{"format" => "json"}}, %{ openstack: :true, webstorage: "<ovh_service_name>" })
"""
@callback ovh_prepare_request(query :: raw_query_t)
:: query_t
@doc ~S"""
Makes a request to the ovh api.
Returns a `response_t` map with the structure:
`%{ body: <body>, headers: [<headers>], status_code: <code>}`
## Example
Making a request to the custom ovh api:
ExOvh.ovh_request({:get, "/cdn/webstorage", :nil}, %{})
Making a request to the openstack compliant ovh cdn webstorage service:
ExOvh.ovh_request({:get, "<account_name>", %{"format" => "json"}}, %{ openstack: :true, webstorage: "<ovh_service_name>" })
"""
@callback ovh_request(query :: raw_query_t, opts :: map)
:: {:ok, response_t} | {:error, response_t}
@doc ~S"""
Makes a request to the hubic api.
Returns a map `%{ body: <body>, headers: %{<headers>}, status_code: <code>}`
"""
@callback hubic_request(query :: raw_query_t, opts :: map)
:: {:ok, response_t} | {:error, response_t}
@doc ~S"""
Prepares all elements necessary prior to making a request to the hubic api.
Returns a tuple `{method, uri, options}`
"""
@callback hubic_prepare_request(query :: raw_query_t)
:: query_t
end