Commit 5ded6ff81c09235b9824c9098c21b8258c2a53da

Stephen Moloney 2016-02-11T12:20:30

bug fix. get_token, get_credentials. client needed.

diff --git a/lib/hubic/hubic_api/auth.ex b/lib/hubic/hubic_api/auth.ex
index 1d90c0e..fa0b7cb 100644
--- a/lib/hubic/hubic_api/auth.ex
+++ b/lib/hubic/hubic_api/auth.ex
@@ -24,7 +24,7 @@ defmodule ExOvh.Hubic.HubicApi.Auth do
     uri = uri(config, uri)
     if params !== :nil and params !== "" and is_map(params), do: uri = uri <> "?" <> URI.encode_query(params)
     if params !== :nil and params !== "" and is_map(params) === :false, do: uri = uri <> URI.encode_www_form(params)
-    options = %{ headers: headers(method), timeout: @timeout }
+    options = %{ headers: headers(client, method), timeout: @timeout }
     {method, uri, options}
   end
 
@@ -32,7 +32,7 @@ defmodule ExOvh.Hubic.HubicApi.Auth do
     config = config(client)
     uri = uri(config, uri)
     if params !== "" and params !== :nil and is_map(params), do: params = Poison.encode!(params)
-    options = %{ body: params, headers: headers(method), timeout: @timeout }
+    options = %{ body: params, headers: headers(client, method), timeout: @timeout }
     {method, uri, options}
   end
 
@@ -78,12 +78,11 @@ defmodule ExOvh.Hubic.HubicApi.Auth do
   # Private
   ###################
 
-  defp default_headers(), do: %{ "Authorization": "Bearer " <> Cache.get_token() }
-  defp headers(method) when method in [:post, :put] do
-    Map.merge(default_headers(), %{ "Content-Type": "application/json;charset=utf-8" })
+  defp default_headers(client), do: %{ "Authorization": "Bearer " <> Cache.get_token(client) }
+  defp headers(client, method) when method in [:post, :put] do
+    Map.merge(default_headers(client), %{ "Content-Type": "application/json;charset=utf-8" })
   end
-  defp headers(method) when method in [:get, :delete], do: default_headers()
-
+  defp headers(client, method) when method in [:get, :delete], do: default_headers(client)
 
   defp config(), do: Cache.get_config(ExOvh)
   defp config(client), do: Cache.get_config(client)
diff --git a/lib/hubic/hubic_api/cache.ex b/lib/hubic/hubic_api/cache.ex
index 6295c25..ce507c4 100644
--- a/lib/hubic/hubic_api/cache.ex
+++ b/lib/hubic/hubic_api/cache.ex
@@ -136,7 +136,7 @@ defmodule ExOvh.Hubic.HubicApi.Cache do
           raise "Problem retrieving the access token from ets table"
         else
           :timer.sleep(@get_token_sleep_interval)
-          get_token(index + 1)
+          get_token(client, index + 1)
         end
       else
         tokens["access_token"]
@@ -146,7 +146,7 @@ defmodule ExOvh.Hubic.HubicApi.Cache do
         raise "Problem retrieving the access token from ets table"
       else
         :timer.sleep(@get_token_sleep_interval)
-        get_token(index + 1)
+        get_token(client, index + 1)
       end
     end
   end
diff --git a/lib/hubic/openstack_api/cache.ex b/lib/hubic/openstack_api/cache.ex
index a4265ab..e2863f1 100644
--- a/lib/hubic/openstack_api/cache.ex
+++ b/lib/hubic/openstack_api/cache.ex
@@ -124,7 +124,7 @@ defmodule ExOvh.Hubic.OpenstackApi.Cache do
           raise "Problem retrieving the openstack credentials from ets table"
         else
           :timer.sleep(@get_credentials_sleep_interval)
-          get_credentials(index + 1)
+          get_credentials(client, index + 1)
         end
       else
         credentials
@@ -134,7 +134,7 @@ defmodule ExOvh.Hubic.OpenstackApi.Cache do
         raise "Problem retrieving the openstack credentials from ets table"
       else
         :timer.sleep(@get_credentials_sleep_interval)
-        get_credentials(index + 1)
+        get_credentials(client, index + 1)
       end
     end
   end