Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Removing AuthenticatedClient in favor of Client.with_authorization_header and Generics#1287

alexandervaneck started this conversation inFeature request
Discussion options

Hi, I've been exploring this package and am very excited about the output it generates. Especially the extensibility due to the jinja2 templating is great! Thank you for maintaining it. 🙇

After generating a client I noticed that there's a lot of duplication between AuthenticatedClient and Client. From what I can tell the usage of AuthenticatedClient is:

  1. To pre-load Client with authentication headers to authenticate requests.
  2. To have good type hinting in endpoint_module's, showing when an endpoint required authentication.

Setting authentication headers

Currently we solve for this usecase:

client=Client()# will make unauthenticated requestsauth_client=AuthenticatedClient(token='my-token')# will make authenticated requests

However, when the token needs to change (f.e. b/c different users use different tokens) then we'd have to completely change the client. Leading to thehttpx.ClientSession being recreated and causing overhead.

auth_client1=AuthenticatedClient(token='user1-token')# will make authenticated requests for user1auth_client2=AuthenticatedClient(token='user2-token')# will make authenticated requests for user2

It might be nicer to instead have methods onClient to set and del authorization headers so that the Client can be longlived and the authentication can vary per request.

client=Client()client.with_authorization_token(token='user1-token')client.request()# will make auth requests for user1client.with_authorization_token(token='user2-token')

The added benefit here is that we can solve for#823 by addingwith_authorization_basic as well. Without duplicating Client more.

Type hinting

To make clear when an endpoint requires authentication headers set we can add a phantom type parameter.

frombase64importb64encodefromtypingimportDict,Generic,TypeVar,castfromattrsimportdefine,evolve,fieldclassUnauthenticated: ...classAuthenticated: ..._State=TypeVar("_State")@defineclass_Client(Generic[_State]):_headers:Dict[str,str]=field(factory=dict,kw_only=True)defwith_headers(self,headers:Dict[str,str])->"Client":returnevolve(self,_headers={**self._headers,**headers})defwith_authorization_token(self:"Client",token:str,prefix:str="Bearer",    )->"AuthClient":returncast("AuthClient",self.with_headers({"Authorization":f"{prefix}{token}"}))defwith_authorization_basic(self:"Client",username:str,password:str,prefix:str="Basic",    )->"AuthClient":encoded_credentials=b64encode(f"{username}:{password}".encode()).decode()returncast("AuthClient",self.with_headers({"Authorization":f"{prefix}{encoded_credentials}"}))defremove_headers(self:"AuthClient")->"Client":returnevolve(self,_headers={})Client=_Client[Unauthenticated]AuthClient=_Client[Authenticated]defmy_func(client:AuthClient)->None:# only accepts authenticated clientsprint("got headers:",client._headers)# --- usage demos ---c0=Client()# Unauthenticatedmy_func(c0)# static type errorc1=c0.with_authorization_token("my-token")# Authenticatedmy_func(c1)# okc2=c1.remove_headers()# back to Unauthenticatedmy_func(c2)# static type errorc1=c0.with_authorization_basic("my-username","my-password")# Authenticatedmy_func(c1)# ok

Let me know what you think. I'm happy to suggest the PR 🙇 and thanks again!

You must be logged in to vote

Replies: 2 comments

Comment options

Oh, and this would be a breaking change asAuthenticatedClient would not taketoken as input anymore.

You must be logged in to vote
0 replies
Comment options

cc@johnthagen@dbanty

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
1 participant
@alexandervaneck

[8]ページ先頭

©2009-2025 Movatter.jp