diff --git a/lib/sanity.ex b/lib/sanity.ex index fdd2a00..7d7ffea 100644 --- a/lib/sanity.ex +++ b/lib/sanity.ex @@ -41,12 +41,6 @@ defmodule Sanity do default: [receive_timeout: 30_000], doc: "Options to be passed to `Req.request/2`." ], - max_attempts: [ - type: :pos_integer, - default: 1, - doc: - "Number of attempts to make before returning error. Requests receiving an HTTP status code of 4xx will not be retried." - ], project_id: [ type: :string, doc: "Sanity project ID.", @@ -57,12 +51,6 @@ defmodule Sanity do default: Req, doc: false ], - retry_delay: [ - type: :pos_integer, - default: 1_000, - doc: - "Delay in ms to wait before retrying after an error. Applies if `max_attempts` is greater than `1`." - ], token: [ type: :string, doc: "Sanity auth token." @@ -251,42 +239,23 @@ defmodule Sanity do ) do opts = NimbleOptions.validate!(opts, @request_options_schema) - result = - 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, %Req.Response{body: body, headers: headers, status: status}}} + 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 do + {:ok, %Req.Response{body: body, headers: headers, status: status}} when status in 200..299 -> {:ok, %Response{body: body, headers: headers, status: status}} - {_, {:ok, %Req.Response{body: body, headers: headers, status: status} = resp}} + {:ok, %Req.Response{body: %{} = body, headers: headers, status: status}} when status in 400..499 -> - 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 -> - Logger.warning( - "retrying failed request in #{opts[:retry_delay]}ms: #{inspect(error_or_response)}" - ) - - :timer.sleep(opts[:retry_delay]) + {:error, %Response{body: body, headers: headers, status: status}} - opts = - opts - |> Keyword.update!(:max_attempts, &(&1 - 1)) - |> Keyword.update!(:retry_delay, &(&1 * 2)) - - request(request, opts) - - {_, {_, error_or_response}} -> + {_, error_or_response} -> raise %Sanity.Error{source: error_or_response} end end @@ -335,8 +304,7 @@ defmodule Sanity do request_opts: [ type: :keyword_list, required: true, - doc: - "Options to be passed to `request/2`. If `max_attempts` is omitted then it will default to `3`." + doc: "Options to be passed to `request/2`." ], variables: [ type: {:map, {:or, [:atom, :string]}, :any}, @@ -363,10 +331,7 @@ defmodule Sanity do @impl true @spec stream(Keyword.t()) :: Enumerable.t() def stream(opts) do - opts = - opts - |> NimbleOptions.validate!(@stream_options_schema) - |> Keyword.update!(:request_opts, &Keyword.put_new(&1, :max_attempts, 3)) + opts = NimbleOptions.validate!(opts, @stream_options_schema) case Map.take(opts[:variables], [:pagination_last_id, "pagination_last_id"]) |> Map.keys() do [] -> nil diff --git a/test/sanity_test.exs b/test/sanity_test.exs index 0f04a03..277b0cb 100644 --- a/test/sanity_test.exs +++ b/test/sanity_test.exs @@ -354,15 +354,13 @@ defmodule SanityTest do end test "query, projection, and variables options" do - Mox.expect(MockSanity, :request!, fn %Request{query_params: query_params}, request_opts -> + Mox.expect(MockSanity, :request!, fn %Request{query_params: query_params}, _request_opts -> assert query_params == %{ "$type" => "\"page\"", "query" => "*[(_type == $type) && (!(_id in path('drafts.**')))] | order(_id) [0..999] { _id, title }" } - assert request_opts[:max_attempts] == 3 - %Response{body: %{"result" => [%{"_id" => "a", "title" => "home"}]}} end)