8
8
import logging
9
9
from typing import Any
10
10
11
- from aiohttp import ClientError , ClientResponse , ClientSession , ContentTypeError
11
+ from aiohttp import (
12
+ ClientError ,
13
+ ClientResponse ,
14
+ ClientSession ,
15
+ ClientTimeout ,
16
+ ContentTypeError ,
17
+ )
12
18
13
19
from .const import (
14
20
AUTHORIZATION_HEADER ,
@@ -51,7 +57,9 @@ async def async_get_image(
51
57
try :
52
58
access_token = await self .async_get_access_token ()
53
59
except ClientError as err :
54
- raise ApiError (f"Access token failure: { err } " ) from err
60
+ error_type = type (err ).__name__
61
+ msg = f"Access token failure: { error_type } - { err } "
62
+ raise ApiError (msg ) from err
55
63
headers = {AUTHORIZATION_HEADER : f"Bearer { access_token } " }
56
64
57
65
req_args = {"data" : params if params is not None else {}}
@@ -61,17 +69,20 @@ async def async_get_image(
61
69
url ,
62
70
** req_args , # type: ignore
63
71
headers = headers ,
64
- timeout = timeout ,
72
+ timeout = ClientTimeout ( total = timeout ) ,
65
73
) as resp :
66
74
resp_content = await resp .read ()
67
75
68
76
if resp .headers .get ("content-type" ) == "image/jpeg" :
69
77
return resp_content
70
78
71
- raise ApiError (
79
+ msg = (
72
80
f"{ resp .status } - "
73
81
f"invalid content-type in response"
74
- f"when accessing '{ url } '" ,
82
+ f"when accessing '{ url } '"
83
+ )
84
+ raise ApiError (
85
+ msg ,
75
86
)
76
87
77
88
async def async_post_api_request (
@@ -104,20 +115,21 @@ async def async_post_request(
104
115
105
116
async with self .websession .post (
106
117
url ,
107
- ** req_args ,
118
+ ** req_args , # type: ignore
108
119
headers = headers ,
109
- timeout = timeout ,
120
+ timeout = ClientTimeout ( total = timeout ) ,
110
121
) as resp :
111
122
return await self .process_response (resp , url )
112
123
113
- async def get_access_token (self ):
124
+ async def get_access_token (self ) -> str :
114
125
"""Get access token."""
115
126
try :
116
127
return await self .async_get_access_token ()
117
128
except ClientError as err :
118
- raise ApiError (f"Access token failure: { err } " ) from err
129
+ msg = f"Access token failure: { err } "
130
+ raise ApiError (msg ) from err
119
131
120
- def prepare_request_arguments (self , params ) :
132
+ def prepare_request_arguments (self , params : dict | None ) -> dict :
121
133
"""Prepare request arguments."""
122
134
req_args = {"data" : params if params is not None else {}}
123
135
@@ -131,7 +143,7 @@ def prepare_request_arguments(self, params):
131
143
132
144
return req_args
133
145
134
- async def process_response (self , resp , url ) :
146
+ async def process_response (self , resp : ClientResponse , url : str ) -> ClientResponse :
135
147
"""Process response."""
136
148
resp_status = resp .status
137
149
resp_content = await resp .read ()
@@ -142,7 +154,12 @@ async def process_response(self, resp, url):
142
154
143
155
return await self .handle_success_response (resp , resp_content )
144
156
145
- async def handle_error_response (self , resp , resp_status , url ):
157
+ async def handle_error_response (
158
+ self ,
159
+ resp : ClientResponse ,
160
+ resp_status : int ,
161
+ url : str ,
162
+ ) -> None :
146
163
"""Handle error response."""
147
164
try :
148
165
resp_json = await resp .json ()
@@ -159,19 +176,25 @@ async def handle_error_response(self, resp, resp_status, url):
159
176
raise ApiErrorThrottling (
160
177
message ,
161
178
)
162
- else :
163
- raise ApiError (
164
- message ,
165
- )
179
+ raise ApiError (
180
+ message ,
181
+ )
166
182
167
183
except (JSONDecodeError , ContentTypeError ) as exc :
168
- raise ApiError (
184
+ msg = (
169
185
f"{ resp_status } - "
170
186
f"{ ERRORS .get (resp_status , '' )} - "
171
- f"when accessing '{ url } '" ,
187
+ f"when accessing '{ url } '"
188
+ )
189
+ raise ApiError (
190
+ msg ,
172
191
) from exc
173
192
174
- async def handle_success_response (self , resp , resp_content ):
193
+ async def handle_success_response (
194
+ self ,
195
+ resp : ClientResponse ,
196
+ resp_content : bytes ,
197
+ ) -> ClientResponse :
175
198
"""Handle success response."""
176
199
try :
177
200
if "application/json" in resp .headers .get ("content-type" , []):
@@ -193,7 +216,8 @@ async def async_addwebhook(self, webhook_url: str) -> None:
193
216
params = {"url" : webhook_url },
194
217
)
195
218
except asyncio .exceptions .TimeoutError as exc :
196
- raise ApiError ("Webhook registration timed out" ) from exc
219
+ msg = "Webhook registration timed out"
220
+ raise ApiError (msg ) from exc
197
221
else :
198
222
LOG .debug ("addwebhook: %s" , resp )
199
223
@@ -205,6 +229,7 @@ async def async_dropwebhook(self) -> None:
205
229
params = {"app_types" : "app_security" },
206
230
)
207
231
except asyncio .exceptions .TimeoutError as exc :
208
- raise ApiError ("Webhook registration timed out" ) from exc
232
+ msg = "Webhook registration timed out"
233
+ raise ApiError (msg ) from exc
209
234
else :
210
235
LOG .debug ("dropwebhook: %s" , resp )
0 commit comments