1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
from typing import Dict, Optional, Tuple
from twilio.http.response import Response
from twilio.rest import Client
class Domain(object):
"""
This represents at Twilio API subdomain.
Like, `api.twilio.com` or `lookups.twilio.com'.
"""
def __init__(self, twilio: Client, base_url: str):
self.twilio = twilio
self.base_url = base_url
def absolute_url(self, uri: str) -> str:
"""
Converts a relative `uri` to an absolute url.
:param string uri: The relative uri to make absolute.
:return: An absolute url (based off this domain)
"""
return "{}/{}".format(self.base_url.strip("/"), uri.strip("/"))
def request(
self,
method: str,
uri: str,
params: Optional[Dict[str, object]] = None,
data: Optional[Dict[str, object]] = None,
headers: Optional[Dict[str, str]] = None,
auth: Optional[Tuple[str, str]] = None,
timeout: Optional[float] = None,
allow_redirects: bool = False,
) -> Response:
"""
Makes an HTTP request to this domain.
:param method: The HTTP method.
:param uri: The HTTP uri.
:param params: Query parameters.
:param data: The request body.
:param headers: The HTTP headers.
:param auth: Basic auth tuple of (username, password)
:param timeout: The request timeout.
:param allow_redirects: True if the client should follow HTTP
redirects.
"""
url = self.absolute_url(uri)
return self.twilio.request(
method,
url,
params=params,
data=data,
headers=headers,
auth=auth,
timeout=timeout,
allow_redirects=allow_redirects,
)
async def request_async(
self,
method: str,
uri: str,
params: Optional[Dict[str, object]] = None,
data: Optional[Dict[str, object]] = None,
headers: Optional[Dict[str, str]] = None,
auth: Optional[Tuple[str, str]] = None,
timeout: Optional[float] = None,
allow_redirects: bool = False,
) -> Response:
"""
Makes an asynchronous HTTP request to this domain.
:param method: The HTTP method.
:param uri: The HTTP uri.
:param params: Query parameters.
:param data: The request body.
:param headers: The HTTP headers.
:param auth: Basic auth tuple of (username, password)
:param timeout: The request timeout.
:param allow_redirects: True if the client should follow HTTP
redirects.
"""
url = self.absolute_url(uri)
return await self.twilio.request_async(
method,
url,
params=params,
data=data,
headers=headers,
auth=auth,
timeout=timeout,
allow_redirects=allow_redirects,
)
|