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

Commit b94e87d

Browse files
committed
compress code with handler function for response
1 parent 0e36ec5 commit b94e87d

1 file changed

Lines changed: 26 additions & 42 deletions

File tree

  • swift_sharing_request/bindings

swift_sharing_request/bindings/bind.py

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

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

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

108104
async def list_owned_requests(
109105
self,
@@ -123,20 +119,8 @@ async def list_owned_requests(
123119
async with self.session.get(url,
124120
params=params,
125121
ssl=ssl_context) as resp:
126-
if resp.status == 200:
127-
try:
128-
return json.loads(await resp.text())
129-
except json.decoder.JSONDecodeError:
130-
logging.error("Decoding JSON error \
131-
response was not possible.")
132-
raise
133-
except Exception as e:
134-
logging.error(f"Unknown exception \
135-
occured with content: {e}.")
136-
raise
137-
else:
138-
logging.error(f"response status: {resp.status}.")
139-
raise Exception(f"response status: {resp.status}.")
122+
123+
return await self._handler_response(resp)
140124

141125
async def list_container_requests(
142126
self,

0 commit comments

Comments
 (0)