Debian Codegraph @

proton/session/transports/base.py

raw
1 """
2 Copyright (c) 2023 Proton AG
3
4 This file is part of Proton.
5
6 Proton is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Proton is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with ProtonVPN. If not, see <https://www.gnu.org/licenses/>.
18 """
19 import weakref
20 from dataclasses import dataclass
21 from typing import Optional, Any, Tuple
22
23
24 @dataclass
25 class RawResponse:
26 """
27 A response that contains the status code and headers along with the body
28 as json. This gives more context to clients when receiving a response.
29 This type is returned where return_raw is set to True.
30
31 :param status_code: The status code of the response
32 :param headers: The headers in the response
33 :param json: The response parsed as json
34 :param data: The response as a binary blob of bytes
35 """
36 status_code: int
37 headers: Tuple[Tuple[str, Any]]
38 json: Optional[dict]
39 data: Optional[bytes]
40
41 def find_first_header(self, key, default=None):
42 """
43 Searches for the given key in the headers and returns the first value
44 if found, otherwise returns the default value.
45 """
46 for k, v in self.headers:
47 if key == k:
48 return v
49 return default
50
51
52 class Transport:
53 """
54 The base class of all transports. This class should be subclassed to
55 implement the async_api_request method, which is the main method that
56 is used to make requests to the API.
57
58 A transport abstracts away the details of how requests are made to the API,
59 for example, it could be using requests, aiohttp, or any other library.
60 """
61 def __init__(self, session):
62 self.__session = weakref.ref(session)
63
64 @property
65 def _session(self):
66 return self.__session()
67
68 @property
69 def _environment(self):
70 #Shortcut to access environment
71 return self._session.environment
72
73 def __eq__(self, other):
74 # It's the same transport if it's the same type (that's what users would generally assume)
75 return self.__class__ == other.__class__
76
77 async def is_working(self):
78 try:
79 return await self.async_api_request('/tests/ping').get('Code') == '1000'
80 except:
81 return False
82
83 async def async_api_request(
84 self, endpoint,
85 jsondata=None, additional_headers=None,
86 method=None, params=None
87 ):
88 raise NotImplementedError("async_api_request should be implemented")
89
90 class TransportFactory:
91 def __init__(self, cls, *args, **kwargs):
92 self._cls = cls
93 self._args = args
94 self._kwargs = kwargs
95
96 def __call__(self, session):
97 return self._cls(session, *self._args, **self._kwargs)
98
99 def __eq__(self, other):
100 # It's the same transport if it's the same type (that's what users would generally assume)
101 return self._cls == other._cls