Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit 860ef3d

Browse files
committed
compress code with handler function for response
1 parent f64069a commit 860ef3d

File tree

1 file changed

+27
-84
lines changed
  • swift_x_account_sharing/bindings

1 file changed

+27
-84
lines changed

swift_x_account_sharing/bindings/bind.py

Lines changed: 27 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ async def __aexit__(self, *excinfo: BaseException) -> None:
3535
"""."""
3636
await self.session.close()
3737

38+
async def _handler_response(
39+
self,
40+
resp: aiohttp.ClientResponse
41+
) -> typing.Any:
42+
"""Handle API response."""
43+
if resp.status == 200:
44+
try:
45+
return json.loads(await resp.text())
46+
except json.decoder.JSONDecodeError:
47+
logging.error("Decoding JSON error \
48+
response was not possible.")
49+
raise
50+
except Exception as e:
51+
logging.error(f"Unknown exception \
52+
occured with content: {e}.")
53+
raise
54+
else:
55+
logging.error(f"response status: {resp.status}.")
56+
raise Exception(f"response status: {resp.status}.")
57+
3858
@staticmethod
3959
def parse_list_to_string(
4060
to_parse: typing.List[str]
@@ -58,20 +78,8 @@ async def get_access(
5878
async with self.session.get(url,
5979
params=params,
6080
ssl=ssl_context) as resp:
61-
if resp.status == 200:
62-
try:
63-
return json.loads(await resp.text())
64-
except json.decoder.JSONDecodeError:
65-
logging.error("Decoding JSON error \
66-
response was not possible.")
67-
raise
68-
except Exception as e:
69-
logging.error(f"Unknown exception \
70-
occured with content: {e}.")
71-
raise
72-
else:
73-
logging.error(f"response status: {resp.status}.")
74-
raise Exception(f"response status: {resp.status}.")
81+
82+
return await self._handler_response(resp)
7583

7684
async def get_access_details(
7785
self,
@@ -89,20 +97,7 @@ async def get_access_details(
8997
async with self.session.get(url,
9098
params=params,
9199
ssl=ssl_context) as resp:
92-
if resp.status == 200:
93-
try:
94-
return json.loads(await resp.text())
95-
except json.decoder.JSONDecodeError:
96-
logging.error("Decoding JSON error \
97-
response was not possible.")
98-
raise
99-
except Exception as e:
100-
logging.error(f"Unknown exception \
101-
occured with content: {e}.")
102-
raise
103-
else:
104-
logging.error(f"response status: {resp.status}.")
105-
raise Exception(f"response status: {resp.status}.")
100+
return await self._handler_response(resp)
106101

107102
async def get_share(
108103
self,
@@ -117,20 +112,7 @@ async def get_share(
117112
async with self.session.get(url,
118113
params=params,
119114
ssl=ssl_context) as resp:
120-
if resp.status == 200:
121-
try:
122-
return json.loads(await resp.text())
123-
except json.decoder.JSONDecodeError:
124-
logging.error("Decoding JSON error \
125-
response was not possible.")
126-
raise
127-
except Exception as e:
128-
logging.error(f"Unknown exception \
129-
occured with content: {e}.")
130-
raise
131-
else:
132-
logging.error(f"response status: {resp.status}.")
133-
raise Exception(f"response status: {resp.status}.")
115+
return await self._handler_response(resp)
134116

135117
async def get_share_details(
136118
self,
@@ -146,20 +128,7 @@ async def get_share_details(
146128
async with self.session.get(url,
147129
params=params,
148130
ssl=ssl_context) as resp:
149-
if resp.status == 200:
150-
try:
151-
return json.loads(await resp.text())
152-
except json.decoder.JSONDecodeError:
153-
logging.error("Decoding JSON error \
154-
response was not possible.")
155-
raise
156-
except Exception as e:
157-
logging.error(f"Unknown exception \
158-
occured with content: {e}.")
159-
raise
160-
else:
161-
logging.error(f"response status: {resp.status}.")
162-
raise Exception(f"response status: {resp.status}.")
131+
return await self._handler_response(resp)
163132

164133
async def share_new_access(
165134
self,
@@ -184,20 +153,7 @@ async def share_new_access(
184153
async with self.session.post(url,
185154
params=params,
186155
ssl=ssl_context) as resp:
187-
if resp.status == 200:
188-
try:
189-
return json.loads(await resp.text())
190-
except json.decoder.JSONDecodeError:
191-
logging.error("Decoding JSON error \
192-
response was not possible.")
193-
raise
194-
except Exception as e:
195-
logging.error(f"Unknown exception \
196-
occured with content: {e}.")
197-
raise
198-
else:
199-
logging.error(f"response status: {resp.status}.")
200-
raise Exception(f"response status: {resp.status}.")
156+
return await self._handler_response(resp)
201157

202158
async def share_edit_access(
203159
self,
@@ -219,20 +175,7 @@ async def share_edit_access(
219175
async with self.session.patch(url,
220176
params=params,
221177
ssl=ssl_context) as resp:
222-
if resp.status == 200:
223-
try:
224-
return json.loads(await resp.text())
225-
except json.decoder.JSONDecodeError:
226-
logging.error("Decoding JSON error \
227-
response was not possible.")
228-
raise
229-
except Exception as e:
230-
logging.error(f"Unknown exception \
231-
occured with content: {e}.")
232-
raise
233-
else:
234-
logging.error(f"response status: {resp.status}.")
235-
raise Exception(f"response status: {resp.status}.")
178+
return await self._handler_response(resp)
236179

237180
async def share_delete_access(
238181
self,

0 commit comments

Comments
 (0)