Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Req http client (draft) #846

Closed
wants to merge 1 commit into from

Conversation

krns
Copy link

@krns krns commented Jan 3, 2025

Hello, I would be happy if Req would be supported as http client lib. Here is a working proof of concept.

Since Hackney is the only HTTP client that is supported so far, I had to bend a little in some places. A few comments on this below.

One general question:

Because Req is not known in the Sentry library I get a few warning like:

warning: Req.new/1 is undefined (module Req is not available or is yet to be defined). Make sure the module name is correct and has been specified in full (or that an alias has been defined)
warning: Req.merge/2 is undefined (module Req is not available or is yet to be defined). Make sure the module name is correct and has been specified in full (or that an alias has been defined)
warning: Req.post/1 is undefined (module Req is not available or is yet to be defined). Make sure the module name is correct and has been specified in full (or that an alias has been defined)

Should I make use of Code.ensure_loaded?(Req) and Application.ensure_all_started(:req)?

Comment on lines +29 to +30
@requirements ["app.start"]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this Req-specific change inside the test command but I left it like this for now because I wanted to get some general feedback first.

Details why this is needed: https://elixirforum.com/t/argumenterror-unknown-registry-req-finch-on-a-simple-http-request-using-req-library/64638/9

|> Req.merge(headers: headers)

case Req.post(req) do
{:ok, %{status: status, headers: headers, body: body}} = result -> {:ok, status, headers, body}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pattern match when Req is available.

Suggested change
{:ok, %{status: status, headers: headers, body: body}} = result -> {:ok, status, headers, body}
{:ok, %Req.Response{status: status, headers: headers, body: body}} = result -> {:ok, status, headers, body}

I transform the result into the Hackney-inspired form so everything else works.

Comment on lines +19 to +21
|> Keyword.put(:decode_body, false)
|> Keyword.put(:url, url)
|> Keyword.put(:body, body)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Req decodes responses automatically. I choose to disable body decoding so the users can make use of the json_library option.

when is_integer(status) and status in 200..599 and is_list(resp_headers) and
when is_integer(status) and status in 200..599 and
Copy link
Author

@krns krns Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the is_list guard because it's highly Hackney-specific (Req response headers are a Map).

@whatyouhide
Copy link
Collaborator

@krns thanks for the PR, but I don't think this is the right approach. This SDK already supports the Sentry.HTTPClient behaviour. With that, you can use any custom HTTP client (which includes Req) provided you will write a translation layer for it. For example, here's how to use Finch—a blog post from 4 years ago.

We are already looking at replacing the default Hackney-based client with one based on Finch (#758), but I’m not convinced this is worth it if Erlang/Elixir will ever get a stdlib HTTP client. We might opt for Mint instead if they don't, since it will be the lowest-level and hopefully most-widely-adopted-as-base client.

Hopefully this all makes sense. Cheers!

@whatyouhide whatyouhide closed this Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants