Skip to content

Commit 8e248f2

Browse files
authored
Enable client to operate against different API instances (#41)
1 parent 5c8e316 commit 8e248f2

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

scaleapi/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ class Batchlist(Paginator[Batch]):
4444
class ScaleClient:
4545
"""Main class serves as an interface for Scale API"""
4646

47-
def __init__(self, api_key, source=None):
48-
self.api = Api(api_key, source)
47+
def __init__(self, api_key, source=None, api_instance_url=None):
48+
self.api = Api(
49+
api_key, user_agent_extension=source, api_instance_url=api_instance_url
50+
)
4951

5052
def get_task(self, task_id: str) -> Task:
5153
"""Fetches a task.

scaleapi/api.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from ._version import __package_name__, __version__
88
from .exceptions import ExceptionMap, ScaleException
99

10-
SCALE_ENDPOINT = "https://api.scale.com/v1"
10+
SCALE_API_BASE_URL_V1 = "https://api.scale.com/v1"
1111

1212
# Parameters for HTTP retry
1313
HTTP_TOTAL_RETRIES = 3 # Number of total retries
@@ -19,7 +19,7 @@
1919
class Api:
2020
"""Internal Api reference for handling http operations"""
2121

22-
def __init__(self, api_key, user_agent_extension=None):
22+
def __init__(self, api_key, user_agent_extension=None, api_instance_url=None):
2323
if api_key == "" or api_key is None:
2424
raise ScaleException("Please provide a valid API Key.")
2525

@@ -33,6 +33,7 @@ def __init__(self, api_key, user_agent_extension=None):
3333
self._headers_multipart_form_data = {
3434
"User-Agent": self._generate_useragent(user_agent_extension),
3535
}
36+
self.base_api_url = api_instance_url or SCALE_API_BASE_URL_V1
3637

3738
@staticmethod
3839
def _http_request(
@@ -101,7 +102,7 @@ def _api_request(
101102
):
102103
"""Generic HTTP request method with error handling."""
103104

104-
url = f"{SCALE_ENDPOINT}/{endpoint}"
105+
url = f"{self.base_api_url}/{endpoint}"
105106

106107
res = self._http_request(method, url, headers, auth, params, body, files, data)
107108

0 commit comments

Comments
 (0)