Skip to content

Commit 9c1205a

Browse files
committed
Refactor URLLibURLDownloader with better separation / abstraction.
1 parent 5cef0b0 commit 9c1205a

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

full_offline_backup_for_todoist/url_downloader.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@ class URLDownloader(metaclass=ABCMeta):
1313
""" Implementation of a class to download the contents of an URL """
1414

1515
_tracer: Tracer
16-
__bearer_token: Optional[str]
16+
_bearer_token: Optional[str]
1717

1818
def __init__(self, tracer: Tracer):
1919
self._tracer = tracer
20-
self.__bearer_token = None
20+
self._bearer_token = None
21+
22+
def set_bearer_token(self, bearer_token: Optional[str]) -> None:
23+
""" Sets the value of the 'Authorization: Bearer XXX' HTTP header """
24+
self._bearer_token = bearer_token
25+
26+
@abstractmethod
27+
def get(self, url: str, data: Optional[Dict[str, str]]=None) -> bytes:
28+
""" Download the contents of the specified URL with a GET request.
29+
You can specify any additional data parameters to pass to the destination. """
30+
31+
class URLLibURLDownloader(URLDownloader):
32+
""" Implementation of a class to download the contents of an URL through URLLib """
2133

2234
def _download(self, opener: urllib.request.OpenerDirector, url: str,
2335
data: Optional[Dict[str, str]]=None) -> bytes:
@@ -38,25 +50,13 @@ def _download_with_retry(self, opener: urllib.request.OpenerDirector, url: str,
3850

3951
return self._download(opener, url, data)
4052

41-
def set_bearer_token(self, bearer_token: Optional[str]) -> None:
42-
""" Sets the value of the 'Authorization: Bearer XXX' HTTP header """
43-
self.__bearer_token = bearer_token
44-
45-
@abstractmethod
46-
def get(self, url: str, data: Optional[Dict[str, str]]=None) -> bytes:
47-
""" Download the contents of the specified URL with a GET request.
48-
You can specify any additional data parameters to pass to the destination. """
49-
5053
def _build_opener_with_app_useragent(
5154
self, *handlers: urllib.request.BaseHandler) -> urllib.request.OpenerDirector:
5255
opener = urllib.request.build_opener(*handlers)
5356
opener.addheaders = ([('User-agent', 'full-offline-backup-for-todoist')] +
54-
([('Authorization', 'Bearer ' + self.__bearer_token)] if self.__bearer_token else []))
57+
([('Authorization', 'Bearer ' + self._bearer_token)] if self._bearer_token else []))
5558
return opener
5659

57-
class URLLibURLDownloader(URLDownloader):
58-
""" Implementation of a class to download the contents of an URL through URLLib """
59-
6060
def get(self, url: str, data: Optional[Dict[str, str]]=None) -> bytes:
6161
opener = self._build_opener_with_app_useragent()
6262
return self._download_with_retry(opener, url, data)

0 commit comments

Comments
 (0)