question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Public middleware API

See original GitHub issue

Problem We currently have an internal middleware API that was brought in via #295 and #268.

We should be looking at making that API (or a different form of it?) public so that users can extend the functionality of the client to fit their needs.

For example, a retry functionality (discussed in https://github.com/encode/httpx/issues/108 and drafted in https://github.com/encode/httpx/pull/134 then abandoned) could probably be implemented as a middleware (basic idea here: https://github.com/encode/httpx/pull/268#issuecomment-526909874).

Questions to be answered

  • Is a public middleware API still a good idea at all? (IMO, it is.)
  • We already have a middleware-side interface: request, get_response -> response. What’s the client-side API going to look like? Probably something like…
client = httpx.Client()
client.add_middleware(RetryMiddleware, max_retries=3)
  • Should we apply existing internal middleware such as auth and redirect by default?

IMO, yes: treat those as core middleware, and add custom middleware on top. It might depend on the use case, but we can start off with that.

  • Should users be able to we control at what level a new middleware is applied, and how?

@sethmlarson proposed an idea inspired by Pyramid here: https://github.com/encode/httpx/issues/295#issuecomment-526933309

  • Should middleware be able to store session-wide data, and how?

This was originally mentioned in https://github.com/encode/httpx/issues/295#issuecomment-526933309, and would be used to store state worth remembering such as cookies or Alt-Svc headers for HTTP/3 (see https://github.com/encode/httpx/issues/275#issuecomment-531067555).

IMO the use case still needs to be refined for us to clearly see what needs to be done.

What needs to be done

This is a proposal. 😃

  • Answer the questions above and add the public middleware API.
  • Document how to write middleware (Advanced section?). We learnt by experience (e.g. https://github.com/encode/httpx/pull/332#discussion_r322816118) that middleware instances shouldn’t hold any mutable state, so let’s make sure we mention that here.
  • Document how to use middleware (Advanced section?).
  • (Later) Figure out middleware ordering.
  • (Later) Figure out client-side storage.

All thoughts welcome!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
lepturecommented, Oct 9, 2019

@kesavkolla I think what you need is something like https://docs.authlib.org/en/latest/client/httpx.html

Stay tuned, Authlib HTTPX feature is not released yet, it will be released in v0.13.

1reaction
tomchristiecommented, Dec 5, 2019

This will need some significant design work.

I think we should treat this as out-of-scope at this point in time, in order to get an API-stable 1.0 release squared away.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the difference between API and middleware? | PeerSpot
The API (Application Programming Interface) can be provided by most middleware. It can be SOAP but generally, REST services.
Read more >
API vs. Middleware Connections | Bear Ideas - Bear Group
Use a middleware system as a connector between your website and other tools. Here's how to find which approach is right for you....
Read more >
Middleware - Laravel - The PHP Framework For Web Artisans
Laravel includes predefined web and api middleware groups that contain common middleware you may want to apply to your web and API routes....
Read more >
What is middleware? - Red Hat
Middleware is software that provides common services and capabilities ... interface (API) management are all commonly handled by middleware.
Read more >
Advanced Features: Middleware - Next.js
Learn how to use Middleware to run code before a request is completed. ... Match all request paths except for the ones starting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found