Skip to content

Commit 7636ef7

Browse files
committed
ENH: send auth with client.request
1 parent f54c3ef commit 7636ef7

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/save_and_restore_api/_api.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,20 @@ def __init__(self, *, base_url, timeout, request_fail_exceptions=True):
4747
self._timeout = timeout
4848
self._client = None
4949
self._root_node_uid = "44bef5de-e8e6-4014-af37-b8f6c8a939a2"
50-
51-
self._username = None
52-
self._password = None
53-
# self._username = "***REMOVED***"
54-
# self._password = "***REMOVED***"
50+
self._auth = None
5551

5652
@property
5753
def ROOT_NODE_UID(self):
5854
return self._root_node_uid
5955

56+
def gen_auth(username, password):
57+
return httpx.BasicAuth(username=username, password=password)
58+
59+
def set_auth(self, *, username, password):
60+
self._auth = self.gen_auth(username=username, password=password)
61+
6062
def open(self):
61-
auth = httpx.BasicAuth(username=self._username, password=self._password)
62-
self._client = httpx.Client(base_url=self._base_url, timeout=self._timeout, auth=auth)
63+
self._client = httpx.Client(base_url=self._base_url, timeout=self._timeout)
6364

6465
def close(self):
6566
self._client.close()
@@ -123,7 +124,9 @@ def _process_comm_exception(self, *, method, params, client_response):
123124
else:
124125
raise self.HTTPServerError(exc, **common_params) from exc
125126

126-
def send_request(self, method, url, *, params=None, url_params=None, headers=None, data=None, timeout=None):
127+
def send_request(
128+
self, method, url, *, params=None, url_params=None, headers=None, data=None, timeout=None, auth=None
129+
):
127130
try:
128131
client_response = None
129132
kwargs = {}
@@ -137,6 +140,10 @@ def send_request(self, method, url, *, params=None, url_params=None, headers=Non
137140
kwargs.update({"data": data})
138141
if timeout is not None:
139142
kwargs.update({"timeout": self._adjust_timeout(timeout)})
143+
if method != "GET":
144+
auth = auth or self._auth
145+
if auth is not None:
146+
kwargs.update({"auth": auth})
140147
client_response = self._client.request(method, url, **kwargs)
141148
response = self._process_response(client_response=client_response)
142149
except Exception:

0 commit comments

Comments
 (0)