From 513944ff97d2ca9612cca2462b43e5c43fe94c46 Mon Sep 17 00:00:00 2001 From: Fatih Kurtoglu Date: Tue, 9 May 2023 15:40:02 -0700 Subject: [PATCH] adding proxy support to SDK --- README.rst | 40 +++++++++++++++++++++++++++++----------- scaleapi/__init__.py | 13 ++++++++++++- scaleapi/_version.py | 2 +- scaleapi/api.py | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/README.rst b/README.rst index 661a58e..2b8ed91 100644 --- a/README.rst +++ b/README.rst @@ -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 _____ @@ -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"] @@ -600,7 +618,7 @@ The attribute can be passed to the task payloads, in the ``attachment`` paramete ... ... ) - + Manage Teammates ________________ @@ -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 ^^^^^^^^^^^^^^^^^^^^^ @@ -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 @@ -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 @@ -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) _____________________________________________ @@ -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 @@ -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. @@ -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) _______________________________________ @@ -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 diff --git a/scaleapi/__init__.py b/scaleapi/__init__.py index 7e4368e..15e3565 100644 --- a/scaleapi/__init__.py +++ b/scaleapi/__init__.py @@ -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: diff --git a/scaleapi/_version.py b/scaleapi/_version.py index 1860cc4..89429b5 100644 --- a/scaleapi/_version.py +++ b/scaleapi/_version.py @@ -1,2 +1,2 @@ -__version__ = "2.13.0" +__version__ = "2.14.0" __package_name__ = "scaleapi" diff --git a/scaleapi/api.py b/scaleapi/api.py index a3c4e0d..106bb6e 100644 --- a/scaleapi/api.py +++ b/scaleapi/api.py @@ -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.") @@ -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, @@ -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( @@ -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 @@ -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: