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

Commit

Permalink
Merge pull request #87 from cpainchaud/main
Browse files Browse the repository at this point in the history
HTTP error code handling
  • Loading branch information
cpainchaud authored Jan 25, 2022
2 parents c75927b + 2abf1e5 commit 52cccf7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 13 additions & 3 deletions reolink/camera_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Dict, List, Optional, Tuple, Any
from . import typings
from .software_version import SoftwareVersion
from .exceptions import CredentialsInvalidError, SnapshotIsNotValidFileTypeError, InvalidContentTypeError
from .exceptions import CredentialsInvalidError, SnapshotIsNotValidFileTypeError, InvalidContentTypeError, ApiError
import traceback
import re

Expand Down Expand Up @@ -1654,16 +1654,21 @@ async def send(self, body, param=None, expected_content_type: Optional[str] = No
async with self._aiohttp_session.get(url=self._url, params=param, allow_redirects=False) as response:
_LOGGER.debug("%s/%s::send() HTTP Request params =%s", self.name, self._host,
str(param).replace(self._password, "<password>"))
json_data = await response.read()

_LOGGER.debug("%s/%s::send() HTTP Response status=%s content-type=(%s)", self.name, self._host,
response.status, response.content_type)

json_data = await response.read()
if param.get("cmd") == "Snap":
_LOGGER_DATA.debug("%s/%s::send() HTTP Response data scrapped because it's too large",
self.name, self._host)
else:
_LOGGER_DATA.debug("%s/%s::send() HTTP Response data: %s", self.name, self._host, json_data)

if response.status >= 400:
raise ApiError(
"API returned HTTP status ERROR code {}/{}".format(response.status, response.reason))

if len(json_data) < 500 and response.content_type == 'text/html':
if b'"detail" : "invalid user"' in json_data or \
b'"detail" : "login failed"' in json_data \
Expand All @@ -1684,15 +1689,20 @@ async def send(self, body, param=None, expected_content_type: Optional[str] = No
str(param).replace(self._password, "<password>"))
_LOGGER.debug("%s/%s::send() HTTP Request body =%s", self.name, self._host,
str(body).replace(self._password, "<password>"))
json_data = await response.text()
_LOGGER.debug("%s/%s::send() HTTP Response status=%s content-type=(%s)", self.name, self._host,
response.status, response.content_type)

json_data = await response.text()
if param.get("cmd") == "Search" and len(json_data) > 500:
_LOGGER_DATA.debug("%s/%s::send() HTTP Response data scrapped because it's too large",
self.name, self._host)
else:
_LOGGER_DATA.debug("%s/%s::send() HTTP Response data: %s", self.name, self._host, json_data)

if response.status >= 400:
raise ApiError(
"API returned HTTP status ERROR code {}/{}".format(response.status, response.reason))

if len(json_data) < 500 and response.content_type == 'text/html':
if 'detail" : "invalid user' in json_data or 'detail" : "login failed' in json_data \
or 'detail" : "please login first' in json_data:
Expand Down
5 changes: 5 additions & 0 deletions reolink/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ class ReolinkError(Exception):
pass


class ApiError(ReolinkError):
"""Raised when API returns an error code"""
pass


class InvalidContentTypeError(ReolinkError):
"""Raised when Snapshot command returns an invalid JPEG file"""
pass
Expand Down

0 comments on commit 52cccf7

Please sign in to comment.