Skip to content

Commit 3b35300

Browse files
noahpmaxd-nordic
authored andcommitted
feat: add custom User-Agent to requests
For identifying tool usage on the backend side, add a custom user agent to HTTP requests.
1 parent 441ebe3 commit 3b35300

6 files changed

Lines changed: 1727 additions & 12 deletions

File tree

src/nrfcloud_utils/cli_helpers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from os import path
88
from os import makedirs
9+
from importlib.metadata import version, PackageNotFoundError
910
import platform
1011
import os
1112
import csv
@@ -21,6 +22,16 @@
2122

2223
logger = logging.getLogger(__name__)
2324

25+
try:
26+
_package_version = version('nrfcloud-utils')
27+
except PackageNotFoundError:
28+
_package_version = 'unknown'
29+
30+
USER_AGENT = (
31+
f'nrfcloud-utils/{_package_version} '
32+
f'({platform.system()} {platform.release()}; Python {platform.python_version()})'
33+
)
34+
2435
def setup_logging(level='info', fmt='%(levelname)-8s %(message)s', use_color=True):
2536
"""
2637
Configure colored logging with dark-terminal-friendly colors.

src/nrfcloud_utils/nrf93_onboard.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import coloredlogs
1313
from nrfcredstore.comms import Comms
1414
from nrfcredstore.command_interface import ATCommandInterface
15+
from nrfcloud_utils.cli_helpers import USER_AGENT
1516

1617
logger = logging.getLogger(__name__)
1718

@@ -144,7 +145,7 @@ def set_dev_stage(stage = ''):
144145
return api_url
145146

146147
def fetch_tenant_id(api_key):
147-
hdr = {'Authorization': 'Bearer ' + api_key}
148+
hdr = {'Authorization': 'Bearer ' + api_key, 'User-Agent': USER_AGENT}
148149
req = api_url + "account"
149150
response = requests.get(req, headers=hdr)
150151
if not response.ok:
@@ -192,6 +193,7 @@ def onboard_device(api_key, dev_id, sub_type, tags, fw_types, onboarding_token):
192193
hdr = {
193194
'Authorization': 'Bearer ' + api_key,
194195
'Accept': 'application/json',
196+
'User-Agent': USER_AGENT,
195197
}
196198

197199
req = api_url + "devices/" + dev_id

src/nrfcloud_utils/nrf_cloud_device_mgmt.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from urllib.parse import quote_plus as url_encode
1313
from datetime import datetime
1414
from enum import Enum
15+
from nrfcloud_utils.cli_helpers import USER_AGENT
1516

1617
API_URL = "https://api.nrfcloud.com/v1/"
1718

@@ -229,7 +230,7 @@ def parse_args(in_args):
229230

230231
def get_bundle_list(api_key, modem_only):
231232

232-
hdr = {'Authorization': 'Bearer ' + api_key}
233+
hdr = {'Authorization': 'Bearer ' + api_key, 'User-Agent': USER_AGENT}
233234
req_base = f'{GET_BUNDLES_BASE}?pageLimit={GET_BUNDLES_PAGE_LIMIT}'
234235

235236
if modem_only:
@@ -273,7 +274,7 @@ def get_requested_bundles(api_key, fota_type):
273274
return [i for i in bundles if i.type == fota_type.name]
274275

275276
def get_device_list(api_key, fota_types_list, device_id):
276-
hdr = {'Authorization': 'Bearer ' + api_key}
277+
hdr = {'Authorization': 'Bearer ' + api_key, 'User-Agent': USER_AGENT}
277278
req_base = GET_DEVICES_BASE + f'&pageLimit={GET_DEVICES_PAGE_LIMIT}'
278279

279280
if fota_types_list:
@@ -321,7 +322,7 @@ def print_api_result(custom_text, api_result, print_response_txt):
321322

322323
def create_fota_job(api_key, json_payload_obj):
323324
jobId = None
324-
hdr = {'Authorization': f'Bearer {api_key}'}
325+
hdr = {'Authorization': f'Bearer {api_key}', 'User-Agent': USER_AGENT}
325326
req = FOTA_JOBS_BASE
326327

327328
api_res = requests.post(req, json=json_payload_obj, headers=hdr)

src/nrfcloud_utils/nrf_cloud_diap.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import requests
88
import logging
99
from nrfcloud_utils import ca_certs
10+
from nrfcloud_utils.cli_helpers import USER_AGENT
1011

1112
DEV_STAGE_DICT = {'dev': '.dev.',
1213
'beta': '.beta.',
@@ -44,7 +45,7 @@ def set_dev_stage(stage = ''):
4445
def get_auth_header(api_key):
4546
if not api_key:
4647
return None
47-
return { AUTH : BEARER + api_key}
48+
return { AUTH : BEARER + api_key, 'User-Agent': USER_AGENT }
4849

4950
def claim_device(api_key, claim_token, tags = None):
5051
global api_url

src/nrfcloud_utils/nrf_cloud_onboard.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import logging
1616
from os import makedirs
1717
from enum import Enum
18-
from nrfcloud_utils.cli_helpers import write_file, setup_logging
18+
from nrfcloud_utils.cli_helpers import write_file, setup_logging, USER_AGENT
1919

2020
logger = logging.getLogger(__name__)
2121

@@ -105,30 +105,31 @@ def set_dev_stage(stage = ''):
105105
return api_url
106106

107107
def get_bulk_ops_result(api_key, bulk_ops_req_id):
108-
hdr = {'Authorization': 'Bearer ' + api_key}
108+
hdr = {'Authorization': 'Bearer ' + api_key, 'User-Agent': USER_AGENT}
109109
req = api_url + "bulk-ops-requests/" + bulk_ops_req_id
110110
return requests.get(req, headers=hdr)
111111

112112
def update_device_shadow(api_key, device_id, json_payload):
113-
hdr = {'Authorization': 'Bearer ' + api_key}
113+
hdr = {'Authorization': 'Bearer ' + api_key, 'User-Agent': USER_AGENT}
114114
req = api_url + "devices/" + device_id + "/state"
115115
return requests.patch(req, json=json_payload, headers=hdr)
116116

117117
def fetch_device(api_key, device_id):
118-
hdr = {'Authorization': 'Bearer ' + api_key}
118+
hdr = {'Authorization': 'Bearer ' + api_key, 'User-Agent': USER_AGENT}
119119
req = api_url + "devices/" + device_id
120120
return requests.get(req, headers=hdr)
121121

122122
def update_device_name(api_key, device_id, name):
123-
hdr = {'Authorization': 'Bearer ' + api_key}
123+
hdr = {'Authorization': 'Bearer ' + api_key, 'User-Agent': USER_AGENT}
124124
req = api_url + "devices/" + device_id + "/name"
125125
json_payload = [name]
126126
return requests.put(req, json=json_payload, headers=hdr)
127127

128128
def onboard_device(api_key, dev_id, sub_type, tags, fw_types, cert_pem_str):
129129
hdr = {'Authorization': 'Bearer ' + api_key,
130130
'content-type' : 'text/plain',
131-
'Accept-Encoding' : '*'}
131+
'Accept-Encoding' : '*',
132+
'User-Agent': USER_AGENT}
132133

133134
req = api_url + "devices"
134135

@@ -139,7 +140,8 @@ def onboard_device(api_key, dev_id, sub_type, tags, fw_types, cert_pem_str):
139140
def onboard_devices(api_key, csv_filepath):
140141
hdr = {'Authorization': 'Bearer ' + api_key,
141142
'content-type' : 'text/csv',
142-
'Accept-Encoding' : '*'}
143+
'Accept-Encoding' : '*',
144+
'User-Agent': USER_AGENT}
143145

144146
req = api_url + "devices"
145147

0 commit comments

Comments
 (0)