Skip to content

Conversation

@lovelydinosaur
Copy link
Contributor

Now includes examples with...

  • HelloWorldTransport
  • HTTPSRedirect
  • DebuggingTransport
  • ProxyRoundRobin

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

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

@lovelydinosaur
Copy link
Contributor 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
Contributor

(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.

@lovelydinosaur
Copy link
Contributor Author

Thanks @karpetrosyan appreciate your time. 🙏🏼

@lovelydinosaur lovelydinosaur merged commit 3faa4a8 into master Feb 14, 2024
@lovelydinosaur lovelydinosaur 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
samclearman pushed a commit to titanmsp/httpx that referenced this pull request Apr 26, 2025
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.

3 participants