Commit 0a08bd8f8cba457c0b0c782f45b6cdc488818802

Stephen Moloney 2016-02-14T14:12:06

improve docs for the Request.request/3 and Auth.prepare_request/3 functions

diff --git a/lib/hubic/auth.ex b/lib/hubic/auth.ex
index cfa030d..8053e00 100644
--- a/lib/hubic/auth.ex
+++ b/lib/hubic/auth.ex
@@ -1,35 +1,68 @@
 defmodule ExOvh.Hubic.Auth do
   @moduledoc ~S"""
-    Delegates the prepare_request to the appropriate module and function
-    depending on the opts specified.
-    `%{ openstack: :true }` ==> delegates the query to the OpenstackApi.Auth Module
-    `%{ }` ==> delegates the query to the HubicApi.Auth Module
+  Houses the `prepare_request` function which delegates the function call to the appropriate
+  module & function depending on the `opts` key-values.
+
+  Ovh uses it's own custom api and also separate Openstack compliant apis so
+  and these apis are quite different.
+  Therefore, the request needs to be routed to the correct `prepare_request` function so
+  that the correct auth credentials are put into the `options_t` in the returned
+  `ExOvh.Client.query_t` query tuple.
+
+  ## Examples of what some delegation depending on opts
+
+      ExOvh.hubic_prepare_request(query, %{} = opts)
+      calls
+      ExOvh.Hubic.HubicApi.Auth.hubic_prepare_request(ExOvh, query, opts)
+
+  -
+
+      ExOvh.hubic_prepare_request(query, %{ openstack: :true } = opts)
+      calls
+      ExOvh.Hubic.OpenstackApi.Auth.hubic_prepare_request(ExOvh, query, opts)
+
+
+  ## Subsequent Request modules
+
+  The subsequent request functions process the request by
+
+  1. Calling the appropriate `prepare_request` function which has been delegated to.
+  2. Getting the appropriate auth credentials and adding them to the headers as needed.
+  3. Returning `ExOvh.Client.query_t` which is a tuple of the format {method, uri, options} which
+     can then be easily used to make requests using HTTPpotion, `ovh_request` or `hubic_request`.
   """
   alias ExOvh.Hubic.OpenstackApi.Auth, as: OpenstackAuth
   alias ExOvh.Hubic.HubicApi.Auth, as: HubicAuth
 
-  ############################
-  # Public
-  ############################
 
+  @doc ~S"""
+  Delegates the function call to the appropriate module & function depending on the `opts` key-values.
 
-  @spec prepare_request(query :: ExOvh.Client.raw_query_t, opts :: map())
-                     :: ExOvh.Client.query_t
-  def prepare_request({method, uri, params} = query, opts), do: prepare_request(ExOvh, query, opts)
+  Subsequent request functions return `{:ok, response_t}` or `{:error, response_t}`
 
+  ## Options
+
+      { } = opts
+
+  The function call will be delegated to `ExOvh.Hubic.HubicApi.Auth`.
+
+      { openstack: :true, webstorage: "<service>" } = opts
+
+  The function call will be delegated to `ExOvh.Hubic.HubicApi.Webstorage.Auth`.
+
+  `openstack: :true` - boolean - indicates whether the request is an openstack one or not.
+
+  `webstorage: service` - String.t and is the name the cdn webstorage in your ovh stack which you which to use.
+  """
   @spec prepare_request(client :: atom, query :: ExOvh.Client.raw_query_t, opts :: map())
                      :: ExOvh.Client.query_t
   def prepare_request(client, {method, uri, params} = query, %{openstack: :true} = opts) do
     OpenstackAuth.prepare_request(client, query)
   end
+
   def prepare_request(client, {method, uri, params} = query, opts) do
     HubicAuth.prepare_request(client, query)
   end
 
 
-  ############################
-  # Private
-  ############################
-
-
 end
diff --git a/lib/hubic/hubic_api/request.ex b/lib/hubic/hubic_api/request.ex
index d370bdf..5cfd436 100644
--- a/lib/hubic/hubic_api/request.ex
+++ b/lib/hubic/hubic_api/request.ex
@@ -4,9 +4,9 @@ defmodule ExOvh.Hubic.HubicApi.Request do
   alias ExOvh.Hubic.HubicApi.Cache, as: TokenCache
 
 
-  @spec request(client :: atom, query :: ExOvh.Client.raw_query_t, retries :: integer)
+  @spec request(client :: atom, query :: ExOvh.Client.raw_query_t, opts :: map, retries :: integer)
                 :: {:ok, ExOvh.Client.response_t} | {:error, ExOvh.Client.response_t}
-  def request(client, {method, uri, params} = query, retries \\ 0) do
+  def request(client, {method, uri, params} = query, opts, retries \\ 0) do
     {method, uri, options} = Auth.prepare_request(client, query)
     LoggingUtils.log_return({method, uri, options}, :debug)
     resp = HTTPotion.request(method, uri, options)
@@ -32,7 +32,7 @@ defmodule ExOvh.Hubic.HubicApi.Request do
       if Map.has_key?(resp.body, "error") do
         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
+          unless retries >= 1, do: request(query, opts, 1) # Try request one more time
         else
           {:error, resp}
         end
diff --git a/lib/hubic/openstack_api/request.ex b/lib/hubic/openstack_api/request.ex
index 2b5d590..21af5f4 100644
--- a/lib/hubic/openstack_api/request.ex
+++ b/lib/hubic/openstack_api/request.ex
@@ -4,9 +4,9 @@ defmodule ExOvh.Hubic.OpenstackApi.Request do
   alias ExOvh.Hubic.OpenstackApi.Auth
 
 
-  @spec request(client :: atom, query :: ExOvh.Client.raw_query_t)
+  @spec request(client :: atom, query :: ExOvh.Client.raw_query_t, opts :: map)
                 :: {:ok, ExOvh.Client.response_t} | {:error, ExOvh.Client.response_t}
-  def request(client, {method, uri, params} = query) do
+  def request(client, {method, uri, params} = query, opts) do
     LoggingUtils.log_mod_func_line(__ENV__, :debug)
     {method, uri, options} = Auth.prepare_request(client, query)
     |> LoggingUtils.log_return(:debug)
diff --git a/lib/hubic/request.ex b/lib/hubic/request.ex
index a164262..9e0f115 100644
--- a/lib/hubic/request.ex
+++ b/lib/hubic/request.ex
@@ -1,26 +1,30 @@
 defmodule ExOvh.Hubic.Request do
   @moduledoc ~S"""
-  Contains the `request` function which delegates the request
-  to the correct module and functions depending on the parameters in `opts`.
+  Houses the `request` function which delegates the function call to the appropriate
+  module & function depending on the `opts` key-values.
 
-  Hubic uses it's own custom api and also a distinct Openstack compliant api so
+  Hubic uses it's own custom api and also separate Openstack compliant apis so
   and these apis are quite different.
-  Therefore, the request needs to be routed to the correct `prepare_request` function so
+  Therefore, the request needs to be routed to the correct `request` function so
   that the correct auth credentials are put into the `options_t` in the returned
   `ExOvh.Client.query_t` query tuple.
 
-  This module's request function delegates the query to the correct `prepare_request`
-  function by pattern matching on the `opts` map.
+  ## Examples of what some delegation depending on opts
 
-  ## Routing/Delegating depending on opts
+      ExOvh.hubic_request(query, %{} = opts)
+      calls
+      ExOvh.Hubic.HubicApi.Request.request(ExOvh, query, opts)
 
-  `%{ }` -> `ExOvh.Hubic.HubicApi.Request`
+  -
+
+      ExOvh.hubic_request(query, %{ openstack: :true } = opts)
+      calls
+      ExOvh.Hubic.OpenstackApi.Request.request(ExOvh, query, opts)
 
-  `%{ openstack: :true}` -> `ExOvh.Hubic.OpenstackApi.Request`
 
   ## Subsequent Request modules
 
-  The subsequent request modules process the request by
+  The subsequent request functions process the request by
 
   1. Calling the appropriate `prepare_request` function which has been delegated to.
   2. Making the actual request with `HTTPotion`
@@ -31,7 +35,7 @@ defmodule ExOvh.Hubic.Request do
 
 
   @doc ~S"""
-  Redirects the query to the appropriate function dependeing on the `opts` key-values.
+  Delegates the function call to the appropriate module & function depending on the `opts` key-values.
 
   Subsequent request functions return `{:ok, response_t}` or `{:error, response_t}`
 
@@ -39,11 +43,13 @@ defmodule ExOvh.Hubic.Request do
 
       { } = opts
 
-  The request will be delegated to `ExOvh.Hubic.HubicApi.Request` and processed as a hubic api request.
+  The function call will be delegated to `ExOvh.Hubic.HubicApi.Request` and processed as a hubic api request.
 
       { openstack: :true } = opts
 
-  The request will be delegated to `ExOvh.Hubic.OpenstackApi.Request` and processed as an openstack api request.
+  The function call will be delegated to `ExOvh.Hubic.OpenstackApi.Request`.
+
+  `openstack: :true` - boolean - indicates whether the request is an openstack one or not.
   """
   @spec request(client :: atom, query :: ExOvh.Client.raw_query_t, opts :: map)
                 :: {:ok, ExOvh.Client.response_t} | {:error, ExOvh.Client.response_t}
diff --git a/lib/ovh/auth.ex b/lib/ovh/auth.ex
index 7d0709b..abbb8ac 100644
--- a/lib/ovh/auth.ex
+++ b/lib/ovh/auth.ex
@@ -1,35 +1,68 @@
 defmodule ExOvh.Ovh.Auth do
   @moduledoc ~s"""
-    Delegates the prepare_request to the appropriate module and function
-    depending on the opts specified.
-    `%{ openstack: :true }` ==> delegates the query to the OpenstackApi.Auth Module
-    `%{ }` ==> delegates the query to the HubicApi.Auth Module
+  Houses the `prepare_request` function which delegates the function call to the appropriate
+  module & function depending on the `opts` key-values.
+
+  Ovh uses it's own custom api and also separate Openstack compliant apis so
+  and these apis are quite different.
+  Therefore, the request needs to be routed to the correct `prepare_request` function so
+  that the correct auth credentials are put into the `options_t` in the returned
+  `ExOvh.Client.query_t` query tuple.
+
+  ## Examples of what some delegation depending on opts
+
+      ExOvh.ovh_prepare_request(query, %{} = opts)
+      calls
+      ExOvh.Ovh.OvhApi.Auth.ovh_prepare_request(ExOvh, query, opts)
+
+  -
+
+      ExOvh.ovh_prepare_request(query, %{ openstack: :true, webstorage: "service_name" } = opts)
+      calls
+      ExOvh.Ovh.OpenstackApi.Webstorage.Auth.ovh_prepare_request(ExOvh, query, opts)
+
+
+  ## Subsequent Request modules
+
+  The subsequent request functions process the request by
+
+  1. Calling the appropriate `prepare_request` function which has been delegated to.
+  2. Getting the appropriate auth credentials and adding them to the headers as needed.
+  3. Returning `ExOvh.Client.query_t` which is a tuple of the format {method, uri, options} which
+     can then be easily used to make requests using HTTPpotion, `ovh_request` or `hubic_request`.
   """
   alias ExOvh.Ovh.Openstack.Auth, as: OpenstackAuth
   alias ExOvh.Ovh.OvhApi.Auth, as: OvhAuth
 
-  ############################
-  # Public
-  ############################
 
+  @doc ~S"""
+  Delegates the function call to the appropriate module & function depending on the `opts` key-values.
 
-  @spec prepare_request(query :: ExOvh.Client.raw_query_t, opts :: map())
-                     :: ExOvh.Client.query_t
-  def prepare_request({method, uri, params} = query, opts), do: prepare_request(ExOvh, query, opts)
+  Subsequent request functions return `{:ok, response_t}` or `{:error, response_t}`
 
+  ## Options
+
+      { } = opts
+
+  The function call will be delegated to `ExOvh.Ovh.OvhApi.Auth`.
+
+      { openstack: :true, webstorage: "<service>" } = opts
+
+  The function call will be delegated to `ExOvh.Ovh.OpenstackApi.Webstorage.Auth`.
+
+  `openstack: :true` - boolean - indicates whether the request is an openstack one or not.
+
+  `webstorage: service` - String.t and is the name the cdn webstorage in your ovh stack which you which to use.
+  """
   @spec prepare_request(client :: atom, query :: ExOvh.Client.raw_query_t, opts :: map())
                      :: ExOvh.Client.query_t
   def prepare_request(client, {method, uri, params} = query, %{openstack: :true} = opts) do
     OpenstackAuth.prepare_request(client, query)
   end
+
   def prepare_request(client, {method, uri, params} = query, opts) do
     OvhAuth.prepare_request(client, query)
   end
 
 
-  ############################
-  # Private
-  ############################
-
-
 end
diff --git a/lib/ovh/openstack_api/webstorage/request.ex b/lib/ovh/openstack_api/webstorage/request.ex
index 57233f9..01d9a6f 100644
--- a/lib/ovh/openstack_api/webstorage/request.ex
+++ b/lib/ovh/openstack_api/webstorage/request.ex
@@ -5,7 +5,7 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Request do
 
   @spec request(client :: atom, query :: ExOvh.Client.query_t, service :: String.t)
                :: {:ok, ExOvh.Client.response_t} | {:error, ExOvh.Client.response_t}
-  def request(client, {method, uri, params} = query, service) do
+  def request(client, {method, uri, params} = query, %{ webstorage: service } = opts) do
     LoggingUtils.log_mod_func_line(__ENV__, :debug)
 
     {method, uri, options} = Auth.prepare_request(client, query, service)
diff --git a/lib/ovh/ovh_api/request.ex b/lib/ovh/ovh_api/request.ex
index e3addfa..09b3b57 100644
--- a/lib/ovh/ovh_api/request.ex
+++ b/lib/ovh/ovh_api/request.ex
@@ -9,9 +9,9 @@ defmodule ExOvh.Ovh.OvhApi.Request do
   ############################
 
 
-  @spec request(client :: atom, query :: ExOvh.Client.query_t)
+  @spec request(client :: atom, query :: ExOvh.Client.query_t, opts :: map)
                :: {:ok, ExOvh.Client.response_t} | {:error, ExOvh.Client.response_t}
-  def request(client, {method, uri, params} = query) do
+  def request(client, {method, uri, params} = query, opts) do
     LoggingUtils.log_mod_func_line(__ENV__, :debug)
     config = config(client)
 
diff --git a/lib/ovh/request.ex b/lib/ovh/request.ex
index acf9ce0..8a541b8 100644
--- a/lib/ovh/request.ex
+++ b/lib/ovh/request.ex
@@ -1,26 +1,30 @@
 defmodule ExOvh.Ovh.Request do
   @moduledoc ~S"""
-  Contains the `request` function which delegates the request
-  to the correct module and functions depending on the parameters in `opts`.
+  Houses the `request` function which delegates the function call to the appropriate
+  module & function depending on the `opts` key-values.
 
   Ovh uses it's own custom api and also separate Openstack compliant apis so
   and these apis are quite different.
-  Therefore, the request needs to be routed to the correct `prepare_request` function so
+  Therefore, the request needs to be routed to the correct `request` function so
   that the correct auth credentials are put into the `options_t` in the returned
   `ExOvh.Client.query_t` query tuple.
 
-  This module's request function delegates the query to the correct `prepare_request`
-  function by pattern matching on the `opts` map.
+  ## Examples of what some delegation depending on opts
 
-  ## Routing/Delegating depending on opts
+      ExOvh.ovh_request(query, %{} = opts)
+      calls
+      ExOvh.Ovh.OvhApi.Request.request(ExOvh, query, opts)
 
-  `%{ }` -> `ExOvh.Ovh.OvhApi.Request`
+  -
+
+      ExOvh.ovh_request(query, %{ openstack: :true, webstorage: "service_name" } = opts)
+      calls
+      ExOvh.Ovh.OpenstackApi.Webstorage.Request.request(ExOvh, query, opts)
 
-  `%{ openstack: :true, webstorage: "service_name" }` -> `ExOvh.Ovh.OpenstackApi.Webstorage.Request`
 
   ## Subsequent Request modules
 
-  The subsequent request modules process the request by
+  The subsequent request functions process the request by
 
   1. Calling the appropriate `prepare_request` function which has been delegated to.
   2. Making the actual request with `HTTPotion`
@@ -31,7 +35,7 @@ defmodule ExOvh.Ovh.Request do
 
 
   @doc ~S"""
-  Redirects the query to the appropriate function dependeing on the `opts` key-values.
+  Delegates the function call to the appropriate module & function depending on the `opts` key-values.
 
   Subsequent request functions return `{:ok, response_t}` or `{:error, response_t}`
 
@@ -39,18 +43,20 @@ defmodule ExOvh.Ovh.Request do
 
       { } = opts
 
-  The request will be delegated to `ExOvh.Ovh.OvhApi.Request` and processed as a hubic api request.
+  The function call will be delegated to `ExOvh.Ovh.OvhApi.Request` and processed as a hubic api request.
 
       { openstack: :true, webstorage: service } = opts
 
-  The request will be delegated to `ExOvh.Ovh.OpenstackApi.Webstorage.Request`
-  and processed as an openstack api request with the service value. The `service` value is a String.t
-  and is the name the cdn webstorage in your ovh stack which you which to use.
+  The function call will be delegated to `ExOvh.Ovh.OpenstackApi.Webstorage.Request`.
+
+  `openstack: :true` - boolean - indicates whether the request is an openstack one or not.
+
+  `webstorage: service` - String.t and is the name the cdn webstorage in your ovh stack which you which to use.
   """
   @spec request(client :: atom, query :: ExOvh.Client.raw_query_t, opts :: map)
                 :: {:ok, ExOvh.Client.response_t} | {:error, ExOvh.Client.response_t}
   def request(client, {method, uri, params} = query, %{ openstack: :true, webstorage: service } = opts) do
-    Webstorage.request(client, query, service)
+    Webstorage.request(client, query, opts)
   end
 
   def request(client, {method, uri, params} = query, opts) do