Skip to content

Commit

Permalink
Merge pull request #203 from EasyPost/backport_user_agent
Browse files Browse the repository at this point in the history
chore: backports user-agent
  • Loading branch information
Justintime50 authored Jul 1, 2022
2 parents 012232f + a58ad8b commit a6031f9
Show file tree
Hide file tree
Showing 26 changed files with 42 additions and 203 deletions.
52 changes: 33 additions & 19 deletions easypost/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import platform
import re
import ssl
import time

import six
Expand Down Expand Up @@ -65,9 +64,6 @@
timeout = min(60, _max_timeout)


USER_AGENT = "EasyPost/v2 PythonClient/{0}".format(VERSION)


class Error(Exception):
def __init__(self, message=None, http_status=None, http_body=None, original_exception=None):
super(Error, self).__init__(message)
Expand Down Expand Up @@ -223,31 +219,49 @@ def request_raw(self, method, url, params=None, apiKeyRequired=True):
abs_url = "%s%s" % (api_base, url or "")
params = self._objects_to_ids(params)

ua = {
# Fallback values for the user-agent header
user_agent = {
"client_version": VERSION,
"lang": "python",
"publisher": "easypost",
"request_lib": request_lib,
"implementation": "NA",
"os_arch": "NA",
"os_version": "NA",
"os": "NA",
"python_version": "NA",
}

# Attempt to populate the user-agent header
for attr, func in (
("lang_version", platform.python_version),
("platform", platform.platform),
("uname", lambda: " ".join(platform.uname())),
("implementation", platform.python_implementation),
("os_details", platform.uname),
("python_version", platform.python_version),
):
try:
val = func()
except Exception as e:
val = "!! %s" % e
ua[attr] = val

if hasattr(ssl, "OPENSSL_VERSION"):
ua["openssl_version"] = ssl.OPENSSL_VERSION
if attr == "os_details":
user_agent["os"] = val[0]
user_agent["os_version"] = val[2]
user_agent["os_arch"] = val[4]
else:
user_agent[attr] = val
except Exception:
# If we fail to get OS info, do nothing as we already set fallbacks for these values
pass

user_agent = (
"EasyPost/v2 PythonClient/{0} Python/{1} OS/{2} OSVersion/{3} OSArch/{4} Implementation/{5}".format(
VERSION,
user_agent["python_version"],
user_agent["os"],
user_agent["os_version"],
user_agent["os_arch"],
user_agent["implementation"],
)
)

headers = {
"X-Client-User-Agent": json.dumps(ua),
"User-Agent": USER_AGENT,
"Authorization": "Bearer %s" % my_api_key,
"Content-type": "application/json",
"User-Agent": user_agent,
}

if timeout > _max_timeout:
Expand Down
2 changes: 2 additions & 0 deletions examples/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
easypost.api_key = "API_KEY"

# unicode
# fmt: off
state = u"DELEGACI\xf3N BENITO JU\xe1REZ"
# fmt: on

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

Expand Down
2 changes: 0 additions & 2 deletions tests/cassettes/test_address_bytestring.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/addresses
response:
Expand Down
6 changes: 0 additions & 6 deletions tests/cassettes/test_address_creation_verification.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/addresses
response:
Expand Down Expand Up @@ -92,8 +90,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: GET
uri: https://api.easypost.com/v2/addresses/adr_e18d7993d8494cff94be285e1fbc6a41/verify
response:
Expand Down Expand Up @@ -170,8 +166,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: GET
uri: https://api.easypost.com/v2/addresses/adr_e18d7993d8494cff94be285e1fbc6a41
response:
Expand Down
2 changes: 0 additions & 2 deletions tests/cassettes/test_address_creation_with_verify.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/addresses
response:
Expand Down
2 changes: 0 additions & 2 deletions tests/cassettes/test_address_creation_with_verify_bool.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/addresses
response:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/addresses
response:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/addresses
response:
Expand Down
2 changes: 0 additions & 2 deletions tests/cassettes/test_address_unicode.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/addresses
response:
Expand Down
10 changes: 0 additions & 10 deletions tests/cassettes/test_batch_create_and_buy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/addresses
response:
Expand Down Expand Up @@ -95,8 +93,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/parcels
response:
Expand Down Expand Up @@ -175,8 +171,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/batches/create_and_buy
response:
Expand Down Expand Up @@ -250,8 +244,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: GET
uri: https://api.easypost.com/v2/batches/batch_e018ee73541a430dbd5b7ab19377543b
response:
Expand Down Expand Up @@ -326,8 +318,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/shipments/shp_d1db138265b8426283c4580b60618aad/insure
response:
Expand Down
2 changes: 0 additions & 2 deletions tests/cassettes/test_carrier_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: GET
uri: https://api.easypost.com/v2/carrier_types
response:
Expand Down
11 changes: 1 addition & 10 deletions tests/cassettes/test_child_user_create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/users
response:
Expand Down Expand Up @@ -91,8 +89,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/users
response:
Expand Down Expand Up @@ -159,8 +155,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: GET
uri: https://api.easypost.com/v2/users/user_99a72f96c77c4a9fa1d7c268b663ea4b
response:
Expand Down Expand Up @@ -232,8 +226,7 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed

method: PUT
uri: https://api.easypost.com/v2/users/user_99a72f96c77c4a9fa1d7c268b663ea4b
response:
Expand Down Expand Up @@ -303,8 +296,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: GET
uri: https://api.easypost.com/v2/users/user_99a72f96c77c4a9fa1d7c268b663ea4b
response:
Expand Down
6 changes: 0 additions & 6 deletions tests/cassettes/test_insurance_creation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/insurances
response:
Expand Down Expand Up @@ -99,8 +97,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: GET
uri: https://api.easypost.com/v2/insurances/ins_2a5732bf515349e59c883da5eb1a8e14
response:
Expand Down Expand Up @@ -183,8 +179,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: GET
uri: https://api.easypost.com/v2/insurances?page_size=5
response:
Expand Down
10 changes: 0 additions & 10 deletions tests/cassettes/test_order_create_then_buy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/orders
response:
Expand Down Expand Up @@ -115,8 +113,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: GET
uri: https://api.easypost.com/v2/orders/order_4a7076406aa8477bb66e619f354c99ce/rates
response:
Expand Down Expand Up @@ -211,8 +207,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/orders/order_4a7076406aa8477bb66e619f354c99ce/buy
response:
Expand Down Expand Up @@ -318,8 +312,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/shipments/shp_53f2547d3c0f409ab51a48d97295b898/insure
response:
Expand Down Expand Up @@ -421,8 +413,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/shipments/shp_843facf0843a4a04ad9cb1449cc5b5c5/insure
response:
Expand Down
2 changes: 0 additions & 2 deletions tests/cassettes/test_parcel_creation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ interactions:
- EZTK-NONE
user-agent:
- easypost/v2 pythonclient/suppressed
x-client-user-agent:
- suppressed
method: POST
uri: https://api.easypost.com/v2/parcels
response:
Expand Down
Loading

0 comments on commit a6031f9

Please sign in to comment.