Skip to content

Some API oddities/possible oversights #365

Closed

Description

@thebigmunch

While I was working on porting my google-music library to httpx using pared down oauth2 code borrowed from requests-oauthlib (see the quick stab at it here if interested), I noticed some API oddities/differences from requests even with the relatively small amount of usage in my library.

  • BasicAuthMiddleware, previously HTTPBasicAuth, is not available at the top level of the package, nor any other middleware aside from DigestAuth. The current naming inconsistency between the two is also strange. Both are middleware, and it was named HTTPBasicAuth in requests and previously in httpx. Though I could certainly see the inconsistency being DigestAuth as all other middleware, including CustomAuthMiddleware, follow the same pattern. I think something needs to happen as far as the exposing, organizing, and making consistent of things like this. What that thing is, I'm not sure. Quick opinion is that these classes being middleware is more of an implementation detail than anything else. I'd expect the auth classes to be exposed through auth like previously and in requests or at the top level. I'd also expect them without "Middleware" in the names (possibly just aliased).

  • HTTPError is not available at the top level like the other exceptions and like it was in requests. This is a somewhat common exception to catch raise_for_status, so having to add another import statement for it is a bit odd if intentional.

  • requests.Session has a params attribute that attaches those params to each session request. This is not present in httpx Client classes like with headers/cookies. This is a useful feature for this and other use cases of mine and a divergence from requests.

Thanks for all the work. Been excited about httpx since the beginning. Glad to finally start using it more. Interested in seeing the discussion around these points.

Activity

  1. added
    requests-compatIssues related to Requests backwards compatibility
    user-experienceEnsuring that users have a good experience using the library
    on Sep 21, 2019
  2. florimondmanca commented on Sep 21, 2019

    @florimondmanca
    Contributor

    Thanks @thebigmunch, IMO these are all valid points. :)

  3. florimondmanca commented on Sep 21, 2019

    @florimondmanca
    Contributor

    I opened separate issues for the last two points you reported.

    As for the auth issues:

    • I do agree that we should be hiding the "middleware" part of it, as it is indeed an implementation detail, because the middleware API is still internal.

    • As far as naming is concerned, the point in time we decided to use DigestAuth instead of HTTPDigestAuth is here: Digest auth middleware #305 (comment).

      The rationale was mostly that DigestAuth looks better. I personally do agree that the HTTP prefix seems arbitrary, but that also means we do break Requests compatibility there.

      So, two options:

      1. Use HTTPDigestAuth, HTTPDigestAuth, and HTTPCustomAuth. (The latter did not exist in Requests, as auth customization went through the AuthBase class.)
      2. Keep BasicAuth, DigestAuth and CustomAuth, but expose aliases for HTTPBasicAuth and HTTPDigestAuth. This kind of breaks the principle of having one obvious way to do things, so I think we'd have to raise some kind of warning on the aliased classes, encouraging users to rename?
    • I'd be fine with re-exporting the auth classes under a public httpx.auth module, to comply with Requests and also make it more obvious where those classes should be imported from. (Again, importing them from httpx.middleware as we do currently exposes an implementation detail.)

    Happy to read thoughts from other @encode/httpx-maintainers on this. :)

  4. thebigmunch commented on Sep 21, 2019

    @thebigmunch
    ContributorAuthor

    I also agree that the HTTP is unnecessary as well, for what it's worth. This is an HTTP client package named httpx after all : P

    I also don't believe a strict 1:1 compat with requests is necessary or necessarily preferable. Frankly, for all the good parts of its API, there is also plenty that could have been better (e.g. see Brett Cannon's blog post on http request API's). Instead, take a look, as we are now, at user experience and expectations. Using an auth module, even with a different class name, will be the obvious place a user would look for auth-related functionality.

    If I were going to do it, I'd move the BaseMiddleware class to the top-level models module as that's really what it is here. Then move the auth classes to an auth module. Expose/move Redirect middleware wherever you see it fitting best or let it stay where it is. I have less of an idea of what middleware might come in the future.

  5. sethmlarson commented on Sep 21, 2019

    @sethmlarson
    Contributor

    Hey @thebigmunch! I'll start off by saying thank you for being one of the first to take the Requests -> HTTPX migration path, especially for a library. Your perspective is invaluable ❤️.

    I'm kinda -1 about aliasing, we definitely need better docs about migrating from Requests though! About reexporting in the auth module, if it'll help users migrate then it sounds like a positive improvement.

  6. StephenBrown2 commented on Sep 23, 2019

    @StephenBrown2
    Contributor

    Aliasing but raising a DeprecationError for the aliases with a link to the migration docs would be a good middle path to take IMHO.

  7. subfxnet commented on Nov 22, 2019

    @subfxnet

    @florimondmanca Are there plans to make the client middleware API public?

    ...as it is indeed an implementation detail, because the middleware API is still internal.

    I am using httpx as our API client library and would use the middleware concept to implement tracing into our client if it was a public interface.

  8. florimondmanca commented on Nov 22, 2019

    @florimondmanca
    Contributor

    @subfxnet There aren’t current plans to make it public, no, as we might be heavily reworking this part of HTTPX soon. I believe this is something we’d like to eventually provide, though.

  9. lovelydinosaur commented on Dec 5, 2019

    @lovelydinosaur
    Member

    Alrighty, I think we 're good to close this one off now...

    • We have httpx.BasicAuth(...) and httpx.DigestAuth(...) available as top-level API now.
    • We have httpx.HTTPException available as top-level API now.
    • We have a .params interface availabe on the client.

    Thanks for helping round out some edges there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    requests-compatIssues related to Requests backwards compatibilityuser-experienceEnsuring that users have a good experience using the library

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions