Skip to content

Commit b035b0b

Browse files
carrascoacddependabot[bot]yordishalostatue
authored
Dependabot/hex/gun 2.0.1 merge master (#629)
* chore(deps): bump ibrowse from 4.4.0 to 4.4.2 (#594) * chore(deps): bump ibrowse from 4.4.0 to 4.4.2 Bumps [ibrowse](https://github.com/cmullaparthi/ibrowse) from 4.4.0 to 4.4.2. - [Release notes](https://github.com/cmullaparthi/ibrowse/releases) - [Changelog](https://github.com/cmullaparthi/ibrowse/blob/master/CHANGELOG) - [Commits](cmullaparthi/ibrowse@v4.4...v4.4.2) --- updated-dependencies: - dependency-name: ibrowse dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * remove comment --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Yordis Prieto <[email protected]> * chore(deps): bump msgpax from 2.3.1 to 2.4.0 (#595) Bumps [msgpax](https://github.com/lexmag/msgpax) from 2.3.1 to 2.4.0. - [Changelog](https://github.com/lexmag/msgpax/blob/master/CHANGELOG.md) - [Commits](https://github.com/lexmag/msgpax/commits) --- updated-dependencies: - dependency-name: msgpax dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Yordis Prieto <[email protected]> * fix: use mint passive mode (#591) * chore: move modules to its own file (#615) * fix: merging adapter options using Opts middleware (#613) Signed-off-by: Yordis Prieto <[email protected]> * chore(deps-dev): bump excoveralls from 0.16.1 to 0.17.1 (#620) Bumps [excoveralls](https://github.com/parroty/excoveralls) from 0.16.1 to 0.17.1. - [Release notes](https://github.com/parroty/excoveralls/releases) - [Changelog](https://github.com/parroty/excoveralls/blob/master/CHANGELOG.md) - [Commits](parroty/excoveralls@v0.16.1...v0.17.1) --- updated-dependencies: - dependency-name: excoveralls dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps-dev): bump mix_test_watch from 1.1.0 to 1.1.1 (#619) Bumps [mix_test_watch](https://github.com/lpil/mix-test.watch) from 1.1.0 to 1.1.1. - [Changelog](https://github.com/lpil/mix-test.watch/blob/master/CHANGELOG.md) - [Commits](lpil/mix-test.watch@v1.1.0...v1.1.1) --- updated-dependencies: - dependency-name: mix_test_watch dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps): bump jason from 1.4.0 to 1.4.1 (#618) Bumps [jason](https://github.com/michalmuskala/jason) from 1.4.0 to 1.4.1. - [Release notes](https://github.com/michalmuskala/jason/releases) - [Changelog](https://github.com/michalmuskala/jason/blob/master/CHANGELOG.md) - [Commits](michalmuskala/jason@v1.4.0...v1.4.1) --- updated-dependencies: - dependency-name: jason dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps): bump actions/checkout from 3 to 4 (#616) Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](actions/checkout@v3...v4) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps-dev): bump ex_doc from 0.29.4 to 0.30.6 (#617) Bumps [ex_doc](https://github.com/elixir-lang/ex_doc) from 0.29.4 to 0.30.6. - [Changelog](https://github.com/elixir-lang/ex_doc/blob/main/CHANGELOG.md) - [Commits](elixir-lang/ex_doc@v0.29.4...v0.30.6) --- updated-dependencies: - dependency-name: ex_doc dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat: improve decompression middleware (#606) closes #598 Signed-off-by: Yordis Prieto <[email protected]> * feat: allow to configure the prefix for the telemtry events (#622) * Revert "feat: allow to configure the prefix for the telemtry events" (#624) Revert "feat: allow to configure the prefix for the telemtry events (#622)" This reverts commit 5d2a8f5. * Update Tesla.Middleware.Logger for Elixir 1.11+ (#627) Elixir 1.15 is now warning whenever the `:warn` level is used with the recommendation of using `:warning` instead. The `Logger` middleware uses `Logger.log/3`, so a compile-time warning would not be shown, only a runtime warning. This change adds `:warning` as a valid configuration option for Tesla log level and automatically translates `:warn` or `:warning` to the appropriate level (`:warn` for Elixir 1.10.x, `:warning` for Elixir 1.11.x or higher, as `Logger.warning/2` was introduced in 1.11.0). --------- Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: Yordis Prieto <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: Austin Ziegler <[email protected]>
1 parent ea09209 commit b035b0b

File tree

16 files changed

+275
-162
lines changed

16 files changed

+275
-162
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
name: Publish
1010
runs-on: ubuntu-latest
1111
steps:
12-
- uses: actions/checkout@v3
12+
- uses: actions/checkout@v4
1313
- name: Set up Elixir
1414
uses: erlef/setup-beam@v1
1515
with:

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- 25.3
1616
- 24.3
1717
steps:
18-
- uses: actions/checkout@v3
18+
- uses: actions/checkout@v4
1919
- name: Set up Elixir
2020
uses: erlef/setup-beam@v1
2121
with:
@@ -79,7 +79,7 @@ jobs:
7979
Linting:
8080
runs-on: ubuntu-latest
8181
steps:
82-
- uses: actions/checkout@v3
82+
- uses: actions/checkout@v4
8383
- name: Set up Elixir
8484
uses: erlef/setup-beam@v1
8585
with:

lib/tesla/adapter/mint.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ if Code.ensure_loaded?(Mint.HTTP) do
155155
_ -> opts
156156
end
157157

158+
opts = Map.put_new(opts, :mode, :passive)
159+
158160
with {:ok, conn} <-
159161
HTTP.connect(String.to_atom(uri.scheme), uri.host, uri.port, Enum.into(opts, [])) do
160162
# If there were redirects, and passed `closed_conn: false`, we need to close opened connections to these intermediate hosts.

lib/tesla/middleware/base_url.ex

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
defmodule Tesla.Middleware.BaseUrl do
2+
@moduledoc """
3+
Set base URL for all requests.
4+
5+
The base URL will be prepended to request path/URL only
6+
if it does not include http(s).
7+
8+
## Examples
9+
10+
```
11+
defmodule MyClient do
12+
use Tesla
13+
14+
plug Tesla.Middleware.BaseUrl, "https://example.com/foo"
15+
end
16+
17+
MyClient.get("/path") # equals to GET https://example.com/foo/path
18+
MyClient.get("path") # equals to GET https://example.com/foo/path
19+
MyClient.get("") # equals to GET https://example.com/foo
20+
MyClient.get("http://example.com/bar") # equals to GET http://example.com/bar
21+
```
22+
"""
23+
24+
@behaviour Tesla.Middleware
25+
26+
@impl Tesla.Middleware
27+
def call(env, next, base) do
28+
env
29+
|> apply_base(base)
30+
|> Tesla.run(next)
31+
end
32+
33+
defp apply_base(env, base) do
34+
if Regex.match?(~r/^https?:\/\//i, env.url) do
35+
# skip if url is already with scheme
36+
env
37+
else
38+
%{env | url: join(base, env.url)}
39+
end
40+
end
41+
42+
defp join(base, url) do
43+
case {String.last(to_string(base)), url} do
44+
{nil, url} -> url
45+
{"/", "/" <> rest} -> base <> rest
46+
{"/", rest} -> base <> rest
47+
{_, ""} -> base
48+
{_, "/" <> rest} -> base <> "/" <> rest
49+
{_, rest} -> base <> "/" <> rest
50+
end
51+
end
52+
end

lib/tesla/middleware/compression.ex

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ defmodule Tesla.Middleware.Compression do
2525
def call(env, next, opts) do
2626
env
2727
|> compress(opts)
28-
|> Tesla.put_headers([{"accept-encoding", "gzip, deflate"}])
28+
|> add_accept_encoding()
2929
|> Tesla.run(next)
3030
|> decompress()
3131
end
3232

33+
@doc false
34+
def add_accept_encoding(env) do
35+
Tesla.put_headers(env, [{"accept-encoding", "gzip, deflate, identity"}])
36+
end
37+
3338
defp compressible?(body), do: is_binary(body)
3439

3540
@doc """
@@ -61,13 +66,59 @@ defmodule Tesla.Middleware.Compression do
6166
def decompress({:error, reason}), do: {:error, reason}
6267

6368
def decompress(env) do
69+
codecs = compression_algorithms(Tesla.get_header(env, "content-encoding"))
70+
{decompressed_body, unknown_codecs} = decompress_body(codecs, env.body, [])
71+
6472
env
65-
|> Tesla.put_body(decompress_body(env.body, Tesla.get_header(env, "content-encoding")))
73+
|> put_decompressed_body(decompressed_body)
74+
|> put_or_delete_content_encoding(unknown_codecs)
75+
end
76+
77+
defp put_or_delete_content_encoding(env, []) do
78+
Tesla.delete_header(env, "content-encoding")
79+
end
80+
81+
defp put_or_delete_content_encoding(env, unknown_codecs) do
82+
Tesla.put_header(env, "content-encoding", Enum.join(unknown_codecs, ", "))
83+
end
84+
85+
defp decompress_body([gzip | rest], body, acc) when gzip in ["gzip", "x-gzip"] do
86+
decompress_body(rest, :zlib.gunzip(body), acc)
6687
end
6788

68-
defp decompress_body(<<31, 139, 8, _::binary>> = body, "gzip"), do: :zlib.gunzip(body)
69-
defp decompress_body(body, "deflate"), do: :zlib.unzip(body)
70-
defp decompress_body(body, _content_encoding), do: body
89+
defp decompress_body(["deflate" | rest], body, acc) do
90+
decompress_body(rest, :zlib.unzip(body), acc)
91+
end
92+
93+
defp decompress_body(["identity" | rest], body, acc) do
94+
decompress_body(rest, body, acc)
95+
end
96+
97+
defp decompress_body([codec | rest], body, acc) do
98+
decompress_body(rest, body, [codec | acc])
99+
end
100+
101+
defp decompress_body([], body, acc) do
102+
{body, acc}
103+
end
104+
105+
defp compression_algorithms(nil) do
106+
[]
107+
end
108+
109+
defp compression_algorithms(value) do
110+
value
111+
|> String.downcase()
112+
|> String.split(",", trim: true)
113+
|> Enum.map(&String.trim/1)
114+
|> Enum.reverse()
115+
end
116+
117+
defp put_decompressed_body(env, body) do
118+
env
119+
|> Tesla.put_body(body)
120+
|> Tesla.delete_header("content-length")
121+
end
71122
end
72123

73124
defmodule Tesla.Middleware.CompressRequest do
@@ -99,7 +150,7 @@ defmodule Tesla.Middleware.DecompressResponse do
99150
@impl Tesla.Middleware
100151
def call(env, next, _opts) do
101152
env
102-
|> Tesla.put_headers([{"accept-encoding", "gzip, deflate"}])
153+
|> Tesla.Middleware.Compression.add_accept_encoding()
103154
|> Tesla.run(next)
104155
|> Tesla.Middleware.Compression.decompress()
105156
end

lib/tesla/middleware/core.ex

Lines changed: 0 additions & 131 deletions
This file was deleted.

lib/tesla/middleware/headers.ex

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmodule Tesla.Middleware.Headers do
2+
@moduledoc """
3+
Set default headers for all requests
4+
5+
## Examples
6+
7+
```
8+
defmodule Myclient do
9+
use Tesla
10+
11+
plug Tesla.Middleware.Headers, [{"user-agent", "Tesla"}]
12+
end
13+
```
14+
"""
15+
16+
@behaviour Tesla.Middleware
17+
18+
@impl Tesla.Middleware
19+
def call(env, next, headers) do
20+
env
21+
|> Tesla.put_headers(headers)
22+
|> Tesla.run(next)
23+
end
24+
end

lib/tesla/middleware/logger.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ defmodule Tesla.Middleware.Logger do
109109
By default, the following log levels will be used:
110110
111111
- `:error` - for errors, 5xx and 4xx responses
112-
- `:warn` - for 3xx responses
112+
- `:warn` or `:warning` - for 3xx responses
113113
- `:info` - for 2xx responses
114114
115115
You can customize this setting by providing your own `log_level/1` function:
@@ -186,7 +186,13 @@ defmodule Tesla.Middleware.Logger do
186186

187187
@format Formatter.compile(@config[:format])
188188

189-
@type log_level :: :info | :warn | :error
189+
@type log_level :: :info | :warn | :warning | :error
190+
191+
if Version.compare(System.version(), "1.11.0") == :lt do
192+
@warning_level :warn
193+
else
194+
@warning_level :warning
195+
end
190196

191197
require Logger
192198

@@ -221,9 +227,13 @@ defmodule Tesla.Middleware.Logger do
221227
fun when is_function(fun) ->
222228
case fun.(env) do
223229
:default -> default_log_level(env)
230+
warning when warning in [:warn, :warning] -> @warning_level
224231
level -> level
225232
end
226233

234+
warning when warning in [:warn, :warning] ->
235+
@warning_level
236+
227237
atom when is_atom(atom) ->
228238
atom
229239
end
@@ -233,7 +243,7 @@ defmodule Tesla.Middleware.Logger do
233243
def default_log_level(env) do
234244
cond do
235245
env.status >= 400 -> :error
236-
env.status >= 300 -> :warn
246+
env.status >= 300 -> @warning_level
237247
true -> :info
238248
end
239249
end

0 commit comments

Comments
 (0)