-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_refresh_flow.py
45 lines (39 loc) · 1.56 KB
/
client_refresh_flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from getpass import getpass
import httpx
class RefreshFlow(httpx.Auth):
def __init__(self, tokens, refresh_url):
self.tokens = tokens
self.refresh_url = refresh_url
def auth_flow(self, request, attempt=0):
request.headers["Authorization"] = f"Bearer {self.tokens['access_token']}"
response = yield request
if response.status_code == 401:
# The access token has expired.
# Insert a request to get a new pair of tokens.
token_request = httpx.Request(
"POST",
self.refresh_url,
json={"refresh_token": self.tokens["refresh_token"]},
)
token_response = yield token_request
if token_response.status_code == 401:
raise Exception("Failed to refresh. Log in again.")
token_response.read()
new_tokens = token_response.json()
self.tokens.update(new_tokens)
# Retry the original request, using the new access token.
request.headers["Authorization"] = f"Bearer {self.tokens['access_token']}"
yield request
def login(client):
username = input("Username: ")
password = getpass("Password: ")
tokens = client.post("/login", auth=(username, password)).json()
client.auth = RefreshFlow(tokens, f"{client.base_url}/refresh")
return client
if __name__ == "__main__":
import time
client = httpx.Client(base_url="http://localhost:8000")
login(client)
while True:
print(client.get("/data"))
time.sleep(2)