Skip to content

Commit

Permalink
chore: update user-agent string to standardize format (#597)
Browse files Browse the repository at this point in the history
* [DI-1565] update user agent string format to allow extension and its test cases
  • Loading branch information
claudiachua authored Apr 1, 2022
1 parent 5011a4b commit 128775b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
4 changes: 3 additions & 1 deletion tests/holodeck.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ def requests(self):

def add_standard_headers(self, request):
standard_headers = {
'User-Agent': 'twilio-python/{} (Python {})'.format(
'User-Agent': 'twilio-python/{} ({} {}) Python/{}'.format(
__version__,
platform.system(),
platform.machine(),
platform.python_version()),
'X-Twilio-Client': 'python-{}'.format(__version__),
'Accept': 'application/json',
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/rest/test_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import unittest
import platform

from twilio import __version__
from twilio.rest import (
Client,
TwilioClient,
Expand Down Expand Up @@ -97,3 +99,36 @@ def test_periods_in_query(self):
self.assertEqual(self.client.get_hostname('https://api.twilio.com/path/to/something.json?foo=12.34'),
'https://api.edge.region.twilio.com/path/to/something.json?foo=12.34')


class TestUserAgentClients(unittest.TestCase):
def setUp(self):
self.client = Client('username', 'password')

def tearDown(self):
self.client.http_client.session.close()

def test_set_default_user_agent(self):
self.client.request('GET', 'https://api.twilio.com/')
request_header = self.client.http_client.last_request.headers['User-Agent']
expected_user_agent = 'twilio-python/{} ({} {}) Python/{}'.format(
__version__,
platform.system(),
platform.machine(),
platform.python_version(),
)
self.assertEqual(request_header, expected_user_agent)

def test_set_user_agent_extensions(self):
user_agent_extensions = ['twilio-run/2.0.0-test', 'flex-plugin/3.4.0']
self.client.user_agent_extensions = user_agent_extensions
self.client.request('GET', 'https://api.twilio.com/')
request_header = self.client.http_client.last_request.headers['User-Agent']
expected_user_agent = 'twilio-python/{} ({} {}) Python/{} {} {}'.format(
__version__,
platform.system(),
platform.machine(),
platform.python_version(),
user_agent_extensions[0],
user_agent_extensions[1]
)
self.assertEqual(request_header, expected_user_agent)
20 changes: 16 additions & 4 deletions twilio/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Client(object):
""" A client for accessing the Twilio API. """

def __init__(self, username=None, password=None, account_sid=None, region=None,
http_client=None, environment=None, edge=None):
http_client=None, environment=None, edge=None,
user_agent_extensions=None):
"""
Initializes the Twilio Client
Expand All @@ -33,6 +34,7 @@ def __init__(self, username=None, password=None, account_sid=None, region=None,
:param HttpClient http_client: HttpClient, defaults to TwilioHttpClient
:param dict environment: Environment to look for auth details, defaults to os.environ
:param str edge: Twilio Edge to make requests to, defaults to None
:param list[str] user_agent_extensions: Additions to the user agent string
:returns: Twilio Client
:rtype: twilio.rest.Client
Expand All @@ -49,6 +51,8 @@ def __init__(self, username=None, password=None, account_sid=None, region=None,
""" :type : str """
self.region = region or environment.get('TWILIO_REGION')
""" :type : str """
self.user_agent_extensions = user_agent_extensions or []
""" :type : list[str] """

if not self.username or not self.password:
raise TwilioException("Credentials are required to create a TwilioClient")
Expand Down Expand Up @@ -113,10 +117,18 @@ def request(self, method, uri, params=None, data=None, headers=None, auth=None,
auth = auth or self.auth
headers = headers or {}

headers['User-Agent'] = 'twilio-python/{} (Python {})'.format(
__version__,
platform.python_version(),
pkg_version = __version__
os_name = platform.system()
os_arch = platform.machine()
python_version = platform.python_version()
headers['User-Agent'] = 'twilio-python/{} ({} {}) Python/{}'.format(
pkg_version,
os_name,
os_arch,
python_version,
)
for extension in self.user_agent_extensions:
headers['User-Agent'] += ' {}'.format(extension)
headers['X-Twilio-Client'] = 'python-{}'.format(__version__)
headers['Accept-Charset'] = 'utf-8'

Expand Down

0 comments on commit 128775b

Please sign in to comment.