Skip to content

Commit

Permalink
Dependabot/hex/gun 2.0.1 merge master (#629)
Browse files Browse the repository at this point in the history
* 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] <support@github.com>

* remove comment

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Yordis Prieto <yordis.prieto@gmail.com>

* 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] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Yordis Prieto <yordis.prieto@gmail.com>

* 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 <yordis.prieto@gmail.com>

* 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] <support@github.com>
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] <support@github.com>
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] <support@github.com>
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] <support@github.com>
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] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* feat: improve decompression middleware (#606)

closes #598

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>

* 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] <support@github.com>
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Yordis Prieto <yordis.prieto@gmail.com>
Co-authored-by: Austin Ziegler <austin@zieglers.ca>
  • Loading branch information
4 people authored Oct 25, 2023
1 parent ea09209 commit b035b0b
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 162 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
name: Publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- 25.3
- 24.3
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
Linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
Expand Down
2 changes: 2 additions & 0 deletions lib/tesla/adapter/mint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ if Code.ensure_loaded?(Mint.HTTP) do
_ -> opts
end

opts = Map.put_new(opts, :mode, :passive)

with {:ok, conn} <-
HTTP.connect(String.to_atom(uri.scheme), uri.host, uri.port, Enum.into(opts, [])) do
# If there were redirects, and passed `closed_conn: false`, we need to close opened connections to these intermediate hosts.
Expand Down
52 changes: 52 additions & 0 deletions lib/tesla/middleware/base_url.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
defmodule Tesla.Middleware.BaseUrl do
@moduledoc """
Set base URL for all requests.
The base URL will be prepended to request path/URL only
if it does not include http(s).
## Examples
```
defmodule MyClient do
use Tesla
plug Tesla.Middleware.BaseUrl, "https://example.com/foo"
end
MyClient.get("/path") # equals to GET https://example.com/foo/path
MyClient.get("path") # equals to GET https://example.com/foo/path
MyClient.get("") # equals to GET https://example.com/foo
MyClient.get("http://example.com/bar") # equals to GET http://example.com/bar
```
"""

@behaviour Tesla.Middleware

@impl Tesla.Middleware
def call(env, next, base) do
env
|> apply_base(base)
|> Tesla.run(next)
end

defp apply_base(env, base) do
if Regex.match?(~r/^https?:\/\//i, env.url) do
# skip if url is already with scheme
env
else
%{env | url: join(base, env.url)}
end
end

defp join(base, url) do
case {String.last(to_string(base)), url} do
{nil, url} -> url
{"/", "/" <> rest} -> base <> rest
{"/", rest} -> base <> rest
{_, ""} -> base
{_, "/" <> rest} -> base <> "/" <> rest
{_, rest} -> base <> "/" <> rest
end
end
end
63 changes: 57 additions & 6 deletions lib/tesla/middleware/compression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ defmodule Tesla.Middleware.Compression do
def call(env, next, opts) do
env
|> compress(opts)
|> Tesla.put_headers([{"accept-encoding", "gzip, deflate"}])
|> add_accept_encoding()
|> Tesla.run(next)
|> decompress()
end

@doc false
def add_accept_encoding(env) do
Tesla.put_headers(env, [{"accept-encoding", "gzip, deflate, identity"}])
end

defp compressible?(body), do: is_binary(body)

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

def decompress(env) do
codecs = compression_algorithms(Tesla.get_header(env, "content-encoding"))
{decompressed_body, unknown_codecs} = decompress_body(codecs, env.body, [])

env
|> Tesla.put_body(decompress_body(env.body, Tesla.get_header(env, "content-encoding")))
|> put_decompressed_body(decompressed_body)
|> put_or_delete_content_encoding(unknown_codecs)
end

defp put_or_delete_content_encoding(env, []) do
Tesla.delete_header(env, "content-encoding")
end

defp put_or_delete_content_encoding(env, unknown_codecs) do
Tesla.put_header(env, "content-encoding", Enum.join(unknown_codecs, ", "))
end

defp decompress_body([gzip | rest], body, acc) when gzip in ["gzip", "x-gzip"] do
decompress_body(rest, :zlib.gunzip(body), acc)
end

defp decompress_body(<<31, 139, 8, _::binary>> = body, "gzip"), do: :zlib.gunzip(body)
defp decompress_body(body, "deflate"), do: :zlib.unzip(body)
defp decompress_body(body, _content_encoding), do: body
defp decompress_body(["deflate" | rest], body, acc) do
decompress_body(rest, :zlib.unzip(body), acc)
end

defp decompress_body(["identity" | rest], body, acc) do
decompress_body(rest, body, acc)
end

defp decompress_body([codec | rest], body, acc) do
decompress_body(rest, body, [codec | acc])
end

defp decompress_body([], body, acc) do
{body, acc}
end

defp compression_algorithms(nil) do
[]
end

defp compression_algorithms(value) do
value
|> String.downcase()
|> String.split(",", trim: true)
|> Enum.map(&String.trim/1)
|> Enum.reverse()
end

defp put_decompressed_body(env, body) do
env
|> Tesla.put_body(body)
|> Tesla.delete_header("content-length")
end
end

defmodule Tesla.Middleware.CompressRequest do
Expand Down Expand Up @@ -99,7 +150,7 @@ defmodule Tesla.Middleware.DecompressResponse do
@impl Tesla.Middleware
def call(env, next, _opts) do
env
|> Tesla.put_headers([{"accept-encoding", "gzip, deflate"}])
|> Tesla.Middleware.Compression.add_accept_encoding()
|> Tesla.run(next)
|> Tesla.Middleware.Compression.decompress()
end
Expand Down
131 changes: 0 additions & 131 deletions lib/tesla/middleware/core.ex

This file was deleted.

24 changes: 24 additions & 0 deletions lib/tesla/middleware/headers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Tesla.Middleware.Headers do
@moduledoc """
Set default headers for all requests
## Examples
```
defmodule Myclient do
use Tesla
plug Tesla.Middleware.Headers, [{"user-agent", "Tesla"}]
end
```
"""

@behaviour Tesla.Middleware

@impl Tesla.Middleware
def call(env, next, headers) do
env
|> Tesla.put_headers(headers)
|> Tesla.run(next)
end
end
16 changes: 13 additions & 3 deletions lib/tesla/middleware/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ defmodule Tesla.Middleware.Logger do
By default, the following log levels will be used:
- `:error` - for errors, 5xx and 4xx responses
- `:warn` - for 3xx responses
- `:warn` or `:warning` - for 3xx responses
- `:info` - for 2xx responses
You can customize this setting by providing your own `log_level/1` function:
Expand Down Expand Up @@ -186,7 +186,13 @@ defmodule Tesla.Middleware.Logger do

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

@type log_level :: :info | :warn | :error
@type log_level :: :info | :warn | :warning | :error

if Version.compare(System.version(), "1.11.0") == :lt do
@warning_level :warn
else
@warning_level :warning
end

require Logger

Expand Down Expand Up @@ -221,9 +227,13 @@ defmodule Tesla.Middleware.Logger do
fun when is_function(fun) ->
case fun.(env) do
:default -> default_log_level(env)
warning when warning in [:warn, :warning] -> @warning_level
level -> level
end

warning when warning in [:warn, :warning] ->
@warning_level

atom when is_atom(atom) ->
atom
end
Expand All @@ -233,7 +243,7 @@ defmodule Tesla.Middleware.Logger do
def default_log_level(env) do
cond do
env.status >= 400 -> :error
env.status >= 300 -> :warn
env.status >= 300 -> @warning_level
true -> :info
end
end
Expand Down
Loading

0 comments on commit b035b0b

Please sign in to comment.