Skip to content

Commit ada216a

Browse files
Merge pull request #24 from scaleapi/useragent-and-fixes
Single-sourcing the package version, user-agent and bug fixes
2 parents 2e17588 + 5cb57e5 commit ada216a

File tree

10 files changed

+37
-265
lines changed

10 files changed

+37
-265
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
.cache
66
/.vscode/
77
.DS_Store
8+
/build/

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ __ https://docs.scale.com/reference#project-retrieval
216216

217217
.. code-block:: python
218218
219-
client.get_projet(project_name = 'test_project')
219+
client.get_project(project_name = 'test_project')
220220
221221
List Projects
222222
^^^^^^^^^^^^^

build/lib/scaleapi/__init__.py

Lines changed: 0 additions & 188 deletions
This file was deleted.

build/lib/scaleapi/batches.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

build/lib/scaleapi/projects.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

build/lib/scaleapi/tasks.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

pypi_update_guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ _Creating and deploying a new package version is easy_
1010

1111
**Step 0: Critical - Bump Project Version**
1212

13-
In `setup.py`, you need to specify a new project version.
13+
In `_version.py`, you need to specify a new project version.
1414

1515
We use [semantic versioning](https://packaging.python.org/guides/distributing-packages-using-setuptools/#semantic-versioning-preferred). If you are adding a meaningful feature, bump the minor version. If you are fixing a bug, bump the incremental version.
1616

scaleapi/__init__.py

Lines changed: 19 additions & 4 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:
@@ -195,7 +201,7 @@ def create_project(self, project_name, type, params):
195201
projectdata = self._postrequest('projects', payload)
196202
return Project(projectdata, self)
197203

198-
def get_projet(self, project_name):
204+
def get_project(self, project_name):
199205
projectdata = self._getrequest('projects/%s' % project_name)
200206
return Project(projectdata, self)
201207

@@ -209,9 +215,18 @@ def update_project(self, project_name, **kwargs):
209215
if key not in allowed_kwargs:
210216
raise ScaleInvalidRequest('Illegal parameter %s for ScaleClient.update_project()'
211217
% key, None)
212-
projectdata = self._postrequest('projects/%s/setParams' % project_name)
218+
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):

scaleapi/_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "1.0.3"

setup.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import warnings
3+
import os.path
34

45
try:
56
from setuptools import setup
@@ -25,10 +26,22 @@
2526
install_requires.append('idna')
2627
install_requires.append('requests[security]')
2728

29+
def read(rel_path):
30+
here = os.path.abspath(os.path.dirname(__file__))
31+
with open(os.path.join(here, rel_path), 'r') as fp:
32+
return fp.read()
33+
34+
def get_version(rel_path):
35+
for line in read(rel_path).splitlines():
36+
if line.startswith('__version__'):
37+
delim = '"' if '"' in line else "'"
38+
return line.split(delim)[1]
39+
raise RuntimeError("Unable to find a valid __version__ string in %s." % rel_path)
40+
2841
setup(
2942
name='scaleapi',
3043
packages=['scaleapi'],
31-
version='1.0.2',
44+
version=get_version("scaleapi/_version.py"),
3245
description='The official Python client library for Scale AI, the Data Platform for AI',
3346
author='Scale AI',
3447
author_email='support@scale.com',

0 commit comments

Comments
 (0)