def post(self, url: str, json: dict) -> dict: result = self._find(url, json) if result is None: json['variables'] = {} result = self._find(url, json) if result is None: raise NuException(f'There is no result expected for {url}') return result
def request_code(self) -> str: response = requests.post(self.url, json=self._get_payload()) if response.status_code != 401 or not response.headers.get('WWW-Authenticate'): raise NuException('Authentication code request failed.') parsed = self._parse_authenticate_headers(response.headers.get('WWW-Authenticate')) self.encrypted_code = parsed.get('device-authorization_encrypted-code') return parsed.get('sent-to')
def exchange_certs(self, code: str): if not self.encrypted_code: raise NuException('No encrypted code found. Did you call `request_code` before exchanging certs ?') payload = self._get_payload() payload['code'] = code payload['encrypted-code'] = self.encrypted_code response = requests.post(self.url, json=payload) if response.status_code != 200: raise NuRequestException(response) data = response.json() cert1 = self._parse_cert(data['certificate']) cert2 = self._parse_cert(data['certificate_crypto']) return self._gen_cert(self.key1, cert1), self._gen_cert(self.key2, cert2)
def get(self, url: str) -> dict: result = self._find(url) if result is None: raise NuException(f'There is no result expected for {url}') return result
def mock_error(_): raise NuException('failed')