Skip to content

Commit a6031f9

Browse files
authoredJul 1, 2022
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:

‎tests/cassettes/test_carrier_types.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ interactions:
1212
- EZTK-NONE
1313
user-agent:
1414
- easypost/v2 pythonclient/suppressed
15-
x-client-user-agent:
16-
- suppressed
1715
method: GET
1816
uri: https://api.easypost.com/v2/carrier_types
1917
response:

‎tests/cassettes/test_child_user_create.yaml

Lines changed: 1 addition & 10 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/users
2119
response:
@@ -91,8 +89,6 @@ interactions:
9189
- EZTK-NONE
9290
user-agent:
9391
- easypost/v2 pythonclient/suppressed
94-
x-client-user-agent:
95-
- suppressed
9692
method: POST
9793
uri: https://api.easypost.com/v2/users
9894
response:
@@ -159,8 +155,6 @@ interactions:
159155
- EZTK-NONE
160156
user-agent:
161157
- easypost/v2 pythonclient/suppressed
162-
x-client-user-agent:
163-
- suppressed
164158
method: GET
165159
uri: https://api.easypost.com/v2/users/user_99a72f96c77c4a9fa1d7c268b663ea4b
166160
response:
@@ -232,8 +226,7 @@ interactions:
232226
- EZTK-NONE
233227
user-agent:
234228
- easypost/v2 pythonclient/suppressed
235-
x-client-user-agent:
236-
- suppressed
229+
237230
method: PUT
238231
uri: https://api.easypost.com/v2/users/user_99a72f96c77c4a9fa1d7c268b663ea4b
239232
response:
@@ -303,8 +296,6 @@ interactions:
303296
- EZTK-NONE
304297
user-agent:
305298
- easypost/v2 pythonclient/suppressed
306-
x-client-user-agent:
307-
- suppressed
308299
method: GET
309300
uri: https://api.easypost.com/v2/users/user_99a72f96c77c4a9fa1d7c268b663ea4b
310301
response:

‎tests/cassettes/test_insurance_creation.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ interactions:
2121
- EZTK-NONE
2222
user-agent:
2323
- easypost/v2 pythonclient/suppressed
24-
x-client-user-agent:
25-
- suppressed
2624
method: POST
2725
uri: https://api.easypost.com/v2/insurances
2826
response:
@@ -99,8 +97,6 @@ interactions:
9997
- EZTK-NONE
10098
user-agent:
10199
- easypost/v2 pythonclient/suppressed
102-
x-client-user-agent:
103-
- suppressed
104100
method: GET
105101
uri: https://api.easypost.com/v2/insurances/ins_2a5732bf515349e59c883da5eb1a8e14
106102
response:
@@ -183,8 +179,6 @@ interactions:
183179
- EZTK-NONE
184180
user-agent:
185181
- easypost/v2 pythonclient/suppressed
186-
x-client-user-agent:
187-
- suppressed
188182
method: GET
189183
uri: https://api.easypost.com/v2/insurances?page_size=5
190184
response:

‎tests/cassettes/test_order_create_then_buy.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ interactions:
2121
- EZTK-NONE
2222
user-agent:
2323
- easypost/v2 pythonclient/suppressed
24-
x-client-user-agent:
25-
- suppressed
2624
method: POST
2725
uri: https://api.easypost.com/v2/orders
2826
response:
@@ -115,8 +113,6 @@ interactions:
115113
- EZTK-NONE
116114
user-agent:
117115
- easypost/v2 pythonclient/suppressed
118-
x-client-user-agent:
119-
- suppressed
120116
method: GET
121117
uri: https://api.easypost.com/v2/orders/order_4a7076406aa8477bb66e619f354c99ce/rates
122118
response:
@@ -211,8 +207,6 @@ interactions:
211207
- EZTK-NONE
212208
user-agent:
213209
- easypost/v2 pythonclient/suppressed
214-
x-client-user-agent:
215-
- suppressed
216210
method: POST
217211
uri: https://api.easypost.com/v2/orders/order_4a7076406aa8477bb66e619f354c99ce/buy
218212
response:
@@ -318,8 +312,6 @@ interactions:
318312
- EZTK-NONE
319313
user-agent:
320314
- easypost/v2 pythonclient/suppressed
321-
x-client-user-agent:
322-
- suppressed
323315
method: POST
324316
uri: https://api.easypost.com/v2/shipments/shp_53f2547d3c0f409ab51a48d97295b898/insure
325317
response:
@@ -421,8 +413,6 @@ interactions:
421413
- EZTK-NONE
422414
user-agent:
423415
- easypost/v2 pythonclient/suppressed
424-
x-client-user-agent:
425-
- suppressed
426416
method: POST
427417
uri: https://api.easypost.com/v2/shipments/shp_843facf0843a4a04ad9cb1449cc5b5c5/insure
428418
response:

‎tests/cassettes/test_parcel_creation.yaml

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

0 commit comments

Comments
 (0)