Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/nrfcloud_utils/cli_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from os import path
from os import makedirs
from importlib.metadata import version, PackageNotFoundError
import platform
import os
import csv
Expand All @@ -21,6 +22,16 @@

logger = logging.getLogger(__name__)

try:
_package_version = version('nrfcloud-utils')
except PackageNotFoundError:
_package_version = 'unknown'

USER_AGENT = (
f'nrfcloud-utils/{_package_version} '
f'({platform.system()} {platform.release()}; Python {platform.python_version()})'
)

def setup_logging(level='info', fmt='%(levelname)-8s %(message)s', use_color=True):
"""
Configure colored logging with dark-terminal-friendly colors.
Expand Down
4 changes: 3 additions & 1 deletion src/nrfcloud_utils/nrf93_onboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import coloredlogs
from nrfcredstore.comms import Comms
from nrfcredstore.command_interface import ATCommandInterface
from nrfcloud_utils.cli_helpers import USER_AGENT

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -144,7 +145,7 @@ def set_dev_stage(stage = ''):
return api_url

def fetch_tenant_id(api_key):
hdr = {'Authorization': 'Bearer ' + api_key}
hdr = {'Authorization': 'Bearer ' + api_key, 'User-Agent': USER_AGENT}
req = api_url + "account"
response = requests.get(req, headers=hdr)
Comment thread
noahp marked this conversation as resolved.
if not response.ok:
Expand Down Expand Up @@ -192,6 +193,7 @@ def onboard_device(api_key, dev_id, sub_type, tags, fw_types, onboarding_token):
hdr = {
'Authorization': 'Bearer ' + api_key,
'Accept': 'application/json',
'User-Agent': USER_AGENT,
}

req = api_url + "devices/" + dev_id
Expand Down
7 changes: 4 additions & 3 deletions src/nrfcloud_utils/nrf_cloud_device_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from urllib.parse import quote_plus as url_encode
from datetime import datetime
from enum import Enum
from nrfcloud_utils.cli_helpers import USER_AGENT

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

Expand Down Expand Up @@ -229,7 +230,7 @@ def parse_args(in_args):

def get_bundle_list(api_key, modem_only):

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

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

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

if fota_types_list:
Expand Down Expand Up @@ -321,7 +322,7 @@ def print_api_result(custom_text, api_result, print_response_txt):

def create_fota_job(api_key, json_payload_obj):
jobId = None
hdr = {'Authorization': f'Bearer {api_key}'}
hdr = {'Authorization': f'Bearer {api_key}', 'User-Agent': USER_AGENT}
req = FOTA_JOBS_BASE

api_res = requests.post(req, json=json_payload_obj, headers=hdr)
Expand Down
3 changes: 2 additions & 1 deletion src/nrfcloud_utils/nrf_cloud_diap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import requests
import logging
from nrfcloud_utils import ca_certs
from nrfcloud_utils.cli_helpers import USER_AGENT

DEV_STAGE_DICT = {'dev': '.dev.',
'beta': '.beta.',
Expand Down Expand Up @@ -44,7 +45,7 @@ def set_dev_stage(stage = ''):
def get_auth_header(api_key):
if not api_key:
return None
return { AUTH : BEARER + api_key}
return { AUTH : BEARER + api_key, 'User-Agent': USER_AGENT }

def claim_device(api_key, claim_token, tags = None):
global api_url
Expand Down
16 changes: 9 additions & 7 deletions src/nrfcloud_utils/nrf_cloud_onboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import logging
from os import makedirs
from enum import Enum
from nrfcloud_utils.cli_helpers import write_file, setup_logging
from nrfcloud_utils.cli_helpers import write_file, setup_logging, USER_AGENT

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -105,30 +105,31 @@ def set_dev_stage(stage = ''):
return api_url

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

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

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

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

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

req = api_url + "devices"

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

req = api_url + "devices"

Expand Down
Loading
Loading