Skip to content

Commit 2325f29

Browse files
author
childish-sambino
authored
chore: refactor base initialization (#686)
Provides for better type checking.
1 parent eb44ff6 commit 2325f29

File tree

429 files changed

+544
-6698
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

429 files changed

+544
-6698
lines changed

tests/unit/base/test_version.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66

77
class TestPage(Page):
8-
def __init__(self, version, response, *_args, **_kwargs):
9-
super(TestPage, self).__init__(version, response)
10-
118
def get_instance(self, payload):
129
return payload
1310

@@ -65,7 +62,7 @@ def setUp(self):
6562
self.response = self.version.page(
6663
method="GET", uri="/Accounts/AC123/Messages.json"
6764
)
68-
self.page = TestPage(self.version, self.response)
65+
self.page = TestPage(self.version, self.response, {})
6966

7067
def test_stream(self):
7168
messages = list(self.version.stream(self.page))

tests/unit/http/test_http_client.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# -*- coding: utf-8 -*-
2-
import unittest
32
import os
3+
import unittest
44
from collections import OrderedDict
55

6-
from mock import patch, Mock
6+
from mock import Mock, patch
77
from requests import Session
88

9-
from twilio.base.version import Version
109
from twilio.base.exceptions import TwilioRestException
10+
from twilio.base.version import Version
1111
from twilio.http.http_client import TwilioHttpClient
1212
from twilio.http.response import Response
1313

@@ -269,6 +269,5 @@ def test_session_not_preserved(self):
269269

270270
class MyVersion(Version):
271271
def __init__(self, domain):
272-
super(MyVersion, self).__init__(domain)
273-
self.version = "v1"
272+
super().__init__(domain, "v1")
274273
self._credentials = None

twilio/base/client_base.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import os
22
import platform
3+
from typing import Dict, Optional, Tuple
4+
from urllib.parse import urlparse, urlunparse
5+
36
from twilio import __version__
47
from twilio.base.exceptions import TwilioException
8+
from twilio.http import HttpClient
59
from twilio.http.http_client import TwilioHttpClient
6-
from urllib.parse import (
7-
urlparse,
8-
urlunparse,
9-
)
10+
from twilio.http.response import Response
1011

1112

1213
class ClientBase(object):
@@ -34,18 +35,13 @@ def __init__(
3435
:param dict environment: Environment to look for auth details, defaults to os.environ
3536
:param str edge: Twilio Edge to make requests to, defaults to None
3637
:param list[str] user_agent_extensions: Additions to the user agent string
37-
38-
:returns: Twilio Client
39-
:rtype: twilio.rest.Client
4038
"""
4139
environment = environment or os.environ
4240

4341
self.username = username or environment.get("TWILIO_ACCOUNT_SID")
4442
""" :type : str """
4543
self.password = password or environment.get("TWILIO_AUTH_TOKEN")
4644
""" :type : str """
47-
self.account_sid = account_sid or self.username
48-
""" :type : str """
4945
self.edge = edge or environment.get("TWILIO_EDGE")
5046
""" :type : str """
5147
self.region = region or environment.get("TWILIO_REGION")
@@ -56,9 +52,11 @@ def __init__(
5652
if not self.username or not self.password:
5753
raise TwilioException("Credentials are required to create a TwilioClient")
5854

55+
self.account_sid = account_sid or self.username
56+
""" :type : str """
5957
self.auth = (self.username, self.password)
6058
""" :type : tuple(str, str) """
61-
self.http_client = http_client or TwilioHttpClient()
59+
self.http_client: HttpClient = http_client or TwilioHttpClient()
6260
""" :type : HttpClient """
6361

6462
def request(
@@ -71,7 +69,7 @@ def request(
7169
auth=None,
7270
timeout=None,
7371
allow_redirects=False,
74-
):
72+
) -> Response:
7573
"""
7674
Makes a request to the Twilio API using the configured http client
7775
Authentication information is automatically added if none is provided
@@ -86,7 +84,6 @@ def request(
8684
:param bool allow_redirects: Should the client follow redirects
8785
8886
:returns: Response from the Twilio API
89-
:rtype: twilio.http.response.Response
9087
"""
9188
auth = self.get_auth(auth)
9289
headers = self.get_headers(method, headers)
@@ -113,7 +110,7 @@ async def request_async(
113110
auth=None,
114111
timeout=None,
115112
allow_redirects=False,
116-
):
113+
) -> Response:
117114
"""
118115
Asynchronously makes a request to the Twilio API using the configured http client
119116
The configured http client must be an asynchronous http client
@@ -129,7 +126,6 @@ async def request_async(
129126
:param bool allow_redirects: Should the client follow redirects
130127
131128
:returns: Response from the Twilio API
132-
:rtype: twilio.http.response.Response
133129
"""
134130
if not self.http_client.is_async:
135131
raise RuntimeError(
@@ -151,22 +147,22 @@ async def request_async(
151147
allow_redirects=allow_redirects,
152148
)
153149

154-
def get_auth(self, auth):
150+
def get_auth(self, auth: Optional[Tuple[str, str]]) -> Tuple[str, str]:
155151
"""
156152
Get the request authentication object
157-
:param tuple(str, str) auth: Authentication (username, password)
153+
:param auth: Authentication (username, password)
158154
:returns: The authentication object
159-
:rtype: tuple(str, str)
160155
"""
161156
return auth or self.auth
162157

163-
def get_headers(self, method, headers):
158+
def get_headers(
159+
self, method: str, headers: Optional[Dict[str, str]]
160+
) -> Dict[str, str]:
164161
"""
165162
Get the request headers including user-agent, extensions, encoding, content-type, MIME type
166-
:param str method: HTTP method
167-
:param dict[str, str] headers: HTTP headers
163+
:param method: HTTP method
164+
:param headers: HTTP headers
168165
:returns: HTTP headers
169-
:rtype: dict[str, str]
170166
"""
171167
headers = headers or {}
172168

@@ -195,15 +191,14 @@ def get_headers(self, method, headers):
195191

196192
return headers
197193

198-
def get_hostname(self, uri):
194+
def get_hostname(self, uri: str) -> str:
199195
"""
200196
Determines the proper hostname given edge and region preferences
201197
via client configuration or uri.
202198
203-
:param str uri: Fully qualified url
199+
:param uri: Fully qualified url
204200
205201
:returns: The final uri used to make the request
206-
:rtype: str
207202
"""
208203
if not self.edge and not self.region:
209204
return uri
@@ -228,13 +223,12 @@ def get_hostname(self, uri):
228223
parsed_url = parsed_url._replace(
229224
netloc=".".join([part for part in [prefix, edge, region, suffix] if part])
230225
)
231-
return urlunparse(parsed_url)
226+
return str(urlunparse(parsed_url))
232227

233-
def __repr__(self):
228+
def __repr__(self) -> str:
234229
"""
235230
Provide a friendly representation
236231
237232
:returns: Machine friendly representation
238-
:rtype: str
239233
"""
240234
return "<Twilio {}>".format(self.account_sid)

twilio/base/domain.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
from twilio.rest import Client
2+
3+
14
class Domain(object):
25
"""
36
This represents at Twilio API subdomain.
47
58
Like, `api.twilio.com` or `lookups.twilio.com'.
69
"""
710

8-
def __init__(self, twilio):
9-
"""
10-
:param Twilio twilio:
11-
:return:
12-
"""
11+
def __init__(self, twilio: Client, base_url: str):
1312
self.twilio = twilio
14-
self.base_url = None
13+
self.base_url = base_url
1514

16-
def absolute_url(self, uri):
15+
def absolute_url(self, uri: str) -> str:
1716
"""
1817
Converts a relative `uri` to an absolute url.
1918
:param string uri: The relative uri to make absolute.

twilio/base/page.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class Page(object):
2525
"uri",
2626
}
2727

28-
def __init__(self, version, response):
28+
def __init__(self, version, response, solution):
2929
payload = self.process_response(response)
3030

3131
self._version = version
3232
self._payload = payload
33-
self._solution = {}
33+
self._solution = solution
3434
self._records = iter(self.load_page(payload))
3535

3636
def __iter__(self):

twilio/base/version.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22

33
from twilio.base import values
4+
from twilio.base.domain import Domain
45
from twilio.base.exceptions import TwilioRestException
56

67

@@ -9,13 +10,9 @@ class Version(object):
910
Represents an API version.
1011
"""
1112

12-
def __init__(self, domain):
13-
"""
14-
:param Domain domain:
15-
:return:
16-
"""
13+
def __init__(self, domain: Domain, version: str):
1714
self.domain = domain
18-
self.version = None
15+
self.version = version
1916

2017
def absolute_url(self, uri):
2118
"""

twilio/rest/accounts/AccountsBase.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,22 @@ def __init__(self, twilio):
1919
Initialize the Accounts Domain
2020
2121
:returns: Domain for Accounts
22-
:rtype: twilio.rest.accounts.Accounts
2322
"""
24-
super().__init__(twilio)
25-
self.base_url = "https://accounts.twilio.com"
23+
super().__init__(twilio, "https://accounts.twilio.com")
2624
self._v1 = None
2725

2826
@property
29-
def v1(self):
27+
def v1(self) -> V1:
3028
"""
3129
:returns: Versions v1 of Accounts
32-
:rtype: twilio.rest.accounts.v1.V1
3330
"""
3431
if self._v1 is None:
3532
self._v1 = V1(self)
3633
return self._v1
3734

38-
def __repr__(self):
35+
def __repr__(self) -> str:
3936
"""
4037
Provide a friendly representation
4138
:returns: Machine friendly representation
42-
:rtype: str
4339
"""
4440
return "<Twilio.Accounts>"

twilio/rest/accounts/v1/__init__.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,25 @@ def __init__(self, domain: Domain):
2626
2727
:param domain: The Twilio.accounts domain
2828
"""
29-
super().__init__(domain)
30-
self.version = "v1"
29+
super().__init__(domain, "v1")
3130
self._auth_token_promotion = None
3231
self._credentials = None
3332
self._secondary_auth_token = None
3433

3534
@property
3635
def auth_token_promotion(self) -> AuthTokenPromotionList:
37-
"""
38-
:rtype: twilio.rest.accounts.v1.auth_token_promotion.AuthTokenPromotionList
39-
"""
4036
if self._auth_token_promotion is None:
4137
self._auth_token_promotion = AuthTokenPromotionList(self)
4238
return self._auth_token_promotion
4339

4440
@property
4541
def credentials(self) -> CredentialList:
46-
"""
47-
:rtype: twilio.rest.accounts.v1.credential.CredentialList
48-
"""
4942
if self._credentials is None:
5043
self._credentials = CredentialList(self)
5144
return self._credentials
5245

5346
@property
5447
def secondary_auth_token(self) -> SecondaryAuthTokenList:
55-
"""
56-
:rtype: twilio.rest.accounts.v1.secondary_auth_token.SecondaryAuthTokenList
57-
"""
5848
if self._secondary_auth_token is None:
5949
self._secondary_auth_token = SecondaryAuthTokenList(self)
6050
return self._secondary_auth_token
@@ -63,6 +53,5 @@ def __repr__(self) -> str:
6353
"""
6454
Provide a friendly representation
6555
:returns: Machine friendly representation
66-
:rtype: str
6756
"""
6857
return "<Twilio.Accounts.V1>"

twilio/rest/accounts/v1/credential/aws.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -296,21 +296,6 @@ def __repr__(self):
296296

297297

298298
class AwsPage(Page):
299-
def __init__(self, version, response, solution):
300-
"""
301-
Initialize the AwsPage
302-
303-
:param Version version: Version that contains the resource
304-
:param Response response: Response from the API
305-
306-
:returns: twilio.rest.accounts.v1.credential.aws.AwsPage
307-
:rtype: twilio.rest.accounts.v1.credential.aws.AwsPage
308-
"""
309-
super().__init__(version, response)
310-
311-
# Path solution
312-
self._solution = solution
313-
314299
def get_instance(self, payload):
315300
"""
316301
Build an instance of AwsInstance
@@ -322,12 +307,11 @@ def get_instance(self, payload):
322307
"""
323308
return AwsInstance(self._version, payload)
324309

325-
def __repr__(self):
310+
def __repr__(self) -> str:
326311
"""
327312
Provide a friendly representation
328313
329314
:returns: Machine friendly representation
330-
:rtype: str
331315
"""
332316
return "<Twilio.Accounts.V1.AwsPage>"
333317

0 commit comments

Comments
 (0)