Commit 80e957e8b1a88da9e9f55a4fd5f6151d13c5e11f

Stephen Moloney 2017-03-03T16:14:40

add prepare_request function

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 067ac29..9ba721c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@
 [bug fix]
 - Fix setting the query string bug (typo) - `url.encode_query(qs_map)` -> `URI.encode_query(qs_map)`
 
+[enhancements]
+- Add `prepare_request/2` function - prepares the request without sending it. Applies standard transformations.
+
 ## v0.3.1
 
 [changes]
diff --git a/lib/ex_ovh/client.ex b/lib/ex_ovh/client.ex
index ea2beeb..4153226 100644
--- a/lib/ex_ovh/client.ex
+++ b/lib/ex_ovh/client.ex
@@ -97,6 +97,13 @@ defmodule ExOvh.Client do
       @spec hackney_opts() :: Keyword.t
       def hackney_opts(), do: config() |> Keyword.fetch!(:hackney)
 
+      @doc "Prepares a request prior to sending by applying standard transformations to the request"
+      @spec prepare_request(HTTPipe.Conn.t | HTTPipe.Request.t) :: HTTPipe.Conn.t
+      def prepare_request(conn) do
+        client = unquote(opts) |> Keyword.fetch!(:client)
+        Request.apply_transformations(conn, client)
+      end
+
       @doc "Sends a request to the ovh api using [httpipe](https://hex.pm/packages/httpipe)."
       @spec request(HTTPipe.Conn.t) :: {:ok, HTTPipe.Conn.t} | {:error, HTTPipe.Conn.t}
       def request(conn) do
@@ -128,6 +135,7 @@ defmodule ExOvh.Client do
   @callback config() :: Keyword.t
   @callback ovh_config() :: Keyword.t
   @callback hackney_opts() :: Keyword.t
+  @callback prepare_request(HTTPipe.Conn.t | HTTPipe.Request.t) :: HTTPipe.Conn.t
   @callback request(conn :: HTTPipe.Conn.t) :: {:ok, HTTPipe.Conn.t} | {:error, HTTPipe.Conn.t}
   @callback request!(conn :: HTTPipe.Conn.t) :: HTTPipe.Conn.t | no_return
 
diff --git a/lib/ex_ovh/request.ex b/lib/ex_ovh/request.ex
index dabb4f9..31d389b 100644
--- a/lib/ex_ovh/request.ex
+++ b/lib/ex_ovh/request.ex
@@ -25,21 +25,8 @@ defmodule ExOvh.Request do
     end
   end
 
-
-  # private
-
-
-  defp parse_body(resp) do
-    try do
-       resp.body |> Poison.decode!()
-    rescue
-      _ ->
-        resp.body
-    end
-  end
-
-
-  defp apply_transformations(conn, client) do
+  @doc :false
+  def apply_transformations(conn, client) do
     conn =
     unless (:url in Map.get(conn, :completed_transformations, [])) do
      ExOvh.Transformation.Url.apply(conn, client)
@@ -65,4 +52,16 @@ defmodule ExOvh.Request do
     end
   end
 
+
+  # private
+
+  defp parse_body(resp) do
+    try do
+       resp.body |> Poison.decode!()
+    rescue
+      _ ->
+        resp.body
+    end
+  end
+
 end