Skip to content

Commit db55f78

Browse files
committed
enabled user-agent logging in request headers
1 parent f06ad2c commit db55f78

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

scaleapi/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import requests
2+
import platform
23

34
from .tasks import Task
45
from .batches import Batch
56
from .projects import Project
7+
from ._version import __version__
68

79
TASK_TYPES = [
810
'annotation',
@@ -63,6 +65,10 @@ class Batchlist(Paginator):
6365
class ScaleClient(object):
6466
def __init__(self, api_key):
6567
self.api_key = api_key
68+
self._headers = {
69+
"Content-Type": "application/json",
70+
"User-Agent": _generate_useragent()
71+
}
6672

6773
def _getrequest(self, endpoint, params=None):
6874
"""Makes a get request to an endpoint.
@@ -73,7 +79,7 @@ def _getrequest(self, endpoint, params=None):
7379
"""
7480
params = params or {}
7581
r = requests.get(SCALE_ENDPOINT + endpoint,
76-
headers={"Content-Type": "application/json"},
82+
headers=self._headers,
7783
auth=(self.api_key, ''), params=params)
7884

7985
if r.status_code == 200:
@@ -97,7 +103,7 @@ def _postrequest(self, endpoint, payload=None):
97103
"""
98104
payload = payload or {}
99105
r = requests.post(SCALE_ENDPOINT + endpoint, json=payload,
100-
headers={"Content-Type": "application/json"},
106+
headers=self._headers,
101107
auth=(self.api_key, ''))
102108

103109
if r.status_code == 200:
@@ -212,6 +218,15 @@ def update_project(self, project_name, **kwargs):
212218
projectdata = self._postrequest('projects/%s/setParams' % project_name, payload=kwargs)
213219
return projectdata
214220

221+
def _generate_useragent():
222+
try:
223+
python_version = platform.python_version()
224+
os_platform = platform.platform()
225+
226+
user_agent = '%s/%s Python/%s OS/%s' % (__name__, __version__, python_version, os_platform)
227+
return user_agent
228+
except:
229+
return "scaleapi-python-client"
215230

216231
def _AddTaskTypeCreator(task_type):
217232
def create_task_wrapper(self, **kwargs):

0 commit comments

Comments
 (0)