Skip to content

Commit

Permalink
[SMS] service plan id version of the API (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
650elx authored Dec 18, 2024
1 parent 08b839d commit 8210f79
Show file tree
Hide file tree
Showing 54 changed files with 558 additions and 197 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ env:
PROJECT_ID: ${{ secrets.PROJECT_ID }}
NUMBERS_ORIGIN: ${{ secrets.NUMBERS_ORIGIN }}
SMS_ORIGIN: ${{ secrets.SMS_ORIGIN }}
SERVICE_PLAN_ID: ${{ secrets.SERVICE_PLAN_ID }}
SMS_API_TOKEN: ${{ secrets.SMS_API_TOKEN }}
CONVERSATION_ORIGIN: ${{ secrets.CONVERSATION_ORIGIN }}
AUTH_ORIGIN: ${{ secrets.AUTH_ORIGIN }}
DISABLE_SSL: ${{ secrets.DISABLE_SSL }}
Expand Down
8 changes: 6 additions & 2 deletions sinch/core/clients/sinch_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def __init__(
logger_name: str = None,
logger: Logger = None,
application_key: str = None,
application_secret: str = None
application_secret: str = None,
service_plan_id: str = None,
sms_api_token: str = None
):
self.configuration = Configuration(
key_id=key_id,
Expand All @@ -36,7 +38,9 @@ def __init__(
transport=HTTPXTransport(self),
token_manager=TokenManagerAsync(self),
application_secret=application_secret,
application_key=application_key
application_key=application_key,
service_plan_id=service_plan_id,
sms_api_token=sms_api_token
)

self.authentication = AuthenticationAsync(self)
Expand Down
4 changes: 3 additions & 1 deletion sinch/core/clients/sinch_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def __init__(
logger_name: str = None,
logger: Logger = None,
application_key: str = None,
application_secret: str = None
application_secret: str = None,
service_plan_id: str = None,
sms_api_token: str = None
):
pass

Expand Down
70 changes: 55 additions & 15 deletions sinch/core/clients/sinch_client_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from sinch.core.ports.http_transport import HTTPTransport
from sinch.core.token_manager import TokenManager, TokenManagerAsync
from sinch.core.enums import HTTPAuthentication


class Configuration:
Expand All @@ -22,14 +23,18 @@ def __init__(
disable_https=False,
connection_timeout=10,
application_key: str = None,
application_secret: str = None
application_secret: str = None,
service_plan_id: str = None,
sms_api_token: str = None
):
self.key_id = key_id
self.key_secret = key_secret
self.project_id = project_id
self.application_key = application_key
self.application_secret = application_secret
self.connection_timeout = connection_timeout
self.sms_api_token = sms_api_token
self.service_plan_id = service_plan_id
self.auth_origin = "auth.sinch.com"
self.numbers_origin = "numbers.api.sinch.com"
self.verification_origin = "verification.api.sinch.com"
Expand All @@ -39,7 +44,10 @@ def __init__(
self._conversation_region = "eu"
self._conversation_domain = ".conversation.api.sinch.com"
self._sms_region = "us"
self._sms_region_with_service_plan_id = "us"
self._sms_domain = "zt.{}.sms.api.sinch.com"
self._sms_domain_with_service_plan_id = "{}.sms.api.sinch.com"
self._sms_authentication = HTTPAuthentication.OAUTH.value
self._templates_region = "eu"
self._templates_domain = ".template.api.sinch.com"
self.token_manager = token_manager
Expand All @@ -48,6 +56,7 @@ def __init__(

self._set_conversation_origin()
self._set_sms_origin()
self._set_sms_origin_with_service_plan_id()
self._set_templates_origin()
self._set_voice_origin()

Expand All @@ -58,23 +67,35 @@ def __init__(
else:
self.logger = logging.getLogger("Sinch")

def _set_voice_origin(self):
if not self._voice_region:
self.voice_origin = self._voice_domain.format("calling")
else:
self.voice_origin = self._voice_domain.format("calling-" + self._voice_region)
def _set_sms_origin_with_service_plan_id(self):
self.sms_origin_with_service_plan_id = self._sms_domain_with_service_plan_id.format(
self._sms_region_with_service_plan_id
)

def _set_voice_region(self, region):
self._voice_region = region
self._set_voice_origin()
def _set_sms_region_with_service_plan_id(self, region):
self._sms_region_with_service_plan_id = region
self._set_sms_origin_with_service_plan_id()

def _get_voice_region(self):
return self._voice_region
def _get_sms_region_with_service_plan_id(self):
return self._sms_region_with_service_plan_id

voice_region = property(
_get_voice_region,
_set_voice_region,
doc="Voice Region"
sms_region_with_service_plan_id = property(
_get_sms_region_with_service_plan_id,
_set_sms_region_with_service_plan_id,
doc="SMS Region for service plan id version of the SMS API"
)

def _set_sms_domain_with_service_plan_id(self, domain):
self._sms_domain_with_service_plan_id = domain
self._set_sms_origin_with_service_plan_id()

def _get_sms_domain_with_service_plan_id(self):
return self._sms_domain_with_service_plan_id

sms_domain_with_service_plan_id = property(
_get_sms_domain_with_service_plan_id,
_set_sms_domain_with_service_plan_id,
doc="SMS Domain for service plan id version of the SMS API"
)

def _set_sms_origin(self):
Expand Down Expand Up @@ -163,3 +184,22 @@ def _get_templates_domain(self):
_set_templates_domain,
doc="Conversation API Templates Domain"
)

def _set_voice_origin(self):
if not self._voice_region:
self.voice_origin = self._voice_domain.format("calling")
else:
self.voice_origin = self._voice_domain.format("calling-" + self._voice_region)

def _set_voice_region(self, region):
self._voice_region = region
self._set_voice_origin()

def _get_voice_region(self):
return self._voice_region

voice_region = property(
_get_voice_region,
_set_voice_region,
doc="Voice Region"
)
8 changes: 6 additions & 2 deletions sinch/core/clients/sinch_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def __init__(
logger_name: str = None,
logger: Logger = None,
application_key: str = None,
application_secret: str = None
application_secret: str = None,
service_plan_id: str = None,
sms_api_token: str = None
):
self.configuration = Configuration(
key_id=key_id,
Expand All @@ -36,7 +38,9 @@ def __init__(
transport=HTTPTransportRequests(self),
token_manager=TokenManager(self),
application_key=application_key,
application_secret=application_secret
application_secret=application_secret,
service_plan_id=service_plan_id,
sms_api_token=sms_api_token
)

self.authentication = Authentication(self)
Expand Down
1 change: 1 addition & 0 deletions sinch/core/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ class HTTPAuthentication(Enum):
BASIC = "BASIC"
OAUTH = "OAUTH"
SIGNED = "SIGNED"
SMS_TOKEN = "SMS_TOKEN"
14 changes: 14 additions & 0 deletions sinch/core/ports/http_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ def authenticate(self, endpoint, request_data):
endpoint.get_url_without_origin(self.sinch)
)
request_data.headers = signature.get_http_headers_with_signature()
elif endpoint.HTTP_AUTHENTICATION == HTTPAuthentication.SMS_TOKEN.value:
if not self.sinch.configuration.sms_api_token or not self.sinch.configuration.service_plan_id:
raise ValidationException(
message=(
"sms_api_token and service_plan_id are required by this API. "
"Those credentials can be obtained from Sinch portal."
),
is_from_server=False,
response=None
)
request_data.headers.update({
"Authorization": f"Bearer {self.sinch.configuration.sms_api_token}",
"Content-Type": "application/json"
})

return request_data

Expand Down
Loading

0 comments on commit 8210f79

Please sign in to comment.