Skip to content

adding proxy support to SDK #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 29 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ _____

client = scaleapi.ScaleClient("YOUR_API_KEY_HERE")

If you need to use a proxy to connect Scale API, you can feed ``proxies``, ``cert`` and ``verify`` attributes of the python ``requests`` package during the client initialization.
Proxy support is available with SDK version 2.14.0 and beyond.

`Documentation of Proxies usage in requests package`__

__ https://requests.readthedocs.io/en/latest/user/advanced/#proxies

.. code-block:: python

proxies = { 'https': 'http://10.10.1.10:1080' }

client = scaleapi.ScaleClient(
api_key="YOUR_API_KEY_HERE",
proxies=proxies,
cert='/path/client.cert',
verify=True
)

Tasks
_____

Expand Down Expand Up @@ -358,7 +376,7 @@ __ https://docs.scale.com/reference/deleting-tags
# delete a list of tags on a task by specifying task id
tags_to_delete = ["tag1", "tag2"]
task = client.delete_task_tags('30553edd0b6a93f8f05f0fee', tags_to_delete)

# delete a list of tags on a task object
task = client.get_task('30553edd0b6a93f8f05f0fee')
tags_to_delete = ["tag1", "tag2"]
Expand Down Expand Up @@ -600,7 +618,7 @@ The attribute can be passed to the task payloads, in the ``attachment`` paramete
...
...
)

Manage Teammates
________________

Expand Down Expand Up @@ -628,9 +646,9 @@ Returns all teammates in a List of Teammate objects.
.. code-block:: python

from scaleapi import TeammateRole

teammates = client.invite_teammates(['email1@example.com', 'email2@example.com'], TeammateRole.Member)

Update Teammate Role
^^^^^^^^^^^^^^^^^^^^^

Expand All @@ -641,7 +659,7 @@ Returns all teammates in a List of Teammate objects.
.. code-block python

from scaleapi import TeammateRole

teammates = client.update_teammates_role(['email1@example.com', 'email2@example.com'], TeammateRole.Manager)

Example Scripts
Expand Down Expand Up @@ -738,7 +756,7 @@ Manage project assignments for your labelers.
List All Assignments
^^^^^^^^^^^^^^^^^^^^

Lists all your Scale team members and the projects they are assigned to.
Lists all your Scale team members and the projects they are assigned to.
Returns a dictionary of all teammate assignments with keys as 'emails' of each teammate, and values as a list of project names the teammate are assigned to.

.. code-block:: python
Expand Down Expand Up @@ -772,7 +790,7 @@ Returns a dictionary of all teammate assignments with keys as 'emails' of each t
.. code-block:: python

assignments = client.remove_studio_assignments(['email1@example.com', 'email2@example.com'], ['project 1', 'project 2'])

Studio Project Groups (For Scale Studio Only)
_____________________________________________

Expand All @@ -784,7 +802,7 @@ List Studio Project Groups
Returns all labeler groups for the specified project.

.. code-block:: python

list_project_group = client.list_project_groups('project_name')

Add Studio Project Group
Expand All @@ -803,7 +821,7 @@ Returns the created StudioProjectGroup object.
Update Studio Project Group
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Assign or remove teammates from a project group.
Assign or remove teammates from a project group.

Returns the updated StudioProjectGroup object.

Expand All @@ -812,7 +830,7 @@ Returns the updated StudioProjectGroup object.
updated_project_group = client.update_project_group(
'project_name', 'project_group_name', ['emails_to_add'], ['emails_to_remove']
)

Studio Batches (For Scale Studio Only)
_______________________________________

Expand All @@ -835,7 +853,7 @@ Sets labeler group assignment for the specified batch.
Returns a StudioBatch object for the specified batch.

.. code-block:: python

assigned_studio_batch = client.assign_studio_batches('batch_name', ['project_group_name'])

Set Studio Batches Priority
Expand Down
13 changes: 12 additions & 1 deletion scaleapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,22 @@ class Batchlist(Paginator[Batch]):
class ScaleClient:
"""Main class serves as an interface for Scale API"""

def __init__(self, api_key, source=None, api_instance_url=None):
def __init__(
self,
api_key,
source=None,
api_instance_url=None,
verify=None,
proxies=None,
cert=None,
):
self.api = Api(
api_key,
user_agent_extension=source,
api_instance_url=api_instance_url,
verify=verify,
proxies=proxies,
cert=cert,
)

def get_task(self, task_id: str) -> Task:
Expand Down
2 changes: 1 addition & 1 deletion scaleapi/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "2.13.1"
__version__ = "2.14.0"
__package_name__ = "scaleapi"
36 changes: 34 additions & 2 deletions scaleapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@
class Api:
"""Internal Api reference for handling http operations"""

def __init__(self, api_key, user_agent_extension=None, api_instance_url=None):
def __init__(
self,
api_key,
user_agent_extension=None,
api_instance_url=None,
verify=None,
proxies=None,
cert=None,
):
if api_key == "" or api_key is None:
raise ScaleException("Please provide a valid API Key.")

Expand All @@ -35,6 +43,10 @@ def __init__(self, api_key, user_agent_extension=None, api_instance_url=None):
}
self.base_api_url = api_instance_url or SCALE_API_BASE_URL_V1

self.verify = verify
self.proxies = proxies
self.cert = cert

@staticmethod
def _http_request(
method,
Expand All @@ -45,6 +57,9 @@ def _http_request(
body=None,
files=None,
data=None,
verify=None,
proxies=None,
cert=None,
) -> Response:
https = requests.Session()
retry_strategy = Retry(
Expand All @@ -58,6 +73,11 @@ def _http_request(
adapter = HTTPAdapter(max_retries=retry_strategy)
https.mount("https://", adapter)

https.cert = cert if cert else None
https.verify = verify if verify else True
if proxies:
https.proxies.update(proxies)

try:
params = params or {}
body = body or None
Expand Down Expand Up @@ -102,7 +122,19 @@ def _api_request(

url = f"{self.base_api_url}/{endpoint}"

res = self._http_request(method, url, headers, auth, params, body, files, data)
res = self._http_request(
method,
url,
headers,
auth,
params,
body,
files,
data,
verify=self.verify,
proxies=self.proxies,
cert=self.cert,
)

json = None
if res.status_code == 200:
Expand Down