From 143b61394f9d17c3e49092cfed13b08ac7826681 Mon Sep 17 00:00:00 2001 From: Brian Alexander Date: Fri, 10 May 2024 15:16:16 -0600 Subject: [PATCH] use `Req` as http client --- lib/sanity.ex | 42 ++++++++++++++++++++---------------------- mix.exs | 1 + mix.lock | 2 ++ 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/sanity.ex b/lib/sanity.ex index 3c54846..00e614d 100644 --- a/lib/sanity.ex +++ b/lib/sanity.ex @@ -36,6 +36,7 @@ defmodule Sanity do doc: "Sanity dataset.", required: true ], + # FIXME finch_mod: [ type: :atom, default: Finch, @@ -44,7 +45,7 @@ defmodule Sanity do http_options: [ type: :keyword_list, default: [receive_timeout: 30_000], - doc: "Options to be passed to `Finch.request/3`." + doc: "Options to be passed to `Req.request/2`." ], max_attempts: [ type: :pos_integer, @@ -57,6 +58,11 @@ defmodule Sanity do doc: "Sanity project ID.", required: true ], + req_mod: [ + type: :atom, + default: Req, + doc: false + ], retry_delay: [ type: :pos_integer, default: 1_000, @@ -251,26 +257,25 @@ defmodule Sanity do ) do opts = NimbleOptions.validate!(opts, @request_options_schema) - finch_mod = Keyword.fetch!(opts, :finch_mod) - http_options = Keyword.fetch!(opts, :http_options) - - url = "#{url_for(request, opts)}?#{URI.encode_query(query_params)}" - result = - Finch.build(method, url, headers(opts) ++ headers, body) - |> finch_mod.request(Sanity.Finch, http_options) + Keyword.merge(Keyword.fetch!(opts, :http_options), + body: body, + headers: headers(opts) ++ headers, + method: method, + url: "#{url_for(request, opts)}?#{URI.encode_query(query_params)}" + ) + |> Keyword.fetch!(opts, :req_mod).request() case {opts[:max_attempts], result} do - {_, {:ok, %Finch.Response{body: body, headers: headers, status: status}}} + {_, {:ok, %Req.Response{body: body, headers: headers, status: status}}} when status in 200..299 -> - {:ok, %Response{body: Jason.decode!(body), headers: headers, status: status}} + {:ok, %Response{body: body, headers: headers, status: status}} - {_, {:ok, %Finch.Response{body: body, headers: headers, status: status} = resp}} + {_, {:ok, %Req.Response{body: body, headers: headers, status: status} = resp}} when status in 400..499 -> - if json_resp?(headers) do - {:error, %Response{body: Jason.decode!(body), headers: headers, status: status}} - else - raise %Sanity.Error{source: resp} + case body do + %{} -> {:error, %Response{body: body, headers: headers, status: status}} + _ -> raise %Sanity.Error{source: resp} end {max_attempts, {_, error_or_response}} when max_attempts > 1 -> @@ -292,13 +297,6 @@ defmodule Sanity do end end - defp json_resp?(headers) do - Enum.any?(headers, fn - {"content-type", value} -> String.contains?(value, "application/json") - {_name, _value} -> false - end) - end - @doc """ Like `request/2`, but raises a `Sanity.Error` instead of returning and error tuple. diff --git a/mix.exs b/mix.exs index 0305476..0f992cb 100644 --- a/mix.exs +++ b/mix.exs @@ -39,6 +39,7 @@ defmodule Sanity.MixProject do {:finch, "~> 0.5"}, {:jason, "~> 1.2"}, {:nimble_options, "~> 0.5 or ~> 1.0"}, + {:req, "~> 0.4"}, # dev/test {:dialyxir, "~> 1.0", only: [:dev], runtime: false}, diff --git a/mix.lock b/mix.lock index 200c8eb..7f8ba03 100644 --- a/mix.lock +++ b/mix.lock @@ -14,7 +14,9 @@ "mint": {:hex, :mint, "1.6.0", "88a4f91cd690508a04ff1c3e28952f322528934be541844d54e0ceb765f01d5e", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "3c5ae85d90a5aca0a49c0d8b67360bbe407f3b54f1030a111047ff988e8fefaa"}, "mox": {:hex, :mox, "1.1.0", "0f5e399649ce9ab7602f72e718305c0f9cdc351190f72844599545e4996af73c", [:mix], [], "hexpm", "d44474c50be02d5b72131070281a5d3895c0e7a95c780e90bc0cfe712f633a13"}, "nimble_options": {:hex, :nimble_options, "1.1.0", "3b31a57ede9cb1502071fade751ab0c7b8dbe75a9a4c2b5bbb0943a690b63172", [:mix], [], "hexpm", "8bbbb3941af3ca9acc7835f5655ea062111c9c27bcac53e004460dfd19008a99"}, + "nimble_ownership": {:hex, :nimble_ownership, "0.3.1", "99d5244672fafdfac89bfad3d3ab8f0d367603ce1dc4855f86a1c75008bce56f", [:mix], [], "hexpm", "4bf510adedff0449a1d6e200e43e57a814794c8b5b6439071274d248d272a549"}, "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"}, "nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"}, + "req": {:hex, :req, "0.4.14", "103de133a076a31044e5458e0f850d5681eef23dfabf3ea34af63212e3b902e2", [:mix], [{:aws_signature, "~> 0.3.2", [hex: :aws_signature, repo: "hexpm", optional: true]}, {:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:nimble_ownership, "~> 0.2.0 or ~> 0.3.0", [hex: :nimble_ownership, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "2ddd3d33f9ab714ced8d3c15fd03db40c14dbf129003c4a3eb80fac2cc0b1b08"}, "telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"}, }