1
1
"""Sample API Client."""
2
+
2
3
from __future__ import annotations
3
4
4
5
import socket
6
+ from typing import Any
5
7
6
8
import aiohttp
7
9
import async_timeout
@@ -12,17 +14,27 @@ class IntegrationBlueprintApiClientError(Exception):
12
14
13
15
14
16
class IntegrationBlueprintApiClientCommunicationError (
15
- IntegrationBlueprintApiClientError
17
+ IntegrationBlueprintApiClientError ,
16
18
):
17
19
"""Exception to indicate a communication error."""
18
20
19
21
20
22
class IntegrationBlueprintApiClientAuthenticationError (
21
- IntegrationBlueprintApiClientError
23
+ IntegrationBlueprintApiClientError ,
22
24
):
23
25
"""Exception to indicate an authentication error."""
24
26
25
27
28
+ def _verify_response_or_raise (response : aiohttp .ClientResponse ) -> None :
29
+ """Verify that the response is valid."""
30
+ if response .status in (401 , 403 ):
31
+ msg = "Invalid credentials"
32
+ raise IntegrationBlueprintApiClientAuthenticationError (
33
+ msg ,
34
+ )
35
+ response .raise_for_status ()
36
+
37
+
26
38
class IntegrationBlueprintApiClient :
27
39
"""Sample API Client."""
28
40
@@ -37,13 +49,14 @@ def __init__(
37
49
self ._password = password
38
50
self ._session = session
39
51
40
- async def async_get_data (self ) -> any :
52
+ async def async_get_data (self ) -> Any :
41
53
"""Get data from the API."""
42
54
return await self ._api_wrapper (
43
- method = "get" , url = "https://jsonplaceholder.typicode.com/posts/1"
55
+ method = "get" ,
56
+ url = "https://jsonplaceholder.typicode.com/posts/1" ,
44
57
)
45
58
46
- async def async_set_title (self , value : str ) -> any :
59
+ async def async_set_title (self , value : str ) -> Any :
47
60
"""Get data from the API."""
48
61
return await self ._api_wrapper (
49
62
method = "patch" ,
@@ -58,7 +71,7 @@ async def _api_wrapper(
58
71
url : str ,
59
72
data : dict | None = None ,
60
73
headers : dict | None = None ,
61
- ) -> any :
74
+ ) -> Any :
62
75
"""Get information from the API."""
63
76
try :
64
77
async with async_timeout .timeout (10 ):
@@ -68,22 +81,21 @@ async def _api_wrapper(
68
81
headers = headers ,
69
82
json = data ,
70
83
)
71
- if response .status in (401 , 403 ):
72
- raise IntegrationBlueprintApiClientAuthenticationError (
73
- "Invalid credentials" ,
74
- )
75
- response .raise_for_status ()
84
+ _verify_response_or_raise (response )
76
85
return await response .json ()
77
86
78
87
except TimeoutError as exception :
88
+ msg = f"Timeout error fetching information - { exception } "
79
89
raise IntegrationBlueprintApiClientCommunicationError (
80
- "Timeout error fetching information" ,
90
+ msg ,
81
91
) from exception
82
92
except (aiohttp .ClientError , socket .gaierror ) as exception :
93
+ msg = f"Error fetching information - { exception } "
83
94
raise IntegrationBlueprintApiClientCommunicationError (
84
- "Error fetching information" ,
95
+ msg ,
85
96
) from exception
86
97
except Exception as exception : # pylint: disable=broad-except
98
+ msg = f"Something really wrong happened! - { exception } "
87
99
raise IntegrationBlueprintApiClientError (
88
- "Something really wrong happened!"
100
+ msg ,
89
101
) from exception
0 commit comments