Skip to content

Commit

Permalink
Merge branch 'main' into DEVEXP-142-static-annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
650elx committed Feb 5, 2024
2 parents feb2303 + 1771be4 commit 908daeb
Show file tree
Hide file tree
Showing 59 changed files with 1,351 additions and 210 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners
* @Dovchik @650elx @krogers0607 @asein-sinch @JPPortier
10 changes: 8 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ env:
APP_ID: ${{ secrets.APP_ID }}
EMPTY_PROJECT_ID: ${{ secrets.EMPTY_PROJECT_ID }}
TEMPLATES_ORIGIN: ${{ secrets.TEMPLATES_ORIGIN }}

APPLICATION_SECRET: ${{ secrets.APPLICATION_SECRET }}
APPLICATION_KEY: ${{ secrets.APPLICATION_KEY }}
VERIFICATION_ID: ${{ secrets.VERIFICATION_ID }}
VERIFICATION_ORIGIN: ${{ secrets.VERIFICATION_ORIGIN}}
VERIFICATION_REQUEST_SIGNATURE_TIMESTAMP: ${{ secrets.VERIFICATION_REQUEST_SIGNATURE_TIMESTAMP}}
VERIFICATION_REQUEST_WITH_EMPTY_BODY_SIGNATURE: ${{ secrets.VERIFICATION_REQUEST_WITH_EMPTY_BODY_SIGNATURE}}
VERIFICATION_REQUEST_SIGNATURE: ${{ secrets.VERIFICATION_REQUEST_SIGNATURE}}

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v3
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,7 @@ dmypy.json
cython_debug/

# PyCharm
.idea/
.idea/

# Poetry
poetry.lock
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://github.com/sinch/sinch-sdk-python/blob/main/LICENSE)


[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-380/)
[![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)](https://www.python.org/downloads/release/python-390/)
[![Python 3.10](https://img.shields.io/badge/python-3.10-blue.svg)](https://www.python.org/downloads/release/python-3100/)
[![Python 3.11](https://img.shields.io/badge/python-3.11-blue.svg)](https://www.python.org/downloads/release/python-3110/)
[![Python 3.12](https://img.shields.io/badge/python-3.12-blue.svg)](https://www.python.org/downloads/release/python-3120/)


</h1>

Expand Down Expand Up @@ -93,12 +94,9 @@ Sinch client provides access to the following Sinch product domains:
Usage example of the `numbers` domain:

```python
from sinch.domains.numbers.enums import NumberType

available_numbers = sinch_client.numbers.available.list(
region_code="US",
number_type=NumberType.LOCAL.value,
project_id="Shrubbery"
number_type="LOCAL"
)
```
Returned values are represented as Python `dataclasses`:
Expand Down Expand Up @@ -128,8 +126,7 @@ from sinch.domains.numbers.exceptions import NumbersException
try:
nums = sinch_client.numbers.available.list(
region_code="US",
number_type="LOCAL",
project_id="project"
number_type="LOCAL"
)
except NumbersException as err:
pass
Expand Down
8 changes: 6 additions & 2 deletions examples/flask_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)


@app.route("/create_app")
@app.route("/create_app", methods=['POST'])
def project():
conversation_api_app = sinch_client.conversation.app.create(
display_name="Shrubbery",
Expand All @@ -27,6 +27,10 @@ def project():
"token": "herring"
}
}
]
],
retention_policy={
"ttl_days": 20,
"retention_type": "MESSAGE_EXPIRE_POLICY"
}
)
return {"sinch_app_id": conversation_api_app.id}
27 changes: 23 additions & 4 deletions examples/logging_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,35 @@
logger = logging.getLogger("myapp.sinch")
logger.setLevel(logging.DEBUG)

sinch_log_file_handler = logging.FileHandler("/tmp/spam.log")
sinch_log_file_handler.setLevel(logging.DEBUG)
file_handler = logging.FileHandler("/tmp/test_python_logging.log")
file_handler.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
sinch_log_file_handler.setFormatter(formatter)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)

formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
file_handler.setFormatter(formatter)

logger.addHandler(file_handler)
logger.addHandler(console_handler)

sinch_client = Client(
key_id=os.getenv("KEY_ID"),
key_secret=os.getenv("KEY_SECRET"),
project_id=os.getenv("PROJECT_ID"),
logger=logger
)


def main():
available_numbers_response = sinch_client.numbers.available.list(
region_code="US",
number_type="LOCAL"
)
print(available_numbers_response)


if __name__ == "__main__":
main()
34 changes: 34 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[tool.poetry]
name = "sinch"
description = "Sinch SDK for Python programming language"
version = "0.3.1"
license = "Apache 2.0"
readme = "README.md"
authors = [
"Sinch Developer Experience Team <[email protected]>",
]
repository = "https://github.com/sinch/sinch-sdk-python"
documentation = "https://developers.sinch.com"
classifiers = [
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Communications :: Telephony",
"Intended Audience :: Developers"
]
keywords = ["sinch", "sdk"]

[tool.poetry.dependencies]
python = ">=3.9"
requests = "*"
aiohttp = "*"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
4 changes: 4 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ flake8
bandit
requests
aiohttp
flask
fastapi
httpx
pytest-asyncio
coverage
mypy
types-requests
coverage
35 changes: 0 additions & 35 deletions setup.cfg

This file was deleted.

4 changes: 0 additions & 4 deletions setup.py

This file was deleted.

1 change: 1 addition & 0 deletions sinch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Sinch Python SDK
To access Sinch resources, use the Sync or Async version of the Sinch Client.
"""
__version__ = "0.3.1"

from sinch.core.clients.sinch_client_sync import Client
from sinch.core.clients.sinch_client_async import ClientAsync
Expand Down
70 changes: 35 additions & 35 deletions sinch/core/adapters/asyncio_http_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,52 @@
from sinch.core.ports.http_transport import AsyncHTTPTransport, HttpRequest
from sinch.core.endpoint import HTTPEndpoint
from sinch.core.models.http_response import HTTPResponse
from sinch.core.models.base_model import SinchBaseModel


class HTTPTransportAioHTTP(AsyncHTTPTransport):
async def request(self, endpoint: HTTPEndpoint) -> SinchBaseModel:
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_with_auth: HttpRequest = await self.authenticate(endpoint, request_data)

if not self.http_session:
self.http_session = aiohttp.ClientSession()

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

if request_data_with_auth.auth:
auth_data = aiohttp.BasicAuth(request_data_with_auth.auth[0], request_data_with_auth.auth[1])
else:
auth_data = None

async with aiohttp.ClientSession() as session:
async with session.request(
method=request_data_with_auth.http_method,
headers=request_data_with_auth.headers,
url=request_data_with_auth.url,
data=request_data_with_auth.request_body,
auth=auth_data,
params=request_data_with_auth.query_params,
timeout=aiohttp.ClientTimeout(
total=self.sinch.configuration.connection_timeout
)
) as response:
response_body = {}
raw_response_body = await response.read()
if raw_response_body:
response_body = json.loads(raw_response_body)

self.sinch.configuration.logger.debug(
f"Async HTTP {response.status} response with headers: {response.headers}"
f"and body: {response_body} from URL: {request_data_with_auth.url}"
)
async with self.http_session.request(
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=aiohttp.ClientTimeout(
total=self.sinch.configuration.connection_timeout
)
) as response:

response_body = await response.read()
if response_body:
response_body = json.loads(response_body)

self.sinch.configuration.logger.debug(
f"Async HTTP {response.status} 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,
body=response_body,
headers=dict(response.headers)
)
return await self.handle_response(
endpoint=endpoint,
http_response=HTTPResponse(
status_code=response.status,
body=response_body,
headers=response.headers
)
)
14 changes: 7 additions & 7 deletions sinch/core/adapters/requests_http_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class HTTPTransportRequests(HTTPTransport):
def __init__(self, sinch: ClientBase):
super().__init__(sinch)
self.session = requests.Session()
self.http_session = requests.Session()

def request(self, endpoint: HTTPEndpoint) -> SinchBaseModel:
request_data: HttpRequest = self.prepare_request(endpoint)
Expand All @@ -23,12 +23,12 @@ def request(self, endpoint: HTTPEndpoint) -> SinchBaseModel:
f"to URL: {request_data_with_auth.url}"
)

response = self.session.request(
method=request_data_with_auth.http_method,
url=request_data_with_auth.url,
data=request_data_with_auth.request_body,
auth=request_data_with_auth.auth,
headers=request_data_with_auth.headers,
response = self.http_session.request(
method=request_data.http_method,
url=request_data.url,
data=request_data.request_body,
auth=request_data.auth,
headers=request_data.headers,
timeout=self.sinch.configuration.connection_timeout,
params=request_data_with_auth.query_params
)
Expand Down
24 changes: 12 additions & 12 deletions sinch/core/clients/sinch_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sinch.domains.numbers import NumbersAsync
from sinch.domains.conversation import ConversationAsync
from sinch.domains.sms import SMSAsync
from sinch.domains.verification import Verification as VerificationAsync


class ClientAsync(ClientBase):
Expand All @@ -18,29 +19,28 @@ class ClientAsync(ClientBase):
"""
def __init__(
self,
key_id: str,
key_secret: str,
project_id: str,
key_id,
key_secret,
project_id,
logger_name: Optional[str] = None,
logger: Optional[Logger] = None
logger: Optional[Logger] = None,
application_key: Optional[str] = None,
application_secret: Optional[str] = None
):
super().__init__(
key_id=key_id,
key_secret=key_secret,
project_id=project_id,
logger_name=logger_name,
logger=logger
)
self.configuration = Configuration(
key_id=key_id,
key_secret=key_secret,
project_id=project_id,
logger_name=logger_name,
logger=logger,
transport=HTTPTransportAioHTTP(self),
token_manager=TokenManagerAsync(self)
token_manager=TokenManagerAsync(self),
application_secret=application_secret,
application_key=application_key
)

self.authentication = AuthenticationAsync(self)
self.numbers = NumbersAsync(self)
self.conversation = ConversationAsync(self)
self.sms = SMSAsync(self)
self.verification = VerificationAsync(self)
Loading

0 comments on commit 908daeb

Please sign in to comment.