Skip to content

Commit

Permalink
Refactor request code
Browse files Browse the repository at this point in the history
  • Loading branch information
rccoleman committed Apr 3, 2021
1 parent 163a4e7 commit 3aef7a0
Showing 1 changed file with 29 additions and 44 deletions.
73 changes: 29 additions & 44 deletions custom_components/channels_dvr_recently_recorded/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,70 +35,55 @@ def version(self):
"""Return the version number of the server software."""
return self._version

async def init_data(self):
"""Get the version number of the server"""
status_uri = f"http://{self.host}:{self.port}{STATUS_ENDPOINT}"
async def request_data(self, uri):
"""Request data from an endpoint."""
try:
resp = await self.request(status_uri)
json_string = resp.decode("utf-8")
status = json.loads(json_string)
except OSError:
resp = await self.request(uri)
except (OSError, AttributeError):
msg = "Host %s is not available" % self.host
_LOGGER.warning(msg)
_LOGGER.error(msg)
raise ConnectionFail(msg)

self._version = {VERSION: status[VERSION]}

async def get_files(self):
"""Get the list of files from the server."""
files_uri = f"http://{self.host}:{self.port}{FILES_ENDPOINT}"
_LOGGER.debug(f"Updating")
except Exception:
msg = "Unexpected exception"
_LOGGER.exception(msg)
raise ConnectionFail(msg)

"""Retrieve recorded show info and server version"""
try:
resp = await self.request(files_uri)
if resp is not None:
json_string = resp.decode("utf-8")
files = json.loads(json_string)
if not files:
msg = "%s cannot be reached" % self.host
data = json.loads(json_string)
if not data:
msg = "Response cannot be decoded"
_LOGGER.warning(msg)
raise ConnectionFail(msg)

except OSError:
else:
msg = "%s cannot be reached" % self.host
_LOGGER.warning(msg)
raise ConnectionFail(msg)

except JSONDecodeError:
msg = "Couldn't decode data returned from %s" % self.host
_LOGGER.warning(msg)
raise ConnectionFail(msg)
return data

async def init_data(self):
"""Get the version number of the server"""
status_uri = f"http://{self.host}:{self.port}{STATUS_ENDPOINT}"
status = await self.request_data(status_uri)

return files
self._version = {VERSION: status[VERSION]}

async def get_files(self):
"""Get the list of files from the server."""
files_uri = f"http://{self.host}:{self.port}{FILES_ENDPOINT}"
_LOGGER.debug(f"Updating")
return await self.request_data(files_uri)

async def get_poster(self, image_uri):
"""Get the poster for a show from the server."""
return await self.request(image_uri)

async def get_auth(self):
"""Make sure we can reach the server and get the unique ID (verification)"""
try:
auth_uri = f"http://{self.host}:{self.port}{AUTH_ENDPOINT}"

resp = await self.request(auth_uri)
json_string = resp.decode("utf-8")
auth = json.loads(json_string)
except (OSError, AttributeError):
msg = "Host %s is not available" % self.host
_LOGGER.error(msg)
raise ConnectionFail(msg)

except Exception:
msg = "Unexpected exception"
_LOGGER.exception(msg)
raise ConnectionFail(msg)

return auth
auth_uri = f"http://{self.host}:{self.port}{AUTH_ENDPOINT}"
return await self.request_data(auth_uri)


class ConnectionFail(Exception):
Expand Down

0 comments on commit 3aef7a0

Please sign in to comment.