add prepare_request function
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
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