Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVEXP-340/DEVEXP-471 - automated CI/CD release to PyPI and async library replacement #29

Merged
merged 11 commits into from
Oct 17, 2024
31 changes: 31 additions & 0 deletions .github/workflows/release-sdk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Release Python SDK

on:
release:
types: [published]

env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v2
- name: Install packaging tools
run: |
python -m pip install --upgrade pip
pip install twine
pip install poetry
- name: Build package
run: |
poetry build
- name: Verify package
run: |
twine check dist/*
- name: Release package
run: |
twine upload dist/*
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ keywords = ["sinch", "sdk"]
[tool.poetry.dependencies]
python = ">=3.9"
requests = "*"
aiohttp = "*"
httpx = "*"

[build-system]
requires = ["poetry-core"]
Expand Down
53 changes: 0 additions & 53 deletions sinch/core/adapters/asyncio_http_adapter.py

This file was deleted.

62 changes: 62 additions & 0 deletions sinch/core/adapters/httpx_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import httpx
from sinch.core.ports.http_transport import AsyncHTTPTransport, HttpRequest
from sinch.core.endpoint import HTTPEndpoint
from sinch.core.models.http_response import HTTPResponse


class HTTPXTransport(AsyncHTTPTransport):
def __init__(self, sinch):
super().__init__(sinch)
self.http_session = None

async def request(self, endpoint: HTTPEndpoint) -> HTTPResponse:
request_data: HttpRequest = self.prepare_request(endpoint)
request_data: HttpRequest = await self.authenticate(endpoint, request_data)

if not self.http_session:
self.http_session = httpx.AsyncClient()

self.sinch.configuration.logger.debug(
f"Async HTTP {request_data.http_method} call with headers:"
f" {request_data.headers} and body: {request_data.request_body} to URL: {request_data.url}"
)

if isinstance(request_data.request_body, str):
response = await self.http_session.request(
method=request_data.http_method,
headers=request_data.headers,
url=request_data.url,
content=request_data.request_body,
auth=request_data.auth,
params=request_data.query_params,
timeout=self.sinch.configuration.connection_timeout
)
else:
response = await self.http_session.request(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of code looks very similar to the lines 25-31. Maybe you can streamline this by using a single call and handling the content type inline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of code looks very similar to the lines 25-31. Maybe you can streamline this by using a single call and handling the content type inline

I've done it on purpose. Since this is a async context I prefer to keep it simple

method=request_data.http_method,
headers=request_data.headers,
url=request_data.url,
data=request_data.request_body,
auth=request_data.auth,
params=request_data.query_params,
timeout=self.sinch.configuration.connection_timeout,
)

response_body = self.deserialize_json_response(response)

self.sinch.configuration.logger.debug(
f"Async HTTP {response.status_code} response with headers: {response.headers}"
f"and body: {response_body} from URL: {request_data.url}"
)

return await self.handle_response(
endpoint=endpoint,
http_response=HTTPResponse(
status_code=response.status_code,
body=response_body,
headers=response.headers
)
)

async def close_session(self):
await self.http_session.aclose()
5 changes: 1 addition & 4 deletions sinch/core/adapters/requests_http_transport.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests
import json
from sinch.core.ports.http_transport import HTTPTransport, HttpRequest
from sinch.core.endpoint import HTTPEndpoint
from sinch.core.models.http_response import HTTPResponse
Expand Down Expand Up @@ -29,9 +28,7 @@ def request(self, endpoint: HTTPEndpoint) -> HTTPResponse:
params=request_data.query_params
)

response_body = response.content
if response_body:
response_body = json.loads(response_body)
response_body = self.deserialize_json_response(response)

self.sinch.configuration.logger.debug(
f"Sync HTTP {response.status_code} response with headers: {response.headers}"
Expand Down
4 changes: 2 additions & 2 deletions sinch/core/clients/sinch_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sinch.core.clients.sinch_client_base import SinchClientBase
from sinch.core.clients.sinch_client_configuration import Configuration
from sinch.core.token_manager import TokenManagerAsync
from sinch.core.adapters.asyncio_http_adapter import HTTPTransportAioHTTP
from sinch.core.adapters.httpx_adapter import HTTPXTransport
from sinch.domains.authentication import AuthenticationAsync
from sinch.domains.numbers import NumbersAsync
from sinch.domains.conversation import ConversationAsync
Expand Down Expand Up @@ -33,7 +33,7 @@ def __init__(
project_id=project_id,
logger_name=logger_name,
logger=logger,
transport=HTTPTransportAioHTTP(self),
transport=HTTPXTransport(self),
token_manager=TokenManagerAsync(self),
application_secret=application_secret,
application_key=application_key
Expand Down
21 changes: 20 additions & 1 deletion sinch/core/ports/http_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sinch.core.signature import Signature
from sinch.core.models.http_request import HttpRequest
from sinch.core.models.http_response import HTTPResponse
from sinch.core.exceptions import ValidationException
from sinch.core.exceptions import ValidationException, SinchException
from sinch.core.enums import HTTPAuthentication
from sinch.core.token_manager import TokenState
from sinch import __version__ as sdk_version
Expand Down Expand Up @@ -83,6 +83,25 @@ def prepare_request(self, endpoint: HTTPEndpoint) -> HttpRequest:
auth=()
)

@staticmethod
def deserialize_json_response(response):
if response.content:
try:
response_body = response.json()
except ValueError as err:
raise SinchException(
message=(
"Error while parsing json response.",
err.msg
),
is_from_server=True,
response=response
)
else:
response_body = {}

return response_body

def handle_response(self, endpoint: HTTPEndpoint, http_response: HTTPResponse):
if http_response.status_code == 401 and endpoint.HTTP_AUTHENTICATION == HTTPAuthentication.OAUTH.value:
self.sinch.configuration.token_manager.handle_invalid_token(http_response)
Expand Down
Loading