Skip to content

Commit

Permalink
Merge pull request #126 from gjohansson-ST/fix_empty_response
Browse files Browse the repository at this point in the history
Fix empty response
  • Loading branch information
gjohansson-ST committed Oct 4, 2022
2 parents c478957 + 7f2cc71 commit ddc36d9
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions custom_components/sector/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import asyncio
from datetime import datetime, timedelta
from typing import Any
import async_timeout

import aiohttp

Expand Down Expand Up @@ -322,7 +323,8 @@ async def _request(
if self._access_token is None:
LOGGER.debug("Access token None, trying to refresh")
try:
await self._login()
async with async_timeout.timeout(TIMEOUT):
await self._login()
except aiohttp.ContentTypeError as error:
LOGGER.error(
"ContentTypeError connecting to Sector: %s, %s ",
Expand All @@ -331,7 +333,7 @@ async def _request(
exc_info=True,
)
except asyncio.TimeoutError as error:
LOGGER.warning("Timeout on during login %s", str(error))
LOGGER.warning("Timeout during login %s", str(error))
except Exception as error: # pylint: disable=broad-except
if retry == 0:
LOGGER.error(
Expand Down Expand Up @@ -359,24 +361,26 @@ async def _request(
"Content-Type": "application/json",
}
try:
if json_data:
LOGGER.debug("Request with post: %s and data %s", url, json_data)
response = await self.websession.post(
url,
json=json_data,
headers=headers,
timeout=TIMEOUT,
)
else:
LOGGER.debug("Request with get: %s", url)
response = await self.websession.get(
url,
headers=headers,
timeout=TIMEOUT,
)
async with async_timeout.timeout(TIMEOUT):
if json_data:
LOGGER.debug("Request with post: %s and data %s", url, json_data)
response = await self.websession.post(
url,
json=json_data,
headers=headers,
timeout=TIMEOUT,
)
else:
LOGGER.debug("Request with get: %s", url)
response = await self.websession.get(
url,
headers=headers,
timeout=TIMEOUT,
)

except asyncio.TimeoutError as error:
LOGGER.warning("Timeout on during login %s", str(error))
LOGGER.warning("Timeout during fetching %s with data %s", url, json_data)
return None
except aiohttp.ContentTypeError as error:
LOGGER.debug("ContentTypeError: %s", error.message)
if "unauthorized" in error.message.lower():
Expand Down Expand Up @@ -422,22 +426,22 @@ async def _login(self) -> None:
"""Login to retrieve access token."""

LOGGER.debug("Trying to login")
response = await self.websession.post(
f"{API_URL}/Login/Login",
headers={
"API-Version": "6",
"Platform": "iOS",
"User-Agent": " SectorAlarm/387 CFNetwork/1206 Darwin/20.1.0",
"Version": "2.0.27",
"Connection": "keep-alive",
"Content-Type": "application/json",
},
json={
"UserId": self._userid,
"Password": self._password,
},
timeout=TIMEOUT,
)
async with async_timeout.timeout(TIMEOUT):
response = await self.websession.post(
f"{API_URL}/Login/Login",
headers={
"API-Version": "6",
"Platform": "iOS",
"User-Agent": " SectorAlarm/387 CFNetwork/1206 Darwin/20.1.0",
"Version": "2.0.27",
"Connection": "keep-alive",
"Content-Type": "application/json",
},
json={
"UserId": self._userid,
"Password": self._password,
},
)

response_text = await response.text()

Expand All @@ -450,7 +454,7 @@ async def _login(self) -> None:
LOGGER.debug("Response status ok: %s", token_data)
self._access_token = token_data.get("AuthorizationToken")

LOGGER.debug("Final exit login")
LOGGER.debug("Exiting login")
LOGGER.debug("Status: %d", response.status)
LOGGER.debug("Text: %s", response_text)

Expand Down

0 comments on commit ddc36d9

Please sign in to comment.