Skip to content

Commit a6031f9

Browse files
authored
Merge pull request #203 from EasyPost/backport_user_agent
chore: backports user-agent
2 parents 012232f + a58ad8b commit a6031f9

26 files changed

+42
-203
lines changed

easypost/__init__.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import json
33
import platform
44
import re
5-
import ssl
65
import time
76

87
import six
@@ -65,9 +64,6 @@
6564
timeout = min(60, _max_timeout)
6665

6766

68-
USER_AGENT = "EasyPost/v2 PythonClient/{0}".format(VERSION)
69-
70-
7167
class Error(Exception):
7268
def __init__(self, message=None, http_status=None, http_body=None, original_exception=None):
7369
super(Error, self).__init__(message)
@@ -223,31 +219,49 @@ def request_raw(self, method, url, params=None, apiKeyRequired=True):
223219
abs_url = "%s%s" % (api_base, url or "")
224220
params = self._objects_to_ids(params)
225221

226-
ua = {
222+
# Fallback values for the user-agent header
223+
user_agent = {
227224
"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",
231230
}
231+
232+
# Attempt to populate the user-agent header
232233
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),
236237
):
237238
try:
238239
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+
)
245260

246261
headers = {
247-
"X-Client-User-Agent": json.dumps(ua),
248-
"User-Agent": USER_AGENT,
249262
"Authorization": "Bearer %s" % my_api_key,
250263
"Content-type": "application/json",
264+
"User-Agent": user_agent,
251265
}
252266

253267
if timeout > _max_timeout:

examples/unicode.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
easypost.api_key = "API_KEY"
44

55
# unicode
6+
# fmt: off
67
state = u"DELEGACI\xf3N BENITO JU\xe1REZ"
8+
# fmt: on
79

810
address = easypost.Address.create(state=state)
911

tests/cassettes/test_address_bytestring.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ interactions:
1414
- EZTK-NONE
1515
user-agent:
1616
- easypost/v2 pythonclient/suppressed
17-
x-client-user-agent:
18-
- suppressed
1917
method: POST
2018
uri: https://api.easypost.com/v2/addresses
2119
response:

tests/cassettes/test_address_creation_verification.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ interactions:
1616
- EZTK-NONE
1717
user-agent:
1818
- easypost/v2 pythonclient/suppressed
19-
x-client-user-agent:
20-
- suppressed
2119
method: POST
2220
uri: https://api.easypost.com/v2/addresses
2321
response:
@@ -92,8 +90,6 @@ interactions:
9290
- EZTK-NONE
9391
user-agent:
9492
- easypost/v2 pythonclient/suppressed
95-
x-client-user-agent:
96-
- suppressed
9793
method: GET
9894
uri: https://api.easypost.com/v2/addresses/adr_e18d7993d8494cff94be285e1fbc6a41/verify
9995
response:
@@ -170,8 +166,6 @@ interactions:
170166
- EZTK-NONE
171167
user-agent:
172168
- easypost/v2 pythonclient/suppressed
173-
x-client-user-agent:
174-
- suppressed
175169
method: GET
176170
uri: https://api.easypost.com/v2/addresses/adr_e18d7993d8494cff94be285e1fbc6a41
177171
response:

tests/cassettes/test_address_creation_with_verify.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ interactions:
1616
- EZTK-NONE
1717
user-agent:
1818
- easypost/v2 pythonclient/suppressed
19-
x-client-user-agent:
20-
- suppressed
2119
method: POST
2220
uri: https://api.easypost.com/v2/addresses
2321
response:

tests/cassettes/test_address_creation_with_verify_bool.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ interactions:
1616
- EZTK-NONE
1717
user-agent:
1818
- easypost/v2 pythonclient/suppressed
19-
x-client-user-agent:
20-
- suppressed
2119
method: POST
2220
uri: https://api.easypost.com/v2/addresses
2321
response:

tests/cassettes/test_address_creation_with_verify_failure.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ interactions:
1616
- EZTK-NONE
1717
user-agent:
1818
- easypost/v2 pythonclient/suppressed
19-
x-client-user-agent:
20-
- suppressed
2119
method: POST
2220
uri: https://api.easypost.com/v2/addresses
2321
response:

tests/cassettes/test_address_creation_with_verify_strict_failure.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ interactions:
1616
- EZTK-NONE
1717
user-agent:
1818
- easypost/v2 pythonclient/suppressed
19-
x-client-user-agent:
20-
- suppressed
2119
method: POST
2220
uri: https://api.easypost.com/v2/addresses
2321
response:

tests/cassettes/test_address_unicode.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ interactions:
1414
- EZTK-NONE
1515
user-agent:
1616
- easypost/v2 pythonclient/suppressed
17-
x-client-user-agent:
18-
- suppressed
1917
method: POST
2018
uri: https://api.easypost.com/v2/addresses
2119
response:

tests/cassettes/test_batch_create_and_buy.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ interactions:
1616
- EZTK-NONE
1717
user-agent:
1818
- easypost/v2 pythonclient/suppressed
19-
x-client-user-agent:
20-
- suppressed
2119
method: POST
2220
uri: https://api.easypost.com/v2/addresses
2321
response:
@@ -95,8 +93,6 @@ interactions:
9593
- EZTK-NONE
9694
user-agent:
9795
- easypost/v2 pythonclient/suppressed
98-
x-client-user-agent:
99-
- suppressed
10096
method: POST
10197
uri: https://api.easypost.com/v2/parcels
10298
response:
@@ -175,8 +171,6 @@ interactions:
175171
- EZTK-NONE
176172
user-agent:
177173
- easypost/v2 pythonclient/suppressed
178-
x-client-user-agent:
179-
- suppressed
180174
method: POST
181175
uri: https://api.easypost.com/v2/batches/create_and_buy
182176
response:
@@ -250,8 +244,6 @@ interactions:
250244
- EZTK-NONE
251245
user-agent:
252246
- easypost/v2 pythonclient/suppressed
253-
x-client-user-agent:
254-
- suppressed
255247
method: GET
256248
uri: https://api.easypost.com/v2/batches/batch_e018ee73541a430dbd5b7ab19377543b
257249
response:
@@ -326,8 +318,6 @@ interactions:
326318
- EZTK-NONE
327319
user-agent:
328320
- easypost/v2 pythonclient/suppressed
329-
x-client-user-agent:
330-
- suppressed
331321
method: POST
332322
uri: https://api.easypost.com/v2/shipments/shp_d1db138265b8426283c4580b60618aad/insure
333323
response:

0 commit comments

Comments
 (0)