def _check_uuid(self, uuid_str): if not uuid_str: raise ArcsecondError('Missing UUID') try: uuid.UUID(uuid_str) except ValueError: raise ArcsecondError('Invalid UUID {}.'.format(uuid_str))
def _check_and_set_auth_key(self, headers, url): if API_AUTH_PATH_REGISTER in url or API_AUTH_PATH_LOGIN in url or 'Authorization' in headers.keys(): return headers # Choose the strongest key first auth_key = self.state.api_key or self.state.upload_key if auth_key is None: if self.state.verbose: click.echo('Checking local API|Upload key... ', nl=False) # Choose the strongest key first auth_key = config_file_read_api_key(self.state.config_section()) if not auth_key: auth_key = config_file_read_upload_key(self.state.config_section()) if not auth_key: raise ArcsecondError('Missing auth keys (API or Upload). You must login first: $ arcsecond login') headers['X-Arcsecond-API-Authorization'] = 'Key ' + auth_key if self.state.verbose: key_str = auth_key[:3] + 9 * '*' click.echo(f'OK (\'X-Arcsecond-API-Authorization\' = \'Key {key_str}\'') return headers
def _perform_request(self, url, method, payload, **headers): assert (url and method) if not isinstance(method, str) or callable(method): raise ArcsecondError('Invalid HTTP request method {}. '.format( str(method))) headers = self._check_and_set_api_key(headers, url) method_name = method.upper() if isinstance(method, str) else '' method = getattr(requests, method.lower()) if isinstance( method, str) else method files = payload.pop('files', None) if payload else None if payload: payload = {k: v for k, v in payload.items() if v is not None} if self.state.verbose: click.echo('Sending {} request to {}'.format(method_name, url)) click.echo('Payload: {}'.format(payload)) response = self._async_perform_request(url, method, payload, files, **headers) if response is None: raise ArcsecondConnectionError(url) if self.state.verbose: click.echo('Request status code ' + str(response.status_code)) if response.status_code >= 200 and response.status_code < 300: return (response.json() if response.text else {}, None) else: return (None, response.text)
def _target(self, url, method, data, payload, headers): try: self._storage['response'] = method(url, data=data, json=payload, headers=headers, timeout=60) except requests.Timeout: self._storage['error'] = ArcsecondRequestTimeoutError(url) except requests.exceptions.ConnectionError: self._storage['error'] = ArcsecondConnectionError(url) except Exception as e: self._storage['error'] = ArcsecondError(str(e))
def _async_perform_request_store_response(storage, method, url, payload, files, headers): try: storage['response'] = method(url, json=payload, files=files, headers=headers) except requests.exceptions.ConnectionError: storage['error'] = ArcsecondConnectionError( self._get_base_url()) except Exception as e: storage['error'] = ArcsecondError(str(e))
def _check_and_set_api_key(self, headers, url): if 'login' in url or 'register' in url or 'Authorization' in headers.keys( ): return headers if self.state.verbose: click.echo('Checking local API key... ', nl=False) api_key = config_file_read_api_key(self.state.debug) if not api_key: raise ArcsecondError( 'Missing API key. You must login first: $ arcsecond login') headers['X-Arcsecond-API-Authorization'] = 'Key ' + api_key if self.state.verbose: click.echo('OK') return headers
def _prepare_request(self, url, method, payload): assert (url and method) if self.state.verbose: click.echo('Preparing request...') if not isinstance(method, str) or callable(method): raise ArcsecondError('Invalid HTTP request method {}. '.format(str(method))) # Put method name aside in its own var. method_name = method.upper() if isinstance(method, str) else '' # Check API key, hence login state. Must do before check for org. headers = self._check_and_set_auth_key(self.headers or {}, url) method = getattr(requests, method.lower()) if isinstance(method, str) else method if payload: # Filtering None values out of payload. payload = {k: v for k, v in payload.items() if v is not None} return method_name, method, payload, headers