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 http header support #66

Merged

Conversation

johncantrell97
Copy link
Contributor

@johncantrell97 johncantrell97 commented Dec 6, 2023

Technically an end-user could do this themselves because the builders support passing in pre-built ureq/reqwest clients but this makes it a bit easier for them.

Summary by CodeRabbit

  • New Features
    • Introduced the ability to set custom HTTP headers for requests in both asynchronous and synchronous clients.
    • Added methods in the Builder struct for adding HTTP headers to requests.
  • Improvements
    • Enhanced error handling with new error variants for invalid HTTP header names and values.
  • Tests
    • Updated test setup to include custom HTTP headers.
    • Added a new test case for fetching transactions with specific HTTP headers.

@LLFourn
Copy link
Contributor

LLFourn commented Dec 8, 2023

Out of curiosity what do you need custom heeaders for?

@johncantrell97
Copy link
Contributor Author

Access to authenticated esplora servers. most public ones are heavily rate limited

@notmandatory
Copy link
Member

Please rebase to pick up changes in #69 that fix CI.

@johncantrell97 johncantrell97 force-pushed the http-header-support branch 2 times, most recently from ab52545 to 9fabcf6 Compare January 18, 2024 18:25
@johncantrell97
Copy link
Contributor Author

rebased

Copy link
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

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

utACK 9fabcf6

@notmandatory
Copy link
Member

@johncantrell97 thanks for your work on this. Our github rules also require commits be signed, can you amend/sign your commit?

@notmandatory notmandatory added the enhancement New feature or request label Feb 14, 2024
@notmandatory
Copy link
Member

@johncantrell97 hey can you rebase and rework this one now that #75 is merged? hopefully not a big change.

Copy link
Collaborator

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

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

ACK

Copy link

coderabbitai bot commented Mar 16, 2024

Walkthrough

The update introduces the ability to handle custom HTTP headers across both asynchronous and blocking clients. This is achieved by adding a headers field to the Builder struct, allowing users to specify headers that are then applied to all requests. Additionally, the update includes error handling for invalid header names and values, and expands testing to cover the new functionality, ensuring that specified headers are correctly used in requests.

Changes

Files Change Summary
src/async.rs, src/blocking.rs Added logic to include custom HTTP headers in requests by utilizing a HeaderMap in AsyncClient and a HashMap<String, String> in BlockingClient.
src/lib.rs Introduced a headers field in the Builder struct for HTTP headers, methods to add headers, error variants for invalid headers, and expanded testing setup.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 8310674431

Details

  • 51 of 63 (80.95%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.2%) to 84.568%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/lib.rs 43 47 91.49%
src/async.rs 2 10 20.0%
Totals Coverage Status
Change from base Build 8167199538: -0.2%
Covered Lines: 959
Relevant Lines: 1134

💛 - Coveralls

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 98edbc5 and 73cc41c.
Files selected for processing (3)
  • src/async.rs (2 hunks)
  • src/blocking.rs (3 hunks)
  • src/lib.rs (7 hunks)
Additional comments: 11
src/blocking.rs (3)
  • 39-40: The addition of the headers field in the BlockingClient struct is a straightforward and effective way to support custom HTTP headers. This change aligns with the PR's objective to simplify the process for end-users to include custom HTTP headers in their requests.
  • 50-50: Correctly transferring the headers from the Builder to the BlockingClient during construction ensures that any headers specified during client configuration are preserved. This is a key part of making the feature user-friendly and functional.
  • 66-70: The implementation of header addition to each request made by the BlockingClient is well-done. Looping through the headers map and applying each key-value pair as a header to the request object is the correct approach. However, it's important to ensure that the headers being set do not inadvertently overwrite any essential headers that minreq might set by default, such as Content-Type for certain requests.

Consider verifying if minreq sets any default headers that should not be overwritten by custom headers, or if there should be a mechanism to prevent certain headers from being overwritten by the user.

src/async.rs (2)
  • 27-27: The import of reqwest::{header, Client, StatusCode} is necessary for the changes made to support custom HTTP headers in the AsyncClient. This ensures that the required types and modules are available for use in the header handling logic.
  • 52-62: The logic to convert the headers from a HashMap<String, String> to a reqwest::header::HeaderMap and setting them as default headers for the reqwest::Client is correctly implemented. This approach allows for the headers to be included in every request made by the AsyncClient. It's important to handle potential errors when converting strings to HeaderName and HeaderValue, as done here, to ensure that only valid headers are set. Additionally, consider documenting the behavior regarding overwriting default headers set by reqwest to make it clear to users how custom headers interact with default ones.

Verify and document how custom headers provided by users interact with default headers set by reqwest, especially in cases where there might be conflicts.

src/lib.rs (6)
  • 120-121: Adding a headers field to the Builder struct is a straightforward and effective way to allow users to specify custom HTTP headers for their requests. This change aligns with the PR's objective to simplify the process of including custom headers, especially for authenticated Esplora servers.
  • 131-131: Initializing the headers field with an empty HashMap in the Builder::new method ensures that users can optionally add headers without having to deal with Option<HashMap<...>>. This is a good practice for keeping the API simple and user-friendly.
  • 147-151: The header method provides a convenient way for users to add individual headers to their requests. This method's design, which takes key-value pairs as strings and returns the modified Builder instance, follows the builder pattern effectively, allowing for fluent API usage.
  • 194-196: The addition of InvalidHttpHeaderName and InvalidHttpHeaderValue error variants to the Error enum is crucial for robust error handling. It allows the library to provide more specific feedback to the user when they attempt to set invalid headers, enhancing the overall usability and reliability of the library.
  • 274-286: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [277-297]

The setup_clients_with_headers function in the test module demonstrates how the new headers functionality can be used in practice. By allowing the function to take a HashMap of headers and applying it to the Builder, this setup is flexible and can be easily extended for additional tests. However, it's important to ensure that the headers used in tests reflect realistic scenarios that users might encounter.

  • 881-913: The test test_get_tx_with_http_header effectively verifies that custom HTTP headers are correctly applied to requests made by both the blocking and async clients. Using an "Authorization" header in the test is a practical choice, as it represents a common use case for custom headers. This test enhances the confidence in the new functionality's correctness.

@notmandatory
Copy link
Member

notmandatory commented Mar 16, 2024

I've rebased this on lastest master changes and updated to work with minreq for blocking module and added a simple test with http headers. In the process of rebasing I also signed the commits so github will let us merge it. Note: minreq doesn't include any header key, value validation.

Copy link
Collaborator

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

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

ACK

@notmandatory notmandatory merged commit bbeaab4 into bitcoindevkit:master Mar 21, 2024
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants