Skip to content

Commit

Permalink
localapi: refactor semaphore usage
Browse files Browse the repository at this point in the history
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
  • Loading branch information
Noltari committed Jul 23, 2024
1 parent 93cc42e commit 3dd3cf1
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions aioairzone/localapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,29 +166,30 @@ async def http_request(
"""Device HTTP request."""
_LOGGER.debug("aiohttp request: /%s (params=%s)", path, data)

await self._api_semaphore.acquire()
try:
resp: ClientResponse = await self.aiohttp_session.request(
method,
f"http://{self.options.host}:{self.options.port}/{path}",
data=json.dumps(data),
timeout=HTTP_CALL_TIMEOUT,
)
except ClientConnectorError as err:
raise InvalidHost(err) from err
finally:
self._api_semaphore.release()
async with self._api_semaphore:
try:
resp: ClientResponse = await self.aiohttp_session.request(
method,
f"http://{self.options.host}:{self.options.port}/{path}",
data=json.dumps(data),
timeout=HTTP_CALL_TIMEOUT,
)
except ClientConnectorError as err:
raise InvalidHost(err) from err

try:
resp_json = await resp.json(content_type=None)
except JSONDecodeError as err:
raise InvalidHost(err) from err
except UnicodeDecodeError:
# Workaround bogus encoding
try:
resp_json = await resp.json(encoding="iso-8859-1", content_type=None)
resp_json = await resp.json(content_type=None)
except JSONDecodeError as err:
raise InvalidHost(err) from err
except UnicodeDecodeError:
# Workaround bogus encoding
try:
resp_json = await resp.json(
encoding="iso-8859-1",
content_type=None,
)
except JSONDecodeError as err:
raise InvalidHost(err) from err

_LOGGER.debug("aiohttp response: %s", resp_json)
if resp.status != 200:
Expand Down

0 comments on commit 3dd3cf1

Please sign in to comment.