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

Commit

Permalink
compress code with handler function for response
Browse files Browse the repository at this point in the history
  • Loading branch information
blankdots committed Jan 26, 2021
1 parent f64069a commit 860ef3d
Showing 1 changed file with 27 additions and 84 deletions.
111 changes: 27 additions & 84 deletions swift_x_account_sharing/bindings/bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ async def __aexit__(self, *excinfo: BaseException) -> None:
"""."""
await self.session.close()

async def _handler_response(
self,
resp: aiohttp.ClientResponse
) -> typing.Any:
"""Handle API response."""
if resp.status == 200:
try:
return json.loads(await resp.text())
except json.decoder.JSONDecodeError:
logging.error("Decoding JSON error \
response was not possible.")
raise
except Exception as e:
logging.error(f"Unknown exception \
occured with content: {e}.")
raise
else:
logging.error(f"response status: {resp.status}.")
raise Exception(f"response status: {resp.status}.")

@staticmethod
def parse_list_to_string(
to_parse: typing.List[str]
Expand All @@ -58,20 +78,8 @@ async def get_access(
async with self.session.get(url,
params=params,
ssl=ssl_context) as resp:
if resp.status == 200:
try:
return json.loads(await resp.text())
except json.decoder.JSONDecodeError:
logging.error("Decoding JSON error \
response was not possible.")
raise
except Exception as e:
logging.error(f"Unknown exception \
occured with content: {e}.")
raise
else:
logging.error(f"response status: {resp.status}.")
raise Exception(f"response status: {resp.status}.")

return await self._handler_response(resp)

async def get_access_details(
self,
Expand All @@ -89,20 +97,7 @@ async def get_access_details(
async with self.session.get(url,
params=params,
ssl=ssl_context) as resp:
if resp.status == 200:
try:
return json.loads(await resp.text())
except json.decoder.JSONDecodeError:
logging.error("Decoding JSON error \
response was not possible.")
raise
except Exception as e:
logging.error(f"Unknown exception \
occured with content: {e}.")
raise
else:
logging.error(f"response status: {resp.status}.")
raise Exception(f"response status: {resp.status}.")
return await self._handler_response(resp)

async def get_share(
self,
Expand All @@ -117,20 +112,7 @@ async def get_share(
async with self.session.get(url,
params=params,
ssl=ssl_context) as resp:
if resp.status == 200:
try:
return json.loads(await resp.text())
except json.decoder.JSONDecodeError:
logging.error("Decoding JSON error \
response was not possible.")
raise
except Exception as e:
logging.error(f"Unknown exception \
occured with content: {e}.")
raise
else:
logging.error(f"response status: {resp.status}.")
raise Exception(f"response status: {resp.status}.")
return await self._handler_response(resp)

async def get_share_details(
self,
Expand All @@ -146,20 +128,7 @@ async def get_share_details(
async with self.session.get(url,
params=params,
ssl=ssl_context) as resp:
if resp.status == 200:
try:
return json.loads(await resp.text())
except json.decoder.JSONDecodeError:
logging.error("Decoding JSON error \
response was not possible.")
raise
except Exception as e:
logging.error(f"Unknown exception \
occured with content: {e}.")
raise
else:
logging.error(f"response status: {resp.status}.")
raise Exception(f"response status: {resp.status}.")
return await self._handler_response(resp)

async def share_new_access(
self,
Expand All @@ -184,20 +153,7 @@ async def share_new_access(
async with self.session.post(url,
params=params,
ssl=ssl_context) as resp:
if resp.status == 200:
try:
return json.loads(await resp.text())
except json.decoder.JSONDecodeError:
logging.error("Decoding JSON error \
response was not possible.")
raise
except Exception as e:
logging.error(f"Unknown exception \
occured with content: {e}.")
raise
else:
logging.error(f"response status: {resp.status}.")
raise Exception(f"response status: {resp.status}.")
return await self._handler_response(resp)

async def share_edit_access(
self,
Expand All @@ -219,20 +175,7 @@ async def share_edit_access(
async with self.session.patch(url,
params=params,
ssl=ssl_context) as resp:
if resp.status == 200:
try:
return json.loads(await resp.text())
except json.decoder.JSONDecodeError:
logging.error("Decoding JSON error \
response was not possible.")
raise
except Exception as e:
logging.error(f"Unknown exception \
occured with content: {e}.")
raise
else:
logging.error(f"response status: {resp.status}.")
raise Exception(f"response status: {resp.status}.")
return await self._handler_response(resp)

async def share_delete_access(
self,
Expand Down

0 comments on commit 860ef3d

Please sign in to comment.