Skip to content

Commit c0825fb

Browse files
committed
ENH: implementation of 'login' function
1 parent 09384b5 commit c0825fb

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

src/save_and_restore_api/_api_async.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ async def send_request(
3131
response = self._process_comm_exception(method=method, params=params, client_response=client_response)
3232

3333
return response
34+
35+
async def login(self, *, username=None, password=None):
36+
method, url, params = await self._prepare_login(username=username, password=password)
37+
self.send_request(method, url, params=params)

src/save_and_restore_api/_api_base.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import getpass
1+
# import getpass
22
import pprint
33
from collections.abc import Mapping
44

55
import httpx
66

7+
rest_api_method_map = {
8+
"login": ("POST", "/login"),
9+
}
10+
711

812
class RequestParameterError(Exception): ...
913

@@ -53,21 +57,22 @@ def __init__(self, *, base_url, timeout, request_fail_exceptions=True):
5357
def ROOT_NODE_UID(self):
5458
return self._root_node_uid
5559

60+
@staticmethod
5661
def gen_auth(username, password):
5762
return httpx.BasicAuth(username=username, password=password)
5863

5964
def set_auth(self, *, username, password):
6065
self._auth = self.gen_auth(username=username, password=password)
6166

62-
def set_username_password(self, username=None, password=None):
63-
if not isinstance(username, str):
64-
print("Username: ", end="")
65-
username = input()
66-
if not isinstance(password, str):
67-
password = getpass.getpass()
67+
# def set_username_password(self, username=None, password=None):
68+
# if not isinstance(username, str):
69+
# print("Username: ", end="")
70+
# username = input()
71+
# if not isinstance(password, str):
72+
# password = getpass.getpass()
6873

69-
self._username = username
70-
self._password = password
74+
# self._username = username
75+
# self._password = password
7176

7277
# # TODO: rewrite the logic in this function
7378
# def _check_response(self, *, request, response):
@@ -137,9 +142,10 @@ def _prepare_request(
137142
kwargs.update({"auth": auth})
138143
return kwargs
139144

140-
def login(self, *, username=None, password=None):
141-
params = {"username": self._username, "password": self._password}
142-
self.send_request("POST", "/login", params=params)
145+
def _prepare_login(self, *, username=None, password=None):
146+
method, url = rest_api_method_map["login"]
147+
params = {"username": username, "password": password}
148+
return method, url, params
143149

144150
def get_node(self, node_uid):
145151
return self.send_request("GET", f"/node/{node_uid}")

src/save_and_restore_api/_api_threads.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ def send_request(
3131
response = self._process_comm_exception(method=method, params=params, client_response=client_response)
3232

3333
return response
34+
35+
def login(self, *, username=None, password=None):
36+
method, url, params = self._prepare_login(username=username, password=password)
37+
self.send_request(method, url, params=params)

tests/test_package.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def test_import():
1717
def test_comm():
1818
SR = SaveRestoreAPI(base_url="http://localhost:8080/save-restore", timeout=2)
1919
# SR.set_username_password(username="johndoe", password="1234")
20-
SR.set_username_password(username="user", password="userPass")
20+
SR.set_auth(username="user", password="userPass")
2121
# SR.set_username_password(username="admin", password="adminPass")
2222
SR.open()
23-
SR.login()
23+
SR.login(username="user", password="userPass")
2424
SR.get_node(SR.ROOT_NODE_UID)
2525
SR.close()

0 commit comments

Comments
 (0)