Skip to content

Commit 6a7befc

Browse files
authored
Add configurable user agent for CDPCLI interface (#34)
* Add property 'agent_header' to cdpy instantiation for configurable user-agent prefix for various downstream clients, with default of CDPY * Add CDPY and version to the user agent for convenient identification Signed-off-by: Daniel Chaffelson <chaffelson@gmail.com>
1 parent bfa4e20 commit 6a7befc

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/cdpy/common.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
from datetime import datetime
4+
import pkg_resources
45
from time import time, sleep
56
import html
67
import io
@@ -16,7 +17,7 @@
1617
from typing import Union
1718

1819
import cdpcli
19-
from cdpcli import VERSION
20+
from cdpcli import VERSION as CDPCLI_VERSION
2021
from cdpcli.client import ClientCreator, Context
2122
from cdpcli.credentials import Credentials
2223
from cdpcli.endpoint import EndpointCreator, EndpointResolver
@@ -136,7 +137,8 @@ def __init__(self, access_key_id, private_key, access_token='', method='static')
136137

137138
class CdpcliWrapper(object):
138139
def __init__(self, debug=False, tls_verify=False, strict_errors=False, tls_warnings=False, client_endpoint=None,
139-
cdp_credentials=None, error_handler=None, warning_handler=None, scrub_inputs=True, cp_region='default'):
140+
cdp_credentials=None, error_handler=None, warning_handler=None, scrub_inputs=True, cp_region='default',
141+
agent_header=None):
140142
# Init Params
141143
self.debug = debug
142144
self.tls_verify = tls_verify
@@ -146,6 +148,7 @@ def __init__(self, debug=False, tls_verify=False, strict_errors=False, tls_warni
146148
self.cdp_credentials = cdp_credentials
147149
self.scrub_inputs = scrub_inputs
148150
self.cp_region = cp_region
151+
self.agent_header = agent_header if agent_header is not None else 'CDPY'
149152

150153
# Setup
151154
self.throw_error = error_handler if error_handler else self._default_throw_error
@@ -221,7 +224,7 @@ def _warning_format(message, category, filename, lineno, line=None):
221224
'STOP_IN_PROGRESS',
222225
'STOPPED',
223226
'ENV_STOPPED',
224-
'Stopped', # DW
227+
'Stopped', # DW
225228
'NOT_ENABLED' # DF
226229
]
227230

@@ -254,12 +257,15 @@ def _warning_format(message, category, filename, lineno, line=None):
254257
self.CREDENTIAL_NAME_PATTERN = re.compile(r'[^a-z0-9-]')
255258
self.OPERATION_REGEX = re.compile(r'operation ([0-9a-zA-Z-]{36}) running')
256259

257-
@staticmethod
258-
def _make_user_agent_header():
259-
return 'CDPSDK/%s Python/%s %s/%s' % (VERSION,
260-
platform.python_version(),
261-
platform.system(),
262-
platform.release())
260+
def _make_user_agent_header(self):
261+
cdpy_version = pkg_resources.get_distribution('cdpy').version
262+
return '%s CDPY/%s CDPCLI/%s Python/%s %s/%s' % (
263+
self.agent_header,
264+
cdpy_version,
265+
CDPCLI_VERSION,
266+
platform.python_version(),
267+
platform.system(),
268+
platform.release())
263269

264270
@staticmethod
265271
def _load_retry_config(loader):

0 commit comments

Comments
 (0)