|
2 | 2 | import json |
3 | 3 | import platform |
4 | 4 | import re |
5 | | -import ssl |
6 | 5 | import time |
7 | 6 |
|
8 | 7 | import six |
|
65 | 64 | timeout = min(60, _max_timeout) |
66 | 65 |
|
67 | 66 |
|
68 | | -USER_AGENT = "EasyPost/v2 PythonClient/{0}".format(VERSION) |
69 | | - |
70 | | - |
71 | 67 | class Error(Exception): |
72 | 68 | def __init__(self, message=None, http_status=None, http_body=None, original_exception=None): |
73 | 69 | super(Error, self).__init__(message) |
@@ -223,31 +219,49 @@ def request_raw(self, method, url, params=None, apiKeyRequired=True): |
223 | 219 | abs_url = "%s%s" % (api_base, url or "") |
224 | 220 | params = self._objects_to_ids(params) |
225 | 221 |
|
226 | | - ua = { |
| 222 | + # Fallback values for the user-agent header |
| 223 | + user_agent = { |
227 | 224 | "client_version": VERSION, |
228 | | - "lang": "python", |
229 | | - "publisher": "easypost", |
230 | | - "request_lib": request_lib, |
| 225 | + "implementation": "NA", |
| 226 | + "os_arch": "NA", |
| 227 | + "os_version": "NA", |
| 228 | + "os": "NA", |
| 229 | + "python_version": "NA", |
231 | 230 | } |
| 231 | + |
| 232 | + # Attempt to populate the user-agent header |
232 | 233 | for attr, func in ( |
233 | | - ("lang_version", platform.python_version), |
234 | | - ("platform", platform.platform), |
235 | | - ("uname", lambda: " ".join(platform.uname())), |
| 234 | + ("implementation", platform.python_implementation), |
| 235 | + ("os_details", platform.uname), |
| 236 | + ("python_version", platform.python_version), |
236 | 237 | ): |
237 | 238 | try: |
238 | 239 | val = func() |
239 | | - except Exception as e: |
240 | | - val = "!! %s" % e |
241 | | - ua[attr] = val |
242 | | - |
243 | | - if hasattr(ssl, "OPENSSL_VERSION"): |
244 | | - ua["openssl_version"] = ssl.OPENSSL_VERSION |
| 240 | + if attr == "os_details": |
| 241 | + user_agent["os"] = val[0] |
| 242 | + user_agent["os_version"] = val[2] |
| 243 | + user_agent["os_arch"] = val[4] |
| 244 | + else: |
| 245 | + user_agent[attr] = val |
| 246 | + except Exception: |
| 247 | + # If we fail to get OS info, do nothing as we already set fallbacks for these values |
| 248 | + pass |
| 249 | + |
| 250 | + user_agent = ( |
| 251 | + "EasyPost/v2 PythonClient/{0} Python/{1} OS/{2} OSVersion/{3} OSArch/{4} Implementation/{5}".format( |
| 252 | + VERSION, |
| 253 | + user_agent["python_version"], |
| 254 | + user_agent["os"], |
| 255 | + user_agent["os_version"], |
| 256 | + user_agent["os_arch"], |
| 257 | + user_agent["implementation"], |
| 258 | + ) |
| 259 | + ) |
245 | 260 |
|
246 | 261 | headers = { |
247 | | - "X-Client-User-Agent": json.dumps(ua), |
248 | | - "User-Agent": USER_AGENT, |
249 | 262 | "Authorization": "Bearer %s" % my_api_key, |
250 | 263 | "Content-type": "application/json", |
| 264 | + "User-Agent": user_agent, |
251 | 265 | } |
252 | 266 |
|
253 | 267 | if timeout > _max_timeout: |
|
0 commit comments