@@ -13,11 +13,23 @@ class URLDownloader(metaclass=ABCMeta):
13
13
""" Implementation of a class to download the contents of an URL """
14
14
15
15
_tracer : Tracer
16
- __bearer_token : Optional [str ]
16
+ _bearer_token : Optional [str ]
17
17
18
18
def __init__ (self , tracer : Tracer ):
19
19
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 """
21
33
22
34
def _download (self , opener : urllib .request .OpenerDirector , url : str ,
23
35
data : Optional [Dict [str , str ]]= None ) -> bytes :
@@ -38,25 +50,13 @@ def _download_with_retry(self, opener: urllib.request.OpenerDirector, url: str,
38
50
39
51
return self ._download (opener , url , data )
40
52
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
-
50
53
def _build_opener_with_app_useragent (
51
54
self , * handlers : urllib .request .BaseHandler ) -> urllib .request .OpenerDirector :
52
55
opener = urllib .request .build_opener (* handlers )
53
56
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 []))
55
58
return opener
56
59
57
- class URLLibURLDownloader (URLDownloader ):
58
- """ Implementation of a class to download the contents of an URL through URLLib """
59
-
60
60
def get (self , url : str , data : Optional [Dict [str , str ]]= None ) -> bytes :
61
61
opener = self ._build_opener_with_app_useragent ()
62
62
return self ._download_with_retry (opener , url , data )
0 commit comments