39
39
from builtins import *
40
40
41
41
import requests
42
+ import urllib
43
+ import platform
44
+ import sys
45
+ import json
42
46
from past .builtins import basestring
43
47
44
48
from .config import DEFAULT_SINGLE_REQUEST_TIMEOUT , DEFAULT_WAIT_ON_RATE_LIMIT
48
52
check_response_code , check_type , extract_and_parse_json , validate_base_url ,
49
53
)
50
54
55
+ from webexteamssdk ._version import get_versions
51
56
52
57
# Helper Functions
53
58
def _fix_next_url (next_url ):
@@ -100,7 +105,9 @@ class RestSession(object):
100
105
def __init__ (self , access_token , base_url ,
101
106
single_request_timeout = DEFAULT_SINGLE_REQUEST_TIMEOUT ,
102
107
wait_on_rate_limit = DEFAULT_WAIT_ON_RATE_LIMIT ,
103
- proxies = None ):
108
+ proxies = None ,
109
+ be_geo_id = None ,
110
+ caller = None ):
104
111
"""Initialize a new RestSession object.
105
112
106
113
Args:
@@ -114,6 +121,12 @@ def __init__(self, access_token, base_url,
114
121
handling.
115
122
proxies(dict): Dictionary of proxies passed on to the requests
116
123
session.
124
+ be_geo_id(basestring): Optional partner identifier for API usage
125
+ tracking. Defaults to checking for a BE_GEO_ID environment
126
+ variable.
127
+ caller(basestring): Optional identifier for API usage tracking.
128
+ Defaults to checking for a WEBEX_PYTHON_SDK_CALLER environment
129
+ variable.
117
130
118
131
Raises:
119
132
TypeError: If the parameter types are incorrect.
@@ -139,9 +152,60 @@ def __init__(self, access_token, base_url,
139
152
if proxies is not None :
140
153
self ._req_session .proxies .update (proxies )
141
154
155
+ # Build a User-Agent header
156
+ ua_base = 'python-webexteams/' + get_versions ()['version' ] + ' '
157
+
158
+ # Generate extended portion of the User-Agent
159
+ ua_ext = {}
160
+
161
+ # Mimic pip system data collection per
162
+ # https://github.com/pypa/pip/blob/master/src/pip/_internal/network/session.py
163
+ ua_ext ['implementation' ] = {
164
+ "name" : platform .python_implementation (),
165
+ }
166
+
167
+ if ua_ext ["implementation" ]["name" ] == 'CPython' :
168
+ ua_ext ["implementation" ]["version" ] = platform .python_version ()
169
+ elif ua_ext ["implementation" ]["name" ] == 'PyPy' :
170
+ if sys .pypy_version_info .releaselevel == 'final' :
171
+ pypy_version_info = sys .pypy_version_info [:3 ]
172
+ else :
173
+ pypy_version_info = sys .pypy_version_info
174
+ ua_ext ["implementation" ]["version" ] = "." .join (
175
+ [str (x ) for x in pypy_version_info ]
176
+ )
177
+ elif ua_ext ["implementation" ]["name" ] == 'Jython' :
178
+ ua_ext ["implementation" ]["version" ] = platform .python_version ()
179
+ elif ua_ext ["implementation" ]["name" ] == 'IronPython' :
180
+ ua_ext ["implementation" ]["version" ] = platform .python_version ()
181
+
182
+ if sys .platform .startswith ("darwin" ) and platform .mac_ver ()[0 ]:
183
+ dist = {"name" : "macOS" , "version" : platform .mac_ver ()[0 ]}
184
+ ua_ext ["distro" ] = dist
185
+
186
+ if platform .system ():
187
+ ua_ext .setdefault ("system" , {})["name" ] = platform .system ()
188
+
189
+ if platform .release ():
190
+ ua_ext .setdefault ("system" , {})["release" ] = platform .release ()
191
+
192
+ if platform .machine ():
193
+ ua_ext ["cpu" ] = platform .machine ()
194
+
195
+ if be_geo_id :
196
+ ua_ext ["be_geo_id" ] = be_geo_id
197
+
198
+ if caller :
199
+ ua_ext ["caller" ] = caller
200
+
201
+ # Override the default requests User-Agent but not other headers
202
+ new_ua = ua_base + urllib .parse .quote (json .dumps (ua_ext ))
203
+ self ._req_session .headers ['User-Agent' ] = new_ua
204
+
142
205
# Update the headers of the `requests` session
143
206
self .update_headers ({'Authorization' : 'Bearer ' + access_token ,
144
207
'Content-type' : 'application/json;charset=utf-8' })
208
+ print (self ._req_session .headers )
145
209
146
210
@property
147
211
def base_url (self ):
0 commit comments