Compare commits
19
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
77959d6d94 | ||
|
|
ad832c4d2d | ||
|
|
0ce2aec139 | ||
|
|
d3761b23ec | ||
|
|
cdbd1a4c27 | ||
|
|
2d2e569595 | ||
|
|
391373b304 | ||
|
|
a030f46b4d | ||
|
|
f6dc22f241 | ||
|
|
220638e525 | ||
|
|
24b1aac1be | ||
|
|
c4608f4258 | ||
|
|
ca19ea62a4 | ||
|
|
2d8f4d8289 | ||
|
|
24ca9d9617 | ||
|
|
41aa1839b7 | ||
|
|
40354e87a0 | ||
|
|
0fc74c3f82 | ||
|
|
7b150eb1af |
+3
-3
@@ -3,7 +3,7 @@
|
||||
import logging
|
||||
from json import dumps
|
||||
from blinkpy.helpers.util import get_time, Throttle
|
||||
from blinkpy.helpers.constants import DEFAULT_URL
|
||||
from blinkpy.helpers.constants import DEFAULT_URL, TIMEOUT
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -284,7 +284,7 @@ def request_motion_detection_disable(blink, network, camera_id):
|
||||
return http_post(blink, url)
|
||||
|
||||
|
||||
def http_get(blink, url, stream=False, json=True, is_retry=False):
|
||||
def http_get(blink, url, stream=False, json=True, is_retry=False, timeout=TIMEOUT):
|
||||
"""
|
||||
Perform an http get request.
|
||||
|
||||
@@ -304,7 +304,7 @@ def http_get(blink, url, stream=False, json=True, is_retry=False):
|
||||
)
|
||||
|
||||
|
||||
def http_post(blink, url, is_retry=False):
|
||||
def http_post(blink, url, is_retry=False, timeout=TIMEOUT):
|
||||
"""
|
||||
Perform an http post request.
|
||||
|
||||
|
||||
+22
-13
@@ -2,9 +2,11 @@
|
||||
import logging
|
||||
from functools import partial
|
||||
from requests import Request, Session, exceptions
|
||||
from requests.adapters import HTTPAdapter
|
||||
from urllib3.util.retry import Retry
|
||||
from blinkpy import api
|
||||
from blinkpy.helpers import util
|
||||
from blinkpy.helpers.constants import BLINK_URL, LOGIN_ENDPOINT
|
||||
from blinkpy.helpers.constants import BLINK_URL, LOGIN_ENDPOINT, TIMEOUT
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -12,7 +14,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
class Auth:
|
||||
"""Class to handle login communication."""
|
||||
|
||||
def __init__(self, login_data=None, no_prompt=False, login_method="v4"):
|
||||
def __init__(self, login_data=None, no_prompt=False):
|
||||
"""
|
||||
Initialize auth handler.
|
||||
|
||||
@@ -22,7 +24,6 @@ class Auth:
|
||||
- password
|
||||
:param no_prompt: Should any user input prompts
|
||||
be supressed? True/FALSE
|
||||
:param login_method: Choose the login endpoint to use. Default: v4. v3 uses email verification rather than a 2FA code.
|
||||
"""
|
||||
if login_data is None:
|
||||
login_data = {}
|
||||
@@ -32,7 +33,6 @@ class Auth:
|
||||
self.region_id = login_data.get("region_id", None)
|
||||
self.client_id = login_data.get("client_id", None)
|
||||
self.account_id = login_data.get("account_id", None)
|
||||
self.login_url = LOGIN_ENDPOINT[login_method]
|
||||
self.login_response = None
|
||||
self.is_errored = False
|
||||
self.no_prompt = no_prompt
|
||||
@@ -58,7 +58,17 @@ class Auth:
|
||||
def create_session(self):
|
||||
"""Create a session for blink communication."""
|
||||
sess = Session()
|
||||
sess.get = partial(sess.get, timeout=10)
|
||||
assert_status_hook = [
|
||||
lambda response, *args, **kwargs: response.raise_for_status()
|
||||
]
|
||||
sess.hooks["response"] = assert_status_hook
|
||||
retry = Retry(
|
||||
total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504]
|
||||
)
|
||||
adapter = HTTPAdapter(max_retries=retry)
|
||||
sess.mount("https://", adapter)
|
||||
sess.mount("http://", adapter)
|
||||
sess.get = partial(sess.get, timeout=TIMEOUT)
|
||||
return sess
|
||||
|
||||
def prepare_request(self, url, headers, data, reqtype):
|
||||
@@ -75,11 +85,11 @@ class Auth:
|
||||
|
||||
self.data = util.validate_login_data(self.data)
|
||||
|
||||
def login(self):
|
||||
def login(self, login_url=LOGIN_ENDPOINT):
|
||||
"""Attempt login to blink servers."""
|
||||
self.validate_login()
|
||||
_LOGGER.info("Attempting login with %s", self.login_url)
|
||||
response = api.request_login(self, self.login_url, self.data, is_retry=False,)
|
||||
_LOGGER.info("Attempting login with %s", login_url)
|
||||
response = api.request_login(self, login_url, self.data, is_retry=False,)
|
||||
try:
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
@@ -142,6 +152,7 @@ class Auth:
|
||||
stream=False,
|
||||
json_resp=True,
|
||||
is_retry=False,
|
||||
timeout=TIMEOUT,
|
||||
):
|
||||
"""
|
||||
Perform server requests.
|
||||
@@ -156,14 +167,11 @@ class Auth:
|
||||
"""
|
||||
req = self.prepare_request(url, headers, data, reqtype)
|
||||
try:
|
||||
response = self.session.send(req, stream=stream)
|
||||
response = self.session.send(req, stream=stream, timeout=timeout)
|
||||
return self.validate_response(response, json_resp)
|
||||
except (exceptions.ConnectionError, exceptions.Timeout):
|
||||
_LOGGER.error(
|
||||
"Connection error. Endpoint %s possibly down or throttled. %s: %s",
|
||||
url,
|
||||
response.status_code,
|
||||
response.reason,
|
||||
"Connection error. Endpoint %s possibly down or throttled.", url,
|
||||
)
|
||||
except BlinkBadResponse:
|
||||
_LOGGER.error(
|
||||
@@ -184,6 +192,7 @@ class Auth:
|
||||
stream=stream,
|
||||
json_resp=json_resp,
|
||||
is_retry=True,
|
||||
timeout=timeout,
|
||||
)
|
||||
_LOGGER.error("Unable to access %s after token refresh.", url)
|
||||
except TokenRefreshFailed:
|
||||
|
||||
+8
-1
@@ -29,6 +29,7 @@ from blinkpy.helpers.constants import (
|
||||
DEFAULT_MOTION_INTERVAL,
|
||||
DEFAULT_REFRESH,
|
||||
MIN_THROTTLE_TIME,
|
||||
TIMEOUT_MEDIA,
|
||||
)
|
||||
from blinkpy.helpers.constants import __version__
|
||||
from blinkpy.auth import Auth, TokenRefreshFailed, LoginError
|
||||
@@ -330,7 +331,13 @@ class Blink:
|
||||
_LOGGER.info("%s already exists, skipping...", filename)
|
||||
continue
|
||||
|
||||
response = api.http_get(self, url=clip_address, stream=True, json=False)
|
||||
response = api.http_get(
|
||||
self,
|
||||
url=clip_address,
|
||||
stream=True,
|
||||
json=False,
|
||||
timeout=TIMEOUT_MEDIA,
|
||||
)
|
||||
with open(filename, "wb") as vidfile:
|
||||
copyfileobj(response.raw, vidfile)
|
||||
|
||||
|
||||
+11
-2
@@ -3,6 +3,7 @@
|
||||
from shutil import copyfileobj
|
||||
import logging
|
||||
from blinkpy import api
|
||||
from blinkpy.helpers.constants import TIMEOUT_MEDIA
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -182,11 +183,19 @@ class BlinkCamera:
|
||||
|
||||
if new_thumbnail is not None and (update_cached_image or force_cache):
|
||||
self._cached_image = api.http_get(
|
||||
self.sync.blink, url=self.thumbnail, stream=True, json=False
|
||||
self.sync.blink,
|
||||
url=self.thumbnail,
|
||||
stream=True,
|
||||
json=False,
|
||||
timeout=TIMEOUT_MEDIA,
|
||||
)
|
||||
if clip_addr is not None and (update_cached_video or force_cache):
|
||||
self._cached_video = api.http_get(
|
||||
self.sync.blink, url=self.clip, stream=True, json=False
|
||||
self.sync.blink,
|
||||
url=self.clip,
|
||||
stream=True,
|
||||
json=False,
|
||||
timeout=TIMEOUT_MEDIA,
|
||||
)
|
||||
|
||||
def get_liveview(self):
|
||||
|
||||
@@ -4,7 +4,7 @@ import os
|
||||
|
||||
MAJOR_VERSION = 0
|
||||
MINOR_VERSION = 16
|
||||
PATCH_VERSION = "0-rc9"
|
||||
PATCH_VERSION = "0-rc11"
|
||||
|
||||
__version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}.{PATCH_VERSION}"
|
||||
|
||||
@@ -48,10 +48,7 @@ URLS
|
||||
BLINK_URL = "immedia-semi.com"
|
||||
DEFAULT_URL = f"rest-prod.{BLINK_URL}"
|
||||
BASE_URL = f"https://{DEFAULT_URL}"
|
||||
LOGIN_ENDPOINT = {
|
||||
"v4": f"{BASE_URL}/api/v4/account/login",
|
||||
"v3": f"{BASE_URL}/api/v3/login",
|
||||
}
|
||||
LOGIN_ENDPOINT = f"{BASE_URL}/api/v4/account/login"
|
||||
|
||||
"""
|
||||
Dictionaries
|
||||
@@ -68,3 +65,5 @@ DEFAULT_REFRESH = 30
|
||||
MIN_THROTTLE_TIME = 2
|
||||
SIZE_NOTIFICATION_KEY = 152
|
||||
SIZE_UID = 16
|
||||
TIMEOUT = 10
|
||||
TIMEOUT_MEDIA = 90
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
python-dateutil~=2.8.1
|
||||
requests~=2.24.0
|
||||
python-slugify~=4.0.0
|
||||
python-slugify~=4.0.1
|
||||
testtools==2.4.0
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
black==19.10b0
|
||||
coverage==5.1
|
||||
coverage==5.2
|
||||
flake8==3.8.3
|
||||
flake8-docstrings==1.5.0
|
||||
pre-commit==2.5.1
|
||||
pre-commit==2.6.0
|
||||
pylint==2.5.3
|
||||
pydocstyle==5.0.2
|
||||
pytest==5.4.3
|
||||
pytest-cov==2.10.0
|
||||
pytest-sugar==0.9.3
|
||||
pytest-sugar==0.9.4
|
||||
pytest-timeout==1.4.1
|
||||
restructuredtext-lint==1.3.1
|
||||
pygments==2.6.1
|
||||
|
||||
@@ -3,23 +3,26 @@
|
||||
from os.path import abspath, dirname
|
||||
from setuptools import setup, find_packages
|
||||
from blinkpy.helpers.constants import (
|
||||
__version__, PROJECT_PACKAGE_NAME, PROJECT_LICENSE, PROJECT_URL,
|
||||
PROJECT_EMAIL, PROJECT_DESCRIPTION, PROJECT_CLASSIFIERS, PROJECT_AUTHOR,
|
||||
__version__,
|
||||
PROJECT_PACKAGE_NAME,
|
||||
PROJECT_LICENSE,
|
||||
PROJECT_URL,
|
||||
PROJECT_EMAIL,
|
||||
PROJECT_DESCRIPTION,
|
||||
PROJECT_CLASSIFIERS,
|
||||
PROJECT_AUTHOR,
|
||||
)
|
||||
|
||||
PROJECT_VERSION = __version__
|
||||
|
||||
THIS_DIR = abspath(dirname(__file__))
|
||||
|
||||
REQUIRES = [
|
||||
"python-dateutil~=2.8.1",
|
||||
"requests~=2.23.0",
|
||||
"python-slugify~=4.0.0",
|
||||
]
|
||||
with open(f"{THIS_DIR}/requirements.txt") as req_file:
|
||||
REQUIRES = [line.rstrip() for line in req_file]
|
||||
|
||||
PACKAGES = find_packages(exclude=['tests*', 'docs'])
|
||||
PACKAGES = find_packages(exclude=["tests*", "docs"])
|
||||
|
||||
with open('{}/README.rst'.format(THIS_DIR), encoding='utf-8') as readme_file:
|
||||
with open("{}/README.rst".format(THIS_DIR), encoding="utf-8") as readme_file:
|
||||
LONG_DESCRIPTION = readme_file.read()
|
||||
|
||||
setup(
|
||||
@@ -31,11 +34,11 @@ setup(
|
||||
author_email=PROJECT_EMAIL,
|
||||
license=PROJECT_LICENSE,
|
||||
url=PROJECT_URL,
|
||||
platforms='any',
|
||||
py_modules=['blinkpy'],
|
||||
platforms="any",
|
||||
py_modules=["blinkpy"],
|
||||
packages=PACKAGES,
|
||||
include_package_data=True,
|
||||
install_requires=REQUIRES,
|
||||
test_suite='tests',
|
||||
classifiers=PROJECT_CLASSIFIERS
|
||||
test_suite="tests",
|
||||
classifiers=PROJECT_CLASSIFIERS,
|
||||
)
|
||||
|
||||
@@ -144,21 +144,6 @@ class TestAuth(unittest.TestCase):
|
||||
fake_resp = mresp.MockResponse({"foo": "bar"}, 200)
|
||||
mock_req.return_value = fake_resp
|
||||
self.assertEqual(self.auth.login(), {"foo": "bar"})
|
||||
mock_req.assert_called_with(
|
||||
self.auth, const.LOGIN_ENDPOINT["v4"], {}, is_retry=False
|
||||
)
|
||||
|
||||
@mock.patch("blinkpy.auth.Auth.validate_login", return_value=None)
|
||||
@mock.patch("blinkpy.auth.api.request_login")
|
||||
def test_login_v3(self, mock_req, mock_validate):
|
||||
"""Test login handling."""
|
||||
auth_v3 = Auth(login_method="v3")
|
||||
fake_resp = mresp.MockResponse({"foo": "bar"}, 200)
|
||||
mock_req.return_value = fake_resp
|
||||
self.assertEqual(auth_v3.login(), {"foo": "bar"})
|
||||
mock_req.assert_called_with(
|
||||
auth_v3, const.LOGIN_ENDPOINT["v3"], {}, is_retry=False
|
||||
)
|
||||
|
||||
@mock.patch("blinkpy.auth.Auth.validate_login", return_value=None)
|
||||
@mock.patch("blinkpy.auth.api.request_login")
|
||||
|
||||
Reference in New Issue
Block a user