Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebdc94a

Browse files
authored
Merge branch 'master' into auth_api_add_organization_param_to_the_change_password_method
Signed-off-by: Adam Mcgrath <[email protected]>
2 parents 7fbb7b3 + 7f5fe75 commit ebdc94a

File tree

7 files changed

+345
-296
lines changed

7 files changed

+345
-296
lines changed

auth0/rest.py

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import annotations
22

33
import base64
4-
import json
54
import platform
65
import sys
6+
from json import dumps, loads
77
from random import randint
88
from time import sleep
99
from typing import TYPE_CHECKING, Any, Mapping
@@ -95,7 +95,7 @@ def __init__(
9595
py_version = platform.python_version()
9696
version = sys.modules["auth0"].__version__
9797

98-
auth0_client = json.dumps(
98+
auth0_client = dumps(
9999
{
100100
"name": "auth0-python",
101101
"version": version,
@@ -136,32 +136,41 @@ def MAX_REQUEST_RETRY_DELAY(self) -> int:
136136
def MIN_REQUEST_RETRY_DELAY(self) -> int:
137137
return 100
138138

139-
def get(
139+
def _request(
140140
self,
141+
method: str,
141142
url: str,
142143
params: dict[str, Any] | None = None,
144+
data: RequestData | None = None,
145+
json: RequestData | None = None,
143146
headers: dict[str, str] | None = None,
147+
files: dict[str, Any] | None = None,
144148
) -> Any:
145-
request_headers = self.base_headers.copy()
146-
request_headers.update(headers or {})
147-
148149
# Track the API request attempt number
149150
attempt = 0
150151

151152
# Reset the metrics tracker
152153
self._metrics = {"retries": 0, "backoff": []}
153154

155+
kwargs = {
156+
k: v
157+
for k, v in {
158+
"params": params,
159+
"json": json,
160+
"data": data,
161+
"headers": headers,
162+
"files": files,
163+
"timeout": self.options.timeout,
164+
}.items()
165+
if v is not None
166+
}
167+
154168
while True:
155169
# Increment attempt number
156170
attempt += 1
157171

158172
# Issue the request
159-
response = requests.get(
160-
url,
161-
params=params,
162-
headers=request_headers,
163-
timeout=self.options.timeout,
164-
)
173+
response = requests.request(method, url, **kwargs)
165174

166175
# If the response did not have a 429 header, or the attempt number is greater than the configured retries, break
167176
if response.status_code != 429 or attempt > self._retries:
@@ -177,6 +186,16 @@ def get(
177186
# Return the final Response
178187
return self._process_response(response)
179188

189+
def get(
190+
self,
191+
url: str,
192+
params: dict[str, Any] | None = None,
193+
headers: dict[str, str] | None = None,
194+
) -> Any:
195+
request_headers = self.base_headers.copy()
196+
request_headers.update(headers or {})
197+
return self._request("GET", url, params=params, headers=request_headers)
198+
180199
def post(
181200
self,
182201
url: str,
@@ -185,11 +204,7 @@ def post(
185204
) -> Any:
186205
request_headers = self.base_headers.copy()
187206
request_headers.update(headers or {})
188-
189-
response = requests.post(
190-
url, json=data, headers=request_headers, timeout=self.options.timeout
191-
)
192-
return self._process_response(response)
207+
return self._request("POST", url, json=data, headers=request_headers)
193208

194209
def file_post(
195210
self,
@@ -199,27 +214,15 @@ def file_post(
199214
) -> Any:
200215
headers = self.base_headers.copy()
201216
headers.pop("Content-Type", None)
202-
203-
response = requests.post(
204-
url, data=data, files=files, headers=headers, timeout=self.options.timeout
205-
)
206-
return self._process_response(response)
217+
return self._request("POST", url, data=data, files=files, headers=headers)
207218

208219
def patch(self, url: str, data: RequestData | None = None) -> Any:
209220
headers = self.base_headers.copy()
210-
211-
response = requests.patch(
212-
url, json=data, headers=headers, timeout=self.options.timeout
213-
)
214-
return self._process_response(response)
221+
return self._request("PATCH", url, json=data, headers=headers)
215222

216223
def put(self, url: str, data: RequestData | None = None) -> Any:
217224
headers = self.base_headers.copy()
218-
219-
response = requests.put(
220-
url, json=data, headers=headers, timeout=self.options.timeout
221-
)
222-
return self._process_response(response)
225+
return self._request("PUT", url, json=data, headers=headers)
223226

224227
def delete(
225228
self,
@@ -228,15 +231,7 @@ def delete(
228231
data: RequestData | None = None,
229232
) -> Any:
230233
headers = self.base_headers.copy()
231-
232-
response = requests.delete(
233-
url,
234-
headers=headers,
235-
params=params or {},
236-
json=data,
237-
timeout=self.options.timeout,
238-
)
239-
return self._process_response(response)
234+
return self._request("DELETE", url, params=params, json=data, headers=headers)
240235

241236
def _calculate_wait(self, attempt: int) -> int:
242237
# Retry the request. Apply a exponential backoff for subsequent attempts, using this formula:
@@ -317,7 +312,7 @@ def _error_message(self):
317312

318313
class JsonResponse(Response):
319314
def __init__(self, response: requests.Response | RequestsResponse) -> None:
320-
content = json.loads(response.text)
315+
content = loads(response.text)
321316
super().__init__(response.status_code, content, response.headers)
322317

323318
def _error_code(self) -> str:

auth0/rest_async.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,43 +52,23 @@ def set_session(self, session: aiohttp.ClientSession) -> None:
5252
"""
5353
self._session = session
5454

55-
async def _request(self, *args: Any, **kwargs: Any) -> Any:
56-
kwargs["headers"] = kwargs.get("headers", self.base_headers)
57-
kwargs["timeout"] = self.timeout
58-
if self._session is not None:
59-
# Request with re-usable session
60-
async with self._session.request(*args, **kwargs) as response:
61-
return await self._process_response(response)
62-
else:
63-
# Request without re-usable session
64-
async with aiohttp.ClientSession() as session:
65-
async with session.request(*args, **kwargs) as response:
66-
return await self._process_response(response)
67-
68-
async def get(
69-
self,
70-
url: str,
71-
params: dict[str, Any] | None = None,
72-
headers: dict[str, str] | None = None,
55+
async def _request_with_session(
56+
self, session: aiohttp.ClientSession, *args: Any, **kwargs: Any
7357
) -> Any:
74-
request_headers = self.base_headers.copy()
75-
request_headers.update(headers or {})
7658
# Track the API request attempt number
7759
attempt = 0
7860

7961
# Reset the metrics tracker
8062
self._metrics = {"retries": 0, "backoff": []}
8163

82-
params = _clean_params(params)
8364
while True:
8465
# Increment attempt number
8566
attempt += 1
8667

8768
try:
88-
response = await self._request(
89-
"get", url, params=params, headers=request_headers
90-
)
91-
return response
69+
async with session.request(*args, **kwargs) as response:
70+
return await self._process_response(response)
71+
9272
except RateLimitError as e:
9373
# If the attempt number is greater than the configured retries, raise RateLimitError
9474
if attempt > self._retries:
@@ -101,6 +81,30 @@ async def get(
10181
# sleep() functions in seconds, so convert the milliseconds formula above accordingly
10282
await asyncio.sleep(wait / 1000)
10383

84+
async def _request(self, *args: Any, **kwargs: Any) -> Any:
85+
kwargs["headers"] = kwargs.get("headers", self.base_headers)
86+
kwargs["timeout"] = self.timeout
87+
if self._session is not None:
88+
# Request with re-usable session
89+
return self._request_with_session(self.session, *args, **kwargs)
90+
else:
91+
# Request without re-usable session
92+
async with aiohttp.ClientSession() as session:
93+
return self._request_with_session(session, *args, **kwargs)
94+
95+
async def get(
96+
self,
97+
url: str,
98+
params: dict[str, Any] | None = None,
99+
headers: dict[str, str] | None = None,
100+
) -> Any:
101+
request_headers = self.base_headers.copy()
102+
request_headers.update(headers or {})
103+
104+
return await self._request(
105+
"get", url, params=_clean_params(params), headers=request_headers
106+
)
107+
104108
async def post(
105109
self,
106110
url: str,
@@ -118,7 +122,7 @@ async def file_post(
118122
files: dict[str, Any],
119123
) -> Any:
120124
headers = self.base_headers.copy()
121-
headers.pop("Content-Type", None)
125+
headers.pop("Content-Type")
122126
return await self._request("post", url, data={**data, **files}, headers=headers)
123127

124128
async def patch(self, url: str, data: RequestData | None = None) -> Any:

0 commit comments

Comments
 (0)