11# import getpass
22import json
3- from collections .abc import Mapping
43from urllib .parse import quote
54
65import httpx
@@ -25,27 +24,16 @@ def __init__(self, msg, request):
2524 super ().__init__ (msg )
2625
2726
28- class RequestFailedError (Exception ):
29- def __init__ (self , request , response ):
30- msg = response .get ("msg" , "" ) if isinstance (response , Mapping ) else str (response )
31- msg = msg or "(no error message)"
32- msg = f"Request failed: { msg } "
33- self .request = request
34- self .response = response
35- super ().__init__ (msg )
36-
37-
3827class _SaveRestoreAPI_Base :
3928 RequestParameterError = RequestParameterError
4029 RequestTimeoutError = RequestTimeoutError
41- RequestFailedError = RequestFailedError
4230 HTTPRequestError = HTTPRequestError
4331 HTTPClientError = HTTPClientError
4432 HTTPServerError = HTTPServerError
4533
4634 ROOT_NODE_UID = "44bef5de-e8e6-4014-af37-b8f6c8a939a2"
4735
48- def __init__ (self , * , base_url , timeout , request_fail_exceptions = True ):
36+ def __init__ (self , * , base_url , timeout = 5.0 ):
4937 self ._base_url = base_url
5038 self ._timeout = timeout
5139 self ._client = None
@@ -55,15 +43,67 @@ def __init__(self, *, base_url, timeout, request_fail_exceptions=True):
5543 def auth_gen (username , password ):
5644 """
5745 Generate and return httpx.BasicAuth object based on username and password.
58- The object can be passed as ``auth`` parameter in API calls.
46+ The object can be passed as a value of ``auth`` parameter in API calls.
47+ Explicitly passing the authentication object may be useful if requests
48+ are made on behalf of multiple users in the same session. If a single user
49+ is authenticated per session, then use ``auth_set()`` to set authentication
50+ once.
51+
52+ Parameters
53+ ----------
54+ username : str
55+ Username.
56+ password : str
57+ Password.
58+
59+ Returns
60+ -------
61+ httpx.BasicAuth
62+ Basic authentication object.
63+
64+ Examples
65+ --------
66+
67+ .. code-block:: python
68+
69+ from save_and_restore_api import SaveRestoreAPI
70+
71+ auth = SaveRestoreAPI.auth_gen(username="user", password="userPass")
5972 """
6073 return httpx .BasicAuth (username = username , password = password )
6174
6275 def auth_set (self , * , username , password ):
6376 """
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.
77+ Set authentication for the session. Once the authentication is configured, the
78+ authentication object is automatically passed with each API call. Calling
79+ this function again overwrites the previous authentication. Use ``auth_clear()``
80+ function to clear authentication.
81+
82+ Parameters
83+ ----------
84+ username : str
85+ Username.
86+ password : str
87+ Password.
88+
89+ Returns
90+ -------
91+ None
92+
93+ Examples
94+ --------
95+
96+ .. code-block:: python
97+
98+ from save_and_restore_api import SaveRestoreAPI
99+
100+ SR = SaveRestoreAPI(base_url="http://localhost:8080/save-restore")
101+ SR.auth_set(username="user", password="userPass")
102+ # ...........
103+ SR.auth_set(username="admin", password="adminPass")
104+ # ...........
105+ SR.auth_clear()
106+
67107 """
68108 self ._auth = self .auth_gen (username = username , password = password )
69109
0 commit comments