diff --git a/full_offline_backup_for_todoist/url_downloader.py b/full_offline_backup_for_todoist/url_downloader.py index 9f67494..94dfab6 100644 --- a/full_offline_backup_for_todoist/url_downloader.py +++ b/full_offline_backup_for_todoist/url_downloader.py @@ -13,11 +13,23 @@ class URLDownloader(metaclass=ABCMeta): """ Implementation of a class to download the contents of an URL """ _tracer: Tracer - __bearer_token: Optional[str] + _bearer_token: Optional[str] def __init__(self, tracer: Tracer): self._tracer = tracer - self.__bearer_token = None + self._bearer_token = None + + def set_bearer_token(self, bearer_token: Optional[str]) -> None: + """ Sets the value of the 'Authorization: Bearer XXX' HTTP header """ + self._bearer_token = bearer_token + + @abstractmethod + def get(self, url: str, data: Optional[Dict[str, str]]=None) -> bytes: + """ Download the contents of the specified URL with a GET request. + You can specify any additional data parameters to pass to the destination. """ + +class URLLibURLDownloader(URLDownloader): + """ Implementation of a class to download the contents of an URL through URLLib """ def _download(self, opener: urllib.request.OpenerDirector, url: str, data: Optional[Dict[str, str]]=None) -> bytes: @@ -38,25 +50,13 @@ def _download_with_retry(self, opener: urllib.request.OpenerDirector, url: str, return self._download(opener, url, data) - def set_bearer_token(self, bearer_token: Optional[str]) -> None: - """ Sets the value of the 'Authorization: Bearer XXX' HTTP header """ - self.__bearer_token = bearer_token - - @abstractmethod - def get(self, url: str, data: Optional[Dict[str, str]]=None) -> bytes: - """ Download the contents of the specified URL with a GET request. - You can specify any additional data parameters to pass to the destination. """ - def _build_opener_with_app_useragent( self, *handlers: urllib.request.BaseHandler) -> urllib.request.OpenerDirector: opener = urllib.request.build_opener(*handlers) opener.addheaders = ([('User-agent', 'full-offline-backup-for-todoist')] + - ([('Authorization', 'Bearer ' + self.__bearer_token)] if self.__bearer_token else [])) + ([('Authorization', 'Bearer ' + self._bearer_token)] if self._bearer_token else [])) return opener -class URLLibURLDownloader(URLDownloader): - """ Implementation of a class to download the contents of an URL through URLLib """ - def get(self, url: str, data: Optional[Dict[str, str]]=None) -> bytes: opener = self._build_opener_with_app_useragent() return self._download_with_retry(opener, url, data)