Skip to content

Commit 854e645

Browse files
committed
Merge pull request #70 from shotgunsoftware/25762_add_platform_to_user_agent
for #25762: Add platform and python version info to user-agent
2 parents 1e61146 + bdab3c3 commit 854e645

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

shotgun_api3/shotgun.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ def __init__(self,
323323
self.config.api_path = urlparse.urljoin(urlparse.urljoin(
324324
api_base or "/", self.config.api_ver + "/"), "json")
325325

326-
self.reset_user_agent()
327326

328327
# if the service contains user information strip it out
329328
# copied from the xmlrpclib which turned the user:password into
@@ -359,9 +358,12 @@ def __init__(self,
359358
self._json_loads = self._json_loads_ascii
360359

361360
self.client_caps = ClientCapabilities()
361+
# this relies on self.client_caps being set first
362+
self.reset_user_agent()
363+
362364
self._server_caps = None
363-
#test to ensure the the server supports the json API
364-
#call to server will only be made once and will raise error
365+
# test to ensure the the server supports the json API
366+
# call to server will only be made once and will raise error
365367
if connect:
366368
self.server_caps
367369

@@ -1065,8 +1067,14 @@ def add_user_agent(self, agent):
10651067

10661068
def reset_user_agent(self):
10671069
"""Reset user agent to the default
1070+
1071+
Eg. shotgun-json (3.0.17); Python 2.6 (Mac)
10681072
"""
1069-
self._user_agents = ["shotgun-json (%s)" % __version__]
1073+
ua_platform = "Unknown"
1074+
if self.client_caps.platform is not None:
1075+
ua_platform = self.client_caps.platform.capitalize()
1076+
self._user_agents = ["shotgun-json (%s)" % __version__,
1077+
"Python %s (%s)" % (self.client_caps.py_version, ua_platform)]
10701078

10711079
def set_session_uuid(self, session_uuid):
10721080
"""Sets the browser session_uuid for this API session.

tests/test_client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,36 @@ def test_user_agent(self):
157157
"""User-Agent passed to server"""
158158
# test default user agent
159159
self.sg.info()
160+
client_caps = self.sg.client_caps
160161
args, _ = self.sg._http_request.call_args
161162
(_, _, _, headers) = args
162-
expected = "shotgun-json (%s)" % api.__version__
163+
expected = "shotgun-json (%s); Python %s (%s)" % (api.__version__,
164+
client_caps.py_version,
165+
client_caps.platform.capitalize()
166+
)
163167
self.assertEqual(expected, headers.get("user-agent"))
164168

165169
# test adding to user agent
166170
self.sg.add_user_agent("test-agent")
167171
self.sg.info()
168172
args, _ = self.sg._http_request.call_args
169173
(_, _, _, headers) = args
170-
expected = "shotgun-json (%s); test-agent" % api.__version__
174+
expected = "shotgun-json (%s); Python %s (%s); test-agent" % (
175+
api.__version__,
176+
client_caps.py_version,
177+
client_caps.platform.capitalize()
178+
)
171179
self.assertEqual(expected, headers.get("user-agent"))
172180

173181
# test resetting user agent
174182
self.sg.reset_user_agent()
175183
self.sg.info()
176184
args, _ = self.sg._http_request.call_args
177185
(_, _, _, headers) = args
178-
expected = "shotgun-json (%s)" % api.__version__
186+
expected = "shotgun-json (%s); Python %s (%s)" % (api.__version__,
187+
client_caps.py_version,
188+
client_caps.platform.capitalize(),
189+
)
179190
self.assertEqual(expected, headers.get("user-agent"))
180191

181192
def test_connect_close(self):

0 commit comments

Comments
 (0)