Skip to content

Commit 1f09d2f

Browse files
committed
Fix #30: add user-agent header and control thereof.
1 parent b1be700 commit 1f09d2f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

tests/test_connections.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from conftest import mock_connection_params, MockResponse
2929
from umapi_client import Connection, UnavailableError, ServerError, RequestError
30+
from umapi_client import __version__ as umapi_version
3031

3132

3233
def test_remote_status_success():
@@ -52,6 +53,25 @@ def test_remote_status_timeout():
5253
_, remote_status = conn.status(remote=True)
5354
assert remote_status["status"].startswith("Unreachable")
5455

56+
def test_ua_string():
57+
conn = Connection(**mock_connection_params)
58+
req = conn.session.prepare_request(requests.Request('GET', "http://test.com/"))
59+
ua_header = req.headers.get("User-Agent")
60+
assert ua_header.startswith("umapi-client/" + umapi_version)
61+
assert " Python" in ua_header
62+
req = conn.session.prepare_request(requests.Request('POST', "http://test.com/", data="This is a test"))
63+
ua_header = req.headers.get("User-Agent")
64+
assert ua_header.startswith("umapi-client/" + umapi_version)
65+
assert " Python" in ua_header
66+
67+
def test_ua_string_additional():
68+
conn = Connection(user_agent="additional/1.0", **mock_connection_params)
69+
req = conn.session.prepare_request(requests.Request('GET', "http://test.com/"))
70+
ua_header = req.headers.get("User-Agent")
71+
assert ua_header.startswith("additional/1.0 umapi-client/" + umapi_version)
72+
req = conn.session.prepare_request(requests.Request('POST', "http://test.com/", data="This is a test"))
73+
ua_header = req.headers.get("User-Agent")
74+
assert ua_header.startswith("additional/1.0 umapi-client/" + umapi_version)
5575

5676
def test_get_success():
5777
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:

umapi_client/connection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import json
2222
import logging
2323
from email.utils import parsedate_tz, mktime_tz
24+
from platform import python_version, version as platform_version
2425
from random import randint
2526
from time import time, sleep, gmtime, strftime
2627

@@ -29,6 +30,7 @@
2930

3031
from .auth import JWT, Auth, AccessRequest
3132
from .error import UnavailableError, ClientError, RequestError, ServerError
33+
from .version import __version__ as umapi_version
3234

3335

3436
class Connection:
@@ -52,6 +54,7 @@ def __init__(self,
5254
timeout_seconds=60.0,
5355
throttle_actions=10,
5456
throttle_commands=10,
57+
user_agent=None,
5558
**kwargs):
5659
"""
5760
Open a connection for the given parameters that has the given options.
@@ -82,6 +85,7 @@ def __init__(self,
8285
:param timeout_seconds: How many seconds to wait for server response (default=60, <= 0 or None means forever)
8386
:param throttle_actions: Max number of actions to pack into a single call
8487
:param throttle_commands: Max number of commands allowed in a single action
88+
:param user_agent: (optional) string to use as User-Agent header (umapi-client/version data will be added)
8589
8690
Additional keywords are allowed to make it easy to pass a big dictionary with other values
8791
:param kwargs: any keywords passed that we ignore.
@@ -111,6 +115,10 @@ def __init__(self,
111115
else:
112116
raise ValueError("Connector create: either auth (an Auth object) or auth_dict (a dictionary) is required")
113117
self.session = requests.Session()
118+
ua_string = "umapi-client/" + umapi_version + " Python/" + python_version() + " (" + platform_version() + ")"
119+
if user_agent and user_agent.strip():
120+
ua_string = user_agent.strip() + " " + ua_string
121+
self.session.headers["User-Agent"] = ua_string
114122

115123
def _get_auth(self, ims_host, ims_endpoint_jwt,
116124
tech_acct_id=None, api_key=None, client_secret=None, private_key_file=None,

0 commit comments

Comments
 (0)