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

Improve 'Custom transports' docs #3081

Merged
merged 2 commits into from Feb 14, 2024
Merged

Conversation

tomchristie
Copy link
Member

Now includes examples with...

  • HelloWorldTransport
  • HTTPSRedirect
  • DebuggingTransport
  • ProxyRoundRobin

@tomchristie tomchristie added the docs Changes to the documentation label Feb 2, 2024
@tomchristie tomchristie marked this pull request as ready for review February 2, 2024 15:41
@tomchristie tomchristie requested a review from a team February 2, 2024 15:41
@karpetrosyan
Copy link
Member

One more useful example:

import httpx


class FirstMiddleWareTransport(httpx.BaseTransport):
    def __init__(self, next_transport: httpx.BaseTransport):
        self.next_transport = next_transport

    def handle_request(self, request: httpx.Request) -> httpx.Response:
        print("First middleware request")
        response = self.next_transport.handle_request(request)
        print("First middleware response")
        return response


class SecondMiddleWareTransport(httpx.BaseTransport):
    def __init__(self, next_transport: httpx.BaseTransport):
        self.next_transport = next_transport

    def handle_request(self, request: httpx.Request) -> httpx.Response:
        print("Second middleware request")
        response = self.next_transport.handle_request(request)
        print("Second middleware response")
        return response


middleware_transport = FirstMiddleWareTransport(
    next_transport=SecondMiddleWareTransport(next_transport=httpx.HTTPTransport())
)
client = httpx.Client(transport=middleware_transport)

client.get("https://www.encode.io")

OUTPUT

First middleware request
Second middleware request
Second middleware response
First middleware response

@tomchristie
Copy link
Member Author

@karpetrosyan I like the intent of demonstrating how transports can be used as nestable middleware;
I'm less keen on an abstract example that's not demo'ing a real-life use-case.

(Should we drop the "hello world" example, or is the PR okay as it currently stands?)

@karpetrosyan
Copy link
Member

(Should we drop the "hello world" example, or is the PR okay as it currently stands?)

It's up to you, it looks great anyway.

@tomchristie
Copy link
Member Author

Thanks @karpetrosyan appreciate your time. 🙏🏼

@tomchristie tomchristie merged commit 3faa4a8 into master Feb 14, 2024
5 checks passed
@tomchristie tomchristie deleted the improve-custom-transport-docs branch February 14, 2024 11:14
shepilov-vladislav pushed a commit to shepilov-vladislav/httpx that referenced this pull request Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Changes to the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants