Skip to content

Commit

Permalink
Refactor URLLibURLDownloader with better separation / abstraction.
Browse files Browse the repository at this point in the history
  • Loading branch information
joanbm committed Jan 1, 2024
1 parent 5cef0b0 commit 9c1205a
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions full_offline_backup_for_todoist/url_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

0 comments on commit 9c1205a

Please sign in to comment.