Skip to content

Commit

Permalink
Cleanup; retry; refactor (#150)
Browse files Browse the repository at this point in the history
* Deleted old files, no longer used.

* Adjusted the exports from __init__.py.

* Use tenacity for retries, and always return response object from HTTP requests, rather than the payload of the response object.

* Add decorators for raising exceptions on 'failed' http requests, and for unpacking json content.

* Do not set `Content-Length` header in `_blob_client.py`.

* Bumped httpx, and added tenacity.

* Bump minimum Python version  to 3.8

* Remove Python 3.7 from test matrix.

---------

Co-authored-by: Raymond Wiker <[email protected]>
  • Loading branch information
rwiker and rwiker authored Sep 29, 2023
1 parent 2a7abbd commit ff8d3a2
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 1,521 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9"]
python-version: ["3.8", "3.9"]
permissions:
contents: read
id-token: write
Expand Down
3 changes: 2 additions & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ msal-extensions>=1.0.0
PyYAML>=5.4
setuptools>=49.2.1
pyjwt>=2.4.0
httpx>=0.24.1
httpx>=0.25.0
tenacity>=8.2.3
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ def parse_requirements(fname):
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Natural Language :: English",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
],
use_scm_version={"write_to": "src/sumo/wrapper/version.py"},
author="Equinor ASA",
install_requires=REQUIREMENTS,
python_requires=">=3.6",
python_requires=">=3.8",
packages=find_packages("src"),
package_dir={"": "src"},
include_package_data=True,
Expand Down
3 changes: 1 addition & 2 deletions src/sumo/wrapper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from ._call_sumo_api import CallSumoApi
from .sumo_client import SumoClient

__all__ = ["CallSumoApi", "SumoClient"]
__all__ = ["SumoClient"]
224 changes: 0 additions & 224 deletions src/sumo/wrapper/_auth.py

This file was deleted.

28 changes: 8 additions & 20 deletions src/sumo/wrapper/_blob_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import httpx

from ._request_error import raise_request_error_exception
from .decorators import raise_for_status, http_retry


class BlobClient:
"""Upload blobs to blob store using pre-authorized URLs"""

@raise_for_status
@http_retry
def upload_blob(self, blob: bytes, url: str):
"""Upload a blob.
Expand All @@ -16,20 +18,15 @@ def upload_blob(self, blob: bytes, url: str):

headers = {
"Content-Type": "application/octet-stream",
"Content-Length": str(len(blob)),
"x-ms-blob-type": "BlockBlob",
}

try:
response = httpx.put(url, data=blob, headers=headers)
except httpx.ProxyError as err:
raise_request_error_exception(503, err)

if response.is_error:
raise_request_error_exception(response.status_code, response.text)
response = httpx.put(url, content=blob, headers=headers)

return response

@raise_for_status
@http_retry
async def upload_blob_async(self, blob: bytes, url: str):
"""Upload a blob async.
Expand All @@ -40,19 +37,10 @@ async def upload_blob_async(self, blob: bytes, url: str):

headers = {
"Content-Type": "application/octet-stream",
"Content-Length": str(len(blob)),
"x-ms-blob-type": "BlockBlob",
}

try:
async with httpx.AsyncClient() as client:
response = await client.put(
url=url, data=blob, headers=headers
)
except httpx.ProxyError as err:
raise_request_error_exception(503, err)

if response.is_error:
raise_request_error_exception(response.status_code, response.text)
async with httpx.AsyncClient() as client:
response = await client.put(url=url, content=blob, headers=headers)

return response
Loading

0 comments on commit ff8d3a2

Please sign in to comment.