def test_bad_request(self, mock_sess): """Check that we raise an Exception with a bad request.""" self.blink.session = create_session() explog = "WARNING:blinkpy.helpers.util:" "Response from server: 200 - foo" with self.assertRaises(BlinkException): http_req(self.blink, reqtype="bad") with self.assertLogs() as logrecord: http_req(self.blink, reqtype="post", is_retry=True) self.assertEqual(logrecord.output, [explog])
def test_bad_request(self, mock_sess): """Check that we raise an Exception with a bad request.""" self.blink.session = create_session() explog = ("ERROR:blinkpy.helpers.util:" "Cannot obtain new token for server auth.") with self.assertRaises(BlinkException): http_req(self.blink, reqtype='bad') with self.assertLogs() as logrecord: http_req(self.blink, reqtype='post', is_retry=True) self.assertEqual(logrecord.output, [explog])
def _network_request(self): """Get network and account information.""" url = self.urls.networks_url headers = self._auth_header if headers is None: raise BlinkException(ERROR.AUTH_TOKEN) return http_req(self, url=url, headers=headers, reqtype='get')
def http_post(self, url): """Perform a post request. :param url: URL to perform post request """ return http_req(self.blink, url=url, headers=self._auth_header, reqtype='post')
def get_auth_token(self): """Retrieve the authentication token from Blink.""" if not isinstance(self._username, str): raise BlinkAuthenticationException(ERROR.USERNAME) if not isinstance(self._password, str): raise BlinkAuthenticationException(ERROR.PASSWORD) headers = {'Host': DEFAULT_URL, 'Content-Type': 'application/json'} data = json.dumps({ "email": self._username, "password": self._password, "client_specifier": "iPhone 9.2 | 2.2 | 222" }) self.session = create_session() response = http_req(self, url=self._login_url, headers=headers, data=data, json_resp=False, reqtype='post') if response.status_code == 200: response = response.json() (self.region_id, self.region), = response['region'].items() else: _LOGGER.debug(("Received response code %s " "when authenticating, " "trying new url"), response.status_code) self._login_url = LOGIN_BACKUP_URL response = http_req(self, url=self._login_url, headers=headers, data=data, reqtype='post') self.region_id = 'piri' self.region = "UNKNOWN" self._host = "{}.{}".format(self.region_id, BLINK_URL) self._token = response['authtoken']['authtoken'] self._auth_header = {'Host': self._host, 'TOKEN_AUTH': self._token} self.urls = BlinkURLHandler(self.region_id) return self._auth_header
def events_request(self, skip_throttle=False): """Get events on server.""" url = "{}/{}".format(self.urls.event_url, self.network_id) headers = self._auth_header if self.check_if_ok_to_update() or skip_throttle: self._last_events = http_req(self, url=url, headers=headers, reqtype='get') return self._last_events
def http_get(self, url, stream=False, json=True): """ Perform an http get request. :param url: URL to perform get request :param stream: Stream response? True/FALSE :param json: Return json response? TRUE/False """ return http_req(self.blink, url=url, headers=self._auth_header, reqtype='get', stream=stream, json_resp=json)
def http_post(blink, url): """ Perform an http post request. :param url: URL to perfom post request. """ if blink.auth_header is None: raise BlinkException(ERROR.AUTH_TOKEN) _LOGGER.debug("Making POST request to %s", url) return http_req(blink, url=url, headers=blink.auth_header, reqtype='post')
def http_post(blink, url, is_retry=False): """ Perform an http post request. :param url: URL to perfom post request. :param is_retry: Is this part of a re-auth attempt? """ if blink.auth_header is None: raise BlinkException(ERROR.AUTH_TOKEN) _LOGGER.debug("Making POST request to %s", url) return http_req(blink, url=url, headers=blink.auth_header, reqtype='post', is_retry=is_retry)
def summary_request(self, skip_throttle=False): """Get blink summary.""" url = self.urls.home_url headers = self._auth_header if headers is None: raise BlinkException(ERROR.AUTH_TOKEN) if self.check_if_ok_to_update() or skip_throttle: self._last_summary = http_req(self, url=url, headers=headers, reqtype='get') return self._last_summary
def request_verify(blink, verify_key): """Send verification key to blink servers.""" url = "{}/api/v4/account/{}/client/{}/pin/verify".format( blink.urls.base_url, blink.account_id, blink.client_id) data = dumps({"pin": verify_key}) return http_req( blink, url=url, headers=blink.auth_header, data=data, json_resp=False, reqtype="post", )
def request_login(blink, url, username, password): """Login request.""" headers = {'Host': DEFAULT_URL, 'Content-Type': 'application/json'} data = dumps({ 'email': username, 'password': password, 'client_specifier': 'iPhone 9.2 | 2.2 | 222' }) return http_req(blink, url=url, headers=headers, data=data, json_resp=False, reqtype='post')
def http_get(blink, url, stream=False, json=True, is_retry=False): """ Perform an http get request. :param url: URL to perform get request. :param stream: Stream response? True/FALSE :param json: Return json response? TRUE/False :param is_retry: Is this part of a re-auth attempt? """ if blink.auth_header is None: raise BlinkException(ERROR.AUTH_TOKEN) _LOGGER.debug("Making GET request to %s", url) return http_req(blink, url=url, headers=blink.auth_header, reqtype='get', stream=stream, json_resp=json, is_retry=is_retry)
def request_login( blink, url, username, password, notification_key, uid, is_retry=False, device_id="Blinkpy", ): """ Login request. :param blink: Blink instance. :param url: Login url. :param username: Blink username. :param password: Blink password. :param notification_key: Randomly genereated key. :param uid: Randomly generated unique id key. :param is_retry: Is this part of a re-authorization attempt? :param device_id: Name of application to send at login. """ headers = {"Host": DEFAULT_URL, "Content-Type": "application/json"} data = dumps({ "email": username, "password": password, "notification_key": notification_key, "unique_id": uid, "app_version": "6.0.7 (520300) #afb0be72a", "client_name": "Computer", "client_type": "android", "device_identifier": device_id, "device_name": "Blinkpy", "os_version": "5.1.1", "reauth": "true", }) return http_req( blink, url=url, headers=headers, data=data, json_resp=False, reqtype="post", is_retry=is_retry, )
def request_login(blink, url, username, password, is_retry=False): """ Login request. :param blink: Blink instance. :param url: Login url. :param username: Blink username. :param password: Blink password. :param is_retry: Is this part of a re-authorization attempt? """ headers = { 'Host': DEFAULT_URL, 'Content-Type': 'application/json' } data = dumps({ 'email': username, 'password': password, 'client_specifier': 'iPhone 9.2 | 2.2 | 222' }) return http_req(blink, url=url, headers=headers, data=data, json_resp=False, reqtype='post', is_retry=is_retry)