Skip to main content

Sinch SDK for Python programming language

Project description

Sinch Logo

Python SDK

License

Python 3.8 Python 3.9 Python 3.10 Python 3.11

Welcome to Sinch's Python SDK.

Here you'll find documentation to start developing Python code using Sinch services.

To use this SDK you'll need a Sinch account and API keys. Please sign up at sinch.com

For more in depth version of the Sinch APIs, please refer to the official developer portal - developers.sinch.com

This SDK is currently available to selected developers for preview use only. It is being provided for the purpose of collecting feedback, and should not be used in production environments.


Installation

You can install this package to your virtual environment or to your global Python installation by cloning this repo and typing: pip install sinch

Getting started

Client initialization

To initialize communication with Sinch backed, credentials obtained from Sinch portal have to be provided to the main client class of this SDK. It's highly advised to not hardcode those credentials, but to fetch them from environment variables:

from sinch import Client

sinch_client = Client(
    key_id="key_id",
    key_secret="key_secret",
    project_id="some_project"
)
import os
from sinch import Client

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

Domains

Each product from Sinch portfolio is considered and coded as a separate domain. Domain contains data structures specific to that service, that can be also used by the developer.

Abstract directory structure of the domain:

├── domain_name
│   ├── __init__.py (public interface of the domain)
│   ├── endpoints (directory with endpoint definitions)
│   ├── models (direcotry with data models definitions)
│   ├── enums.py (enums that can be helpful for a developer; e.g. SinchNumberType)
│   ├── exceptions.py (exceptions spefific for a particular domain; e.g. NumbersException)
│

Sinch client provides access to the following Sinch product domains:

  • Numbers
  • Conversation API
  • SMS

Usage example of the numbers domain:

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"
)

Returned values are represented as Python dataclasses:

ListAvailableNumbersResponse(
    available_numbers=[
        Number(
            phone_number='+17862045855',
            region_code='US',
            type='LOCAL',
            capability=['SMS', 'VOICE'],
            setup_price={'currency_code': 'EUR', 'amount': '0.80'},
            monthly_price={'currency_code': 'EUR', 'amount': '0.80'}
            ...

Handling exceptions

Each domain throws a DomainException exception for an unsuccessful backed call.

Example for Numbers Domain:

from sinch.domains.numbers.exceptions import NumbersException

try:
    nums = sinch_client.numbers.available.list(
        region_code="US",
        number_type="LOCAL",
        project_id="project"
    )
except NumbersException as err:
    pass

For handling all possible exceptions thrown by this SDK use SinchException (superclass of all Sinch exceptions) form sinch.core.exceptions.

Logging

Logging configuration for this SDK utilizes following hierarchy:

  1. If no configuration was provided via logger_name or logger configurable, SDK will inherit configuration from the root logger with the Sinch prefix.
  2. If logger_name configurable was provided, SDK will use logger related to that name. For example: myapp.sinch will inherit configuration from the myapp logger.
  3. If logger (logger instance) configurable was provided, SDK will use that particular logger for all its logging operations.

If all logging returned by this SDK needs to be disabled, usage of NullHanlder provided by the standard logging module is advised.

Custom HTTP client implementation

By default, two HTTP implementations are provided:

  • Synchronous using requests HTTP library
  • Asynchronous using aiohttp HTTP library

For creating custom HTTP client code, use either Client or ClientAsync client and inject your transport during initialisation:

sinch_client = ClientAsync(
    key_id="Spanish",
    key_secret="Inquisition",
    project_id="some_project",
    transport=MyHTTPAsyncImplementation
)

Custom client has to obey types and methods described by HTTPTransport abstract base class:

class HTTPTransport(ABC):
    @abstractmethod
    def request(self, endpoint: HTTPEndpoint) -> HTTPResponse:
        pass

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sinch-0.2.0.tar.gz (44.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sinch-0.2.0-py3-none-any.whl (111.0 kB view details)

Uploaded Python 3

File details

Details for the file sinch-0.2.0.tar.gz.

File metadata

  • Download URL: sinch-0.2.0.tar.gz
  • Upload date:
  • Size: 44.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for sinch-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f24f1da27fb95ed472f3127b94b371eaffc91b376c1f4cc12279915bbe1c0ff6
MD5 90cdcbc1adbf79c523e518bce3ca6666
BLAKE2b-256 fa19df78a16623bd0700b081968f0d7211c53599df8ef8824147190a46306200

See more details on using hashes here.

File details

Details for the file sinch-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sinch-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 111.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for sinch-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 186b11a57e493a31b60a9db4ed6594e50092e237330b265566d100f08c230cf5
MD5 c434ccbf09981bb0a69aed66f3cc45ae
BLAKE2b-256 fb39d000b19d107e06bd36da21e3e80bac0d929fc198ab6702d97a6b9094c692

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page