diff --git a/scilog/authclient.py b/scilog/authclient.py new file mode 100644 index 0000000..6270c37 --- /dev/null +++ b/scilog/authclient.py @@ -0,0 +1,46 @@ +import getpass +from .config import Config +from .utils import typename + + +AUTH_HEADERS = { + "Content-type": "application/json", + "Accept": "application/json" +} + + +class AuthClient: + + def __init__(self, address): + self.address = address.rstrip("/") + self._token = None + tn = typename(self).lower() + self.config = Config(f".{tn}-tokens") + + def __repr__(self): + tn = typename(self) + return f"{tn} @ {self.address}" + + + @property + def auth_headers(self): + headers = AUTH_HEADERS.copy() + headers["Authorization"] = self.token + return headers + + @property + def token(self): + username = getpass.getuser() + token = self._token + if token is None: + try: + token = self.config[username] + except KeyError: + tn = typename(self) + password = getpass.getpass(prompt=f"{tn} password for {username}: ") + token = self.authenticate(username, password) + self.config[username] = self._token = token + return token + + + diff --git a/scilog/scicat.py b/scilog/scicat.py index 59ca80a..642b97c 100644 --- a/scilog/scicat.py +++ b/scilog/scicat.py @@ -1,24 +1,9 @@ -import getpass -from .config import Config -from .utils import post_request, get_request +from .authclient import AuthClient, AUTH_HEADERS from .autherror import AuthError +from .utils import post_request, get_request -AUTH_HEADERS = { - "Content-type": "application/json", - "Accept": "application/json" -} - - -class SciCat: - - def __init__(self, address): - self.address = address.rstrip("/") - self._token = None - self.config = Config(".scicat-tokens") - - def __repr__(self): - return f"SciCat @ {self.address}" +class SciCat(AuthClient): @property def proposals(self): @@ -26,26 +11,6 @@ class SciCat: headers = self.auth_headers return get_request(url, headers=headers) - - @property - def auth_headers(self): - headers = AUTH_HEADERS.copy() - headers["Authorization"] = self.token - return headers - - @property - def token(self): - username = getpass.getuser() - password = getpass.getpass(prompt=f"SciCat password for {username}: ") - token = self._token - if token is None: - try: - token = self.config[username] - except KeyError: - token = self.authenticate(username, password) - self.config[username] = self._token = token - return token - def authenticate(self, username, password): url = self.address + "/users/login" auth_payload = { diff --git a/scilog/scilog.py b/scilog/scilog.py index 3f20c96..aed1ac7 100644 --- a/scilog/scilog.py +++ b/scilog/scilog.py @@ -1,26 +1,10 @@ -import getpass -from .config import Config -from .utils import post_request, get_request +from .authclient import AuthClient, AUTH_HEADERS from .autherror import AuthError +from .utils import post_request, get_request from .mkfilt import make_filter -AUTH_HEADERS = { - "Content-type": "application/json", - "Accept": "application/json" -} - - -class SciLog: - - def __init__(self, address): - self.address = address.rstrip("/") - self._token = None - self.config = Config(".scilog-tokens") - - def __repr__(self): - return f"SciLog @ {self.address}" - +class SciLog(AuthClient): def get_snippets(self, **kwargs): url = self.address + "/basesnippets" @@ -35,25 +19,6 @@ class SciLog: return post_request(url, payload=payload, headers=headers) - @property - def auth_headers(self): - headers = AUTH_HEADERS.copy() - headers["Authorization"] = self.token - return headers - - @property - def token(self): - username = getpass.getuser() - token = self._token - if token is None: - try: - token = self.config[username] - except KeyError: - password = getpass.getpass(prompt=f"SciLog password for {username}: ") - token = self.authenticate(username, password) - self.config[username] = self._token = token - return token - def authenticate(self, username, password): url = self.address + "/users/login" auth_payload = {