Skip to content

Commit 52d3561

Browse files
authored
Async support (#139)
* add async version of request methods * rename async methods * remove requests dependency for httpx * fix * get correct length of json * formatting * get follow redirects * sort imports * formatting * set black line-length to match flake8 conf * fix github actions black config * formatting
1 parent c5d08b5 commit 52d3561

8 files changed

Lines changed: 313 additions & 56 deletions

File tree

.github/workflows/linting.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@ jobs:
1818
python-version: ${{ matrix.python-version }}
1919
- uses: psf/black@stable
2020
with:
21-
options: "--check --verbose --line-length 80"
21+
options: "--check --verbose --line-length 79"
2222
src: "./src/sumo/wrapper"
2323
flake8:
2424
runs-on: ubuntu-latest
2525
strategy:
2626
matrix:
2727
python-version: ["3.8"]
2828
steps:
29-
- uses: actions/checkout@v3
30-
- name: Set up Python ${{ matrix.python-version }}
31-
uses: actions/setup-python@v3
32-
with:
33-
python-version: ${{ matrix.python-version }}
34-
- name: Install dependencies
35-
run: |
36-
python -m pip install --upgrade pip
37-
pip install flake8
38-
- name: Analysing the code with flake8
39-
run: |
40-
flake8 src/sumo/wrapper --config .flake8
29+
- uses: actions/checkout@v3
30+
- name: Set up Python ${{ matrix.python-version }}
31+
uses: actions/setup-python@v3
32+
with:
33+
python-version: ${{ matrix.python-version }}
34+
- name: Install dependencies
35+
run: |
36+
python -m pip install --upgrade pip
37+
pip install flake8
38+
- name: Analysing the code with flake8
39+
run: |
40+
flake8 src/sumo/wrapper --config .flake8

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ build
1111
/src/testing.py
1212
/docs/_build
1313
/docs/_static
14+
/src/sumo/wrapper/version.py

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ requires = [
77
]
88

99
[tool.black]
10-
line-length = 80
10+
line-length = 79

requirements/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
msal>=1.18.0
22
msal-extensions>=1.0.0
3-
requests>=2.24.0
43
PyYAML>=5.4
54
setuptools>=49.2.1
65
pyjwt>=2.4.0
6+
httpx>=0.24.1

src/sumo/wrapper/_blob_client.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import requests
1+
import httpx
2+
23
from ._request_error import raise_request_error_exception
34

45

@@ -20,11 +21,38 @@ def upload_blob(self, blob: bytes, url: str):
2021
}
2122

2223
try:
23-
response = requests.put(url, data=blob, headers=headers)
24-
except requests.exceptions.ProxyError as err:
24+
response = httpx.put(url, data=blob, headers=headers)
25+
except httpx.ProxyError as err:
26+
raise_request_error_exception(503, err)
27+
28+
if response.is_error:
29+
raise_request_error_exception(response.status_code, response.text)
30+
31+
return response
32+
33+
async def upload_blob_async(self, blob: bytes, url: str):
34+
"""Upload a blob async.
35+
36+
Parameters:
37+
blob: byte string to upload
38+
url: pre-authorized URL to blob store
39+
"""
40+
41+
headers = {
42+
"Content-Type": "application/octet-stream",
43+
"Content-Length": str(len(blob)),
44+
"x-ms-blob-type": "BlockBlob",
45+
}
46+
47+
try:
48+
async with httpx.AsyncClient() as client:
49+
response = await client.put(
50+
url=url, data=blob, headers=headers
51+
)
52+
except httpx.ProxyError as err:
2553
raise_request_error_exception(503, err)
2654

27-
if not response.ok:
55+
if response.is_error:
2856
raise_request_error_exception(response.status_code, response.text)
2957

3058
return response

src/sumo/wrapper/_call_azure_api.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import requests
1+
import httpx
22
import logging
33

44
from ._auth import Auth
@@ -154,9 +154,9 @@ def get_json(self, url, bearer=None):
154154

155155
headers = {"Content-Type": "application/json", "Authorization": bearer}
156156

157-
response = requests.get(url, headers=headers)
157+
response = httpx.get(url, headers=headers)
158158

159-
if not response.ok:
159+
if response.is_error:
160160
_raise_request_error_exception(response.status_code, response.text)
161161

162162
return response.json()
@@ -182,9 +182,9 @@ def get_image(self, url, bearer=None):
182182

183183
headers = {"Content-Type": "html/text", "Authorization": bearer}
184184

185-
response = requests.get(url, headers=headers, stream=True)
185+
response = httpx.get(url, headers=headers)
186186

187-
if not response.ok:
187+
if response.is_error:
188188
_raise_request_error_exception(response.status_code, response.text)
189189

190190
return None
@@ -210,9 +210,9 @@ def get_content(self, url, bearer=None):
210210

211211
headers = {"Content-Type": "application/json", "Authorization": bearer}
212212

213-
response = requests.get(url, headers=headers)
213+
response = httpx.get(url, headers=headers)
214214

215-
if not response.ok:
215+
if response.is_error:
216216
_raise_request_error_exception(response.status_code, response.text)
217217

218218
return response.content
@@ -249,11 +249,11 @@ def post(self, url, blob=None, json=None, bearer=None):
249249
}
250250

251251
try:
252-
response = requests.post(url, data=blob, json=json, headers=headers)
253-
except requests.exceptions.ProxyError as err:
252+
response = httpx.post(url, data=blob, json=json, headers=headers)
253+
except httpx.ProxyError as err:
254254
_raise_request_error_exception(503, err)
255255

256-
if not response.ok:
256+
if response.is_error:
257257
_raise_request_error_exception(response.status_code, response.text)
258258

259259
return response
@@ -293,11 +293,11 @@ def put(self, url, blob=None, json=None, bearer=None):
293293
headers["Authorization"] = bearer
294294

295295
try:
296-
response = requests.put(url, data=blob, json=json, headers=headers)
297-
except requests.exceptions.ProxyError as err:
296+
response = httpx.put(url, data=blob, json=json, headers=headers)
297+
except httpx.ProxyError as err:
298298
_raise_request_error_exception(503, err)
299299

300-
if not response.ok:
300+
if response.is_error:
301301
_raise_request_error_exception(response.status_code, response.text)
302302

303303
return response
@@ -321,9 +321,9 @@ def delete_object(self, url, bearer=None):
321321
"Authorization": bearer,
322322
}
323323

324-
response = requests.delete(url, headers=headers)
324+
response = httpx.delete(url, headers=headers)
325325

326-
if not response.ok:
326+
if response.is_error:
327327
_raise_request_error_exception(response.status_code, response.text)
328328

329329
return response.json()

src/sumo/wrapper/_call_sumo_api.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def __init__(
3030
self.base_url = f"https://main-sumo-{env}.radix.equinor.com/api/v1"
3131

3232
resource_id = (
33-
resource_id if resource_id else APP_REGISTRATION[env]["RESOURCE_ID"]
33+
resource_id
34+
if resource_id
35+
else APP_REGISTRATION[env]["RESOURCE_ID"]
3436
)
3537
client_id = (
3638
client_id if client_id else APP_REGISTRATION[env]["CLIENT_ID"]
@@ -224,7 +226,9 @@ def save_child_level_json(self, parent_id, json, bearer=None):
224226
Return
225227
string: The object id of the newly created object, or error message.
226228
"""
227-
return self._post_objects(object_id=parent_id, json=json, bearer=bearer)
229+
return self._post_objects(
230+
object_id=parent_id, json=json, bearer=bearer
231+
)
228232

229233
def update_child_level_json(
230234
self, json, object_id=None, url=None, bearer=None

0 commit comments

Comments
 (0)