Skip to content

Commit

Permalink
Make ApiClient more pyodide friendly (#114)
Browse files Browse the repository at this point in the history
## Changes
Embed `requests.Session` into ApiClient instead of extending it, so that
`pyodide-http` [picks it
up](https://github.com/koenvo/pyodide-http#supported-packages).

## Tests

- [x] `make test` run locally
- [ ] `make fmt` applied
- [x] relevant integration tests applied
  • Loading branch information
nfx committed May 17, 2023
1 parent 1135557 commit afeb4f5
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions databricks/sdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,12 +717,14 @@ def __init__(self,
self.kwargs = kwargs


class ApiClient(requests.Session):
class ApiClient:
_cfg: Config

def __init__(self, cfg: Config = None):
super().__init__()
self._cfg = Config() if not cfg else cfg
self._debug_truncate_bytes = cfg.debug_truncate_bytes if cfg.debug_truncate_bytes else 96
self._user_agent_base = cfg.user_agent

retry_strategy = Retry(
total=6,
backoff_factor=1,
Expand All @@ -731,11 +733,10 @@ def __init__(self, cfg: Config = None):
respect_retry_after_header=True,
raise_on_status=False, # return original response when retries have been exhausted
)
self._debug_truncate_bytes = cfg.debug_truncate_bytes if cfg.debug_truncate_bytes else 96
self._user_agent_base = cfg.user_agent
self.auth = self._authenticate

self.mount("https://", HTTPAdapter(max_retries=retry_strategy))
self._session = requests.Session()
self._session.auth = self._authenticate
self._session.mount("https://", HTTPAdapter(max_retries=retry_strategy))

@property
def account_id(self) -> str:
Expand All @@ -753,7 +754,7 @@ def _authenticate(self, r: requests.PreparedRequest) -> requests.PreparedRequest

def do(self, method: str, path: str, query: dict = None, body: dict = None) -> dict:
headers = {'Accept': 'application/json', 'User-Agent': self._user_agent_base}
response = self.request(method, f"{self._cfg.host}{path}", params=query, json=body, headers=headers)
response = self._session.request(method, f"{self._cfg.host}{path}", params=query, json=body, headers=headers)
try:
self._record_request_log(response)
if not response.ok:
Expand Down

0 comments on commit afeb4f5

Please sign in to comment.