From 307a7f9077546bdaf3273a1f3500766c1b4fbf69 Mon Sep 17 00:00:00 2001 From: Ward Van Heddeghem Date: Fri, 5 Jan 2024 20:53:47 +0100 Subject: [PATCH] Refactor: abstract class reuse --- src/mijnbib/login_handlers.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/mijnbib/login_handlers.py b/src/mijnbib/login_handlers.py index 0e97e09..a48fb00 100644 --- a/src/mijnbib/login_handlers.py +++ b/src/mijnbib/login_handlers.py @@ -19,18 +19,18 @@ class LoginHandler(ABC): - @abstractmethod - def login(self) -> mechanize.Browser: - pass - - -class LoginByForm(LoginHandler): def __init__(self, username, password, url: str, br: mechanize.Browser): self._username = username self._pwd = password self._url = url self._br = br + @abstractmethod + def login(self) -> mechanize.Browser: + pass + + +class LoginByForm(LoginHandler): def login(self) -> mechanize.Browser: response = self._log_in() html = response.read().decode("utf-8") if response is not None else "" @@ -59,7 +59,8 @@ def _log_in(self): ) from e return response - def _validate_logged_in(self, html: str): + @staticmethod + def _validate_logged_in(html: str): _log.debug("Checking if login is successful ...") if "Profiel" not in html: if ( @@ -75,11 +76,8 @@ def _validate_logged_in(self, html: str): class LoginByOAuth(LoginHandler): - def __init__(self, username, password, url: str, br: mechanize.Browser): - self._username = username - self._pwd = password - self._br = br - self._url = url # e.g. "https://gent.bibliotheek.be/mijn-bibliotheek/aanmelden" + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self._s = requests.Session() # self._s.cookies = self._br.cookiejar # load cookies from earlier session(s)