From 2b2aba85131f27c5d8edaebc1d947338c1ed74e8 Mon Sep 17 00:00:00 2001 From: Aaron Renner Date: Fri, 4 Aug 2023 10:59:49 -0600 Subject: [PATCH] Use Tesla as http client for telemetry hooks (#1) --- lib/algolia.ex | 41 +++++++++++++++++++++++++++++------------ mix.exs | 2 ++ mix.lock | 3 +++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/algolia.ex b/lib/algolia.ex index 161ebdc..a403e2c 100644 --- a/lib/algolia.ex +++ b/lib/algolia.ex @@ -157,20 +157,27 @@ defmodule Algolia do headers = request_headers(config, request[:options] || []) body = request[:body] || "" - request[:method] - |> :hackney.request(url, headers, body, [ - :with_body, - path_encode_fun: &URI.encode/1, - connect_timeout: 3_000 * (curr_retry + 1), - recv_timeout: 30_000 * (curr_retry + 1), - ssl_options: [{:versions, [:"tlsv1.2"]}] - ]) + tesla_client() + |> Tesla.request( + method: request[:method], + url: url, + headers: headers, + body: body, + opts: [ + adapter: [ + path_encode_fun: &URI.encode/1, + connect_timeout: 3_000 * (curr_retry + 1), + recv_timeout: 30_000 * (curr_retry + 1), + ssl_options: [{:versions, [:"tlsv1.2"]}] + ] + ] + ) |> case do - {:ok, code, _headers, response} when code in 200..299 -> - {:ok, Jason.decode!(response)} + {:ok, %Tesla.Env{status: status} = env} when status in 200..299 -> + {:ok, env.body} - {:ok, code, _, response} -> - {:error, code, response} + {:ok, %Tesla.Env{} = env} -> + {:error, env.status, env.body} _ -> send_request(config, read_or_write, request, curr_retry + 1) @@ -646,4 +653,14 @@ defmodule Algolia do def wait(response, _opts), do: response defp default_config, do: Config.new() + + defp tesla_client do + Tesla.client( + [ + Tesla.Middleware.Telemetry, + Tesla.Middleware.JSON + ], + Tesla.Adapter.Hackney + ) + end end diff --git a/mix.exs b/mix.exs index 56ecf52..6abf937 100644 --- a/mix.exs +++ b/mix.exs @@ -30,6 +30,8 @@ defmodule Algolia.Mixfile do defp deps do [ {:hackney, "~> 1.9 or ~> 1.10"}, + {:tesla, "~> 1.5"}, + {:telemetry, "~> 0.4 or ~> 1.0"}, {:jason, "~> 1.0"}, # Docs {:ex_doc, "~> 0.19", only: :dev}, diff --git a/mix.lock b/mix.lock index e48cf94..4351716 100644 --- a/mix.lock +++ b/mix.lock @@ -12,10 +12,13 @@ "makeup_elixir": {:hex, :makeup_elixir, "0.15.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"}, "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, + "mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"}, "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, "nimble_parsec": {:hex, :nimble_parsec, "1.2.0", "b44d75e2a6542dcb6acf5d71c32c74ca88960421b6874777f79153bbbbd7dccc", [:mix], [], "hexpm", "52b2871a7515a5ac49b00f214e4165a40724cf99798d8e4a65e4fd64ebd002c1"}, "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, + "telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"}, + "tesla": {:hex, :tesla, "1.7.0", "a62dda2f80d4f8a925eb7b8c5b78c461e0eb996672719fe1a63b26321a5f8b4e", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.13", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:msgpax, "~> 2.3", [hex: :msgpax, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "2e64f01ebfdb026209b47bc651a0e65203fcff4ae79c11efb73c4852b00dc313"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, }