Skip to content

Commit d9c0548

Browse files
committed
ENH: refactoring of the API implementations and tests
1 parent 8746191 commit d9c0548

File tree

5 files changed

+110
-74
lines changed

5 files changed

+110
-74
lines changed

src/save_and_restore_api/_api_async.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,22 @@ async def nodes_get(self, uniqueIds):
5858
method, url, params = self._prepare_nodes_get(uniqueIds=uniqueIds)
5959
return await self.send_request(method, url, params=params)
6060

61-
async def node_add(self, parentNodeId, *, name, nodeType, **kwargs):
61+
async def node_add(self, parentNodeId, *, name, nodeType, auth=None, **kwargs):
6262
# Reusing docstrings from the threaded version
6363
method, url, params = self._prepare_node_add(
6464
parentNodeId=parentNodeId, name=name, nodeType=nodeType, **kwargs
6565
)
66-
return await self.send_request(method, url, params=params)
66+
return await self.send_request(method, url, params=params, auth=auth)
6767

68-
async def node_delete(self, nodeId):
68+
async def node_delete(self, nodeId, *, auth=None):
6969
# Reusing docstrings from the threaded version
7070
method, url = self._prepare_node_delete(nodeId=nodeId)
71-
return await self.send_request(method, url)
71+
return await self.send_request(method, url, auth=auth)
7272

73-
async def nodes_delete(self, uniqueIds):
73+
async def nodes_delete(self, uniqueIds, *, auth=None):
7474
# Reusing docstrings from the threaded version
7575
method, url, params = self._prepare_nodes_delete(uniqueIds=uniqueIds)
76-
return await self.send_request(method, url, params=params)
76+
return await self.send_request(method, url, params=params, auth=auth)
7777

7878
async def node_get_children(self, uniqueNodeId):
7979
# Reusing docstrings from the threaded version

src/save_and_restore_api/_api_base.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,26 @@ def __init__(self, *, base_url, timeout, request_fail_exceptions=True):
5252
self._auth = None
5353

5454
@staticmethod
55-
def gen_auth(username, password):
55+
def auth_gen(username, password):
56+
"""
57+
Generate and return httpx.BasicAuth object based on username and password.
58+
The object can be passed as ``auth`` parameter in API calls.
59+
"""
5660
return httpx.BasicAuth(username=username, password=password)
5761

58-
def set_auth(self, *, username, password):
59-
self._auth = self.gen_auth(username=username, password=password)
62+
def auth_set(self, *, username, password):
63+
"""
64+
Configure authentication for the session based on username and password.
65+
If the authentication is configured, there is no need to pass the authentication
66+
object with each API call.
67+
"""
68+
self._auth = self.auth_gen(username=username, password=password)
69+
70+
def auth_clear(self):
71+
"""
72+
Clear authentication for the session.
73+
"""
74+
self._auth = None
6075

6176
# def set_username_password(self, username=None, password=None):
6277
# if not isinstance(username, str):
@@ -132,7 +147,7 @@ def _prepare_request(
132147
kwargs.update({"data": data})
133148
if timeout is not None:
134149
kwargs.update({"timeout": self._adjust_timeout(timeout)})
135-
if method != "GET":
150+
if method.upper() != "GET":
136151
auth = auth or self._auth
137152
if auth is not None:
138153
kwargs.update({"auth": auth})

src/save_and_restore_api/_api_threads.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def nodes_get(self, uniqueIds):
6565
method, url, params = self._prepare_nodes_get(uniqueIds=uniqueIds)
6666
return self.send_request(method, url, params=params)
6767

68-
def node_add(self, parentNodeId, *, name, nodeType, **kwargs):
68+
def node_add(self, parentNodeId, *, name, nodeType, auth=None, **kwargs):
6969
"""
7070
Creates a new node under the specified parent node. Required parameters:
7171
``name`` and ``nodeType``. Supported types: ``"FOLDER"``, ``"CONFIGURATION"``.
@@ -75,27 +75,27 @@ def node_add(self, parentNodeId, *, name, nodeType, **kwargs):
7575
method, url, params = self._prepare_node_add(
7676
parentNodeId=parentNodeId, name=name, nodeType=nodeType, **kwargs
7777
)
78-
return self.send_request(method, url, params=params)
78+
return self.send_request(method, url, params=params, auth=auth)
7979

80-
def node_delete(self, nodeId):
80+
def node_delete(self, nodeId, *, auth=None):
8181
"""
8282
Deletes the node with specified node ID. The call fails if the node can
8383
not be deleted.
8484
8585
API: DELETE /node/{nodeId}
8686
"""
8787
method, url = self._prepare_node_delete(nodeId=nodeId)
88-
return self.send_request(method, url)
88+
return self.send_request(method, url, auth=auth)
8989

90-
def nodes_delete(self, uniqueIds):
90+
def nodes_delete(self, uniqueIds, *, auth=None):
9191
"""
9292
Deletes multiple nodes specified as a list of UIDs. The call fails if
9393
any of the nodes can not be deleted.
9494
9595
API: DELETE /node
9696
"""
9797
method, url, params = self._prepare_nodes_delete(uniqueIds=uniqueIds)
98-
return self.send_request(method, url, params=params)
98+
return self.send_request(method, url, params=params, auth=auth)
9999

100100
def node_get_children(self, uniqueNodeId):
101101
"""

tests/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _clear():
2929
Remove all nodes from the database.
3030
"""
3131
with SaveRestoreAPI(base_url=base_url, timeout=2) as SR:
32-
SR.set_auth(username=user_username, password=user_password)
32+
SR.auth_set(username=user_username, password=user_password)
3333

3434
# Create all nodes. Children always follow the parent
3535
n_uid = 0

0 commit comments

Comments
 (0)