def test_json(self, request): request().content = '{}' resp = make_rest_api_call( 'GET', 'http://something.com/path/to/resource.json') self.assertEqual(resp, {}) request.assert_called_with( 'GET', 'http://something.com/path/to/resource.json', headers=None, timeout=None) # Test JSON Error e = HTTPError('error') e.response = MagicMock() e.response.status_code = 404 e.response.content = '''{ "error": "description", "code": "Error Code" }''' request().raise_for_status.side_effect = e self.assertRaises( SoftLayerAPIError, make_rest_api_call, 'GET', 'http://something.com/path/to/resource.json')
def invalid_virtual_address(target): setup_target(target) bigip = make_bigip() http_error = HTTPError('foo') http_error.response = Mock() http_error.response.status_code = 400 BigIPResourceHelper.load.side_effect = http_error target.split_addr_port.return_value = \ tuple([bigip.lb_id, bigip.vip_port]) expected = [] assert target.get_virtual_service_insertion( bigip, partition=bigip.partition) == expected
def __raise_for_status(self, status_code, reason): http_error_msg = '' if 400 <= status_code < 500: http_error_msg = '%s Client Error: %s' % (status_code, reason) elif 500 <= status_code < 600: http_error_msg = '%s Server Error: %s' % (status_code, reason) if http_error_msg: http_error = HTTPError(http_error_msg) http_error.response = self raise http_error
def test_text(self, request): request().text = "content" resp = make_rest_api_call("GET", "http://something.com/path/to/resource.txt") self.assertEqual(resp, "content") request.assert_called_with("GET", "http://something.com/path/to/resource.txt", headers=None, timeout=None) # Test Text Error e = HTTPError("error") e.response = MagicMock() e.response.status_code = 404 e.response.content = "Error Code" request().raise_for_status.side_effect = e self.assertRaises(SoftLayerAPIError, make_rest_api_call, "GET", "http://something.com/path/to/resource.txt")
def raise_for_status(self, allow_redirects=True): """Raises stored :class:`HTTPError` or :class:`URLError`, if one occurred.""" if self.error: http_error = HTTPError(self.error) elif (self.status_code >= 300) and (self.status_code < 400) and not allow_redirects: http_error = HTTPError('%s Redirection' % (self.status_code)) elif (self.status_code >= 400) and (self.status_code < 500): http_error = HTTPError('%s Client Error' % (self.status_code)) elif (self.status_code >= 500) and (self.status_code < 600): http_error = HTTPError('%s Server Error' % (self.status_code)) else: return http_error.response = self raise http_error
def positive_w_exception(target): setup_target(target) bigip = make_bigip() http_error = HTTPError('foo') http_error.response = Mock() http_error.response.status_code = 404 bigip.tm.ltm.virtual_address_s.virtual_address.load.side_effect = \ http_error target.split_addr_port.return_value = \ tuple([bigip.lb_id, bigip.vip_port]) expected = \ [{bigip.name: dict(address=bigip.lb_id, netmask=bigip.netmask, protocol=bigip.protocol, port=bigip.vip_port)}] assert target.get_virtual_service_insertion( bigip, partition=bigip.partition) == expected
def test_purge_orphaned_health_monitor(self, standalone_builder, fully_mocked_target, mock_logger, service_with_health_monitor, mock_resource_helper): svc = service_with_health_monitor builder = standalone_builder target = fully_mocked_target def main_path(target, builder, svc, resource_helper): bigip = Mock() hostnames = ['foodoozoo'] bigip.hostname = hostnames[0] builder.mock_get_all_bigips(target, return_value=[bigip]) li_id = svc['healthmonitors'][0]['id'] t_id = svc['healthmonitors'][0]['tenant_id'] target.purge_orphaned_health_monitor(t_id, li_id, hostnames) builder.check_mocks(target) assert resource_helper.return_value.load.call_count assert resource_helper.call_count == 4 def error(target, svc, builder, resource_helper, logger, error): bigip = Mock() hostnames = ['foodoozoo'] bigip.hostname = hostnames[0] builder.mock_get_all_bigips(target, return_value=[bigip]) li_id = svc['healthmonitors'][0]['id'] t_id = svc['healthmonitors'][0]['tenant_id'] resource_helper.return_value.load.side_effect = error target.purge_orphaned_health_monitor(t_id, li_id, hostnames) main_path(target, builder, svc, mock_resource_helper) mock_resource_helper.reset_mock() response = Mock() response.status_code = 404 http_error = HTTPError("foo") http_error.response = response error(target, svc, builder, mock_resource_helper, self.logger, http_error) assert self.logger.exception.call_count self.logger.reset_mock() error(target, svc, builder, mock_resource_helper, self.logger, Exception) assert self.logger.exception.call_count
def raise_for_status(self, allow_redirects=True): """Raises stored :class:`HTTPError` or :class:`URLError`, if one occurred.""" if self.status_code == 304: return elif self.error: if self.traceback: six.reraise(Exception, Exception(self.error), Traceback.from_string(self.traceback).as_traceback()) http_error = HTTPError(self.error) elif (self.status_code >= 300) and (self.status_code < 400) and not allow_redirects: http_error = HTTPError('%s Redirection' % (self.status_code)) elif (self.status_code >= 400) and (self.status_code < 500): http_error = HTTPError('%s Client Error' % (self.status_code)) elif (self.status_code >= 500) and (self.status_code < 600): http_error = HTTPError('%s Server Error' % (self.status_code)) else: return http_error.response = self raise http_error
def test_text(self, request): request().text = 'content' resp = make_rest_api_call( 'GET', 'http://something.com/path/to/resource.txt') self.assertEqual(resp, 'content') request.assert_called_with( 'GET', 'http://something.com/path/to/resource.txt', headers=None, timeout=None) # Test Text Error e = HTTPError('error') e.response = MagicMock() e.response.status_code = 404 e.response.content = 'Error Code' request().raise_for_status.side_effect = e self.assertRaises( SoftLayerAPIError, make_rest_api_call, 'GET', 'http://something.com/path/to/resource.txt')
def send_request(self, request): if request.method.upper() == "GET": rv = requests.get(url=request.url, headers=request.headers, params=request.params, proxies=request.proxy) elif request.method.upper() == "post": rv = requests.get(url=request.url, headers=request.headers, params=request.params, proxiex=request.proxy, data=request.formdata) else: raise HTTPError("Not Support {} method".format(request.method)) rv.encoding = chardet.detect(rv.content)["encoding"] return Response(url=rv.url, content=rv.content, headers=rv.headers, encoding=rv.encoding, status_code=rv.status_code)
def test_process_response_2(self): """ Dado que: - existe uma resposta r1 mockada de erro Quando essa resposta for processada Então: - deve ter sido lançado um HttpError - r1.data deve ser r1.json.return_value - r1.reason deve ser r1.data - r1.raise_for_status deve ter sido chamado uma vez """ r1 = MagicMock(json=MagicMock(), raise_for_status=MagicMock(side_effect=HTTPError())) with self.assertRaises(HTTPError): RequestsWrapper._RequestsWrapper__process_response(r1) self.assertEqual(r1.data, r1.json.return_value) self.assertEqual(r1.reason, r1.data) r1.raise_for_status.assert_called_once()
def get_logins(cls, connection_id, nonce, verifier, url): """getting logins through url""" payload = { 'RequestType': 'get-logins', 'SortSelection': 'true', 'TriggerUnlock': 'false', 'Id': connection_id, 'Nonce': nonce, 'Verifier': verifier, 'Url': url, 'SubmitUrl': url } r = requests.post(cls.URL, json=payload) data = r.json() error = data.get('Error') if error: raise HTTPError(error) r.raise_for_status() return data['Entries'], data['Nonce']
def test_get_charts_http_error( fetch_charts_mock, app, client, client_application, organization_pipeline, organization_pipeline_run, ): fetch_charts_mock.side_effect = HTTPError("an error") result = client.get( f"/v1/organizations/{ORGANIZATION_UUID}/pipelines/{organization_pipeline.uuid}/runs/{organization_pipeline_run.uuid}/charts", content_type="application/json", json=CHART_JSON, headers={ "Authorization": f"Bearer {JWT_TOKEN}", ROLES_KEY: client_application.api_key, }, ) assert result.status_code == 503
def test_provider_renews_access_token(self, mocked_post): new_token = 'new_test_token' response401 = MagicMock() response401.status_code = 401 data = MagicMock() data.return_value = {'access_token': new_token, 'token_type': 'type'} response = MagicMock() response.json = data response.status_code = 200 mocked_post.side_effect = [ HTTPError(response=response401), response, response] self.payment.created = timezone.now() self.payment.extra_data = json.dumps({ 'auth_response': { 'access_token': 'expired_token', 'token_type': 'token type', 'expires_in': 99999}}) self.provider.create_payment(self.payment) payment_response = json.loads(self.payment.extra_data)['auth_response'] self.assertEqual(payment_response['access_token'], new_token)
def test_check_incomplete_publish_build_statuses_404(settings, mocker, api_mock): """A website with a non-existent pipeline/build should have publishing status set to errored""" mock_log = mocker.patch("content_sync.tasks.log.error") bad_build_website = WebsiteFactory.create( draft_publish_status_updated_on=now_in_utc() - timedelta(seconds=settings.PUBLISH_STATUS_CUTOFF + 5), draft_publish_status=PUBLISH_STATUS_NOT_STARTED, latest_build_id_draft=1, ) api_mock.get_sync_pipeline.return_value.get_build_status.side_effect = HTTPError( response=mocker.Mock(status_code=404)) tasks.check_incomplete_publish_build_statuses.delay() mock_log.assert_called_once_with( "Could not find %s build %s for %s", VERSION_DRAFT, bad_build_website.latest_build_id_draft, bad_build_website.name, ) bad_build_website.refresh_from_db() assert bad_build_website.draft_publish_status == PUBLISH_STATUS_ERRORED
def test_stats_grafana_xapi_fun_backend_HTTPError(self, logger_mock): """HttpError from call to the backend should return stats with 0.""" video = VideoFactory() settings = { "api_key": "grafana_api_key", "api_endpoint": "https://grafana.tld/api", "api_datasource_id": "1", "api_datastream": "statements-ds-marsha", } exception = HTTPError("An error occurred") responses.get( url=( f"{settings.get('api_endpoint')}/datasources/proxy/" f"{settings.get('api_datasource_id')}/{settings.get('api_datastream')}/_count" ), body=exception, ) self.assertEqual({"nb_views": 0}, grafana_xapi_fun_backend(video, **settings)) logger_mock.warning.assert_called_with("Http error %s", exception)
def test_perform_request_bad_response(self, request): response_mock = Mock() response_mock.raise_for_status.side_effect = HTTPError() request.return_value = response_mock API._perform_request( 'POST', 'https://api.litmos.com/v1.svc/pies/wsGty/eaters?apikey=api-key-123&source=app-name-123&format=json', json={ 'Id': '', 'Name': 'Charlie' }) request.assert_called_once_with( 'POST', 'https://api.litmos.com/v1.svc/pies/wsGty/eaters?apikey=api-key-123&source=app-name-123&format=json', json={ 'Id': '', 'Name': 'Charlie' })
def handle_response(response: Response): """Handles HTTP Repsonse code""" if response.status_code == 200: pass elif response.status_code == 401: # Authentication info is missing or invalid. raise FormsiteInvalidAuthenticationException( response, "Please check if token, directory and server are correct", ) elif response.status_code == 403: # Forbidden. raise FormsiteForbiddenException(response) elif response.status_code == 404: # Path or object not found. raise FormsiteFormNotFoundException(response) elif response.status_code == 422: # Invalid parameter. raise FormsiteInvalidParameterException(response) elif response.status_code == 429: # Too many requests or too busy. raise FormsiteRateLimitException(response) elif response.status_code >= 500: # Unexpected Formsite internal error. raise FormsiteInternalException(response) else: raise HTTPError(response)
def download_huc4(HUC4, filename): """Download HUC4 geodatabase (flowlines and boundaries) from NHD Plus HR data distribution site Parameters ---------- HUC4 : str HUC4 ID code filename : str output filename. Will always overwrite this filename. """ with requests.get(DATA_URL.format(HUC4=HUC4), stream=True) as r: if not r.status_code == 200: raise HTTPError("Could not download {}".format(HUC4)) with open(filename, "wb") as out: print("Downloading HUC4: {HUC4} ({size:.2f} MB)".format( HUC4=HUC4, size=int(r.headers["Content-Length"]) / 1024**2)) # Use a streaming copy to download the bytes of this file copyfileobj(r.raw, out)
def get_response(self, response, no_refresh=False): """ Parse response and extract data from response. Will try to refresh the access token if it's expired. Arguments: response (requests Response object): Response to parse no_refresh (bool): Disable refreshing of the token Returns: data fetched from server """ data = self.get_data(response) if 200 <= response.status_code < 300: return data elif (not no_refresh and response.status_code == 401 and not response.url.endswith("auth/token") and self.identity.is_expired()): self.refresh_token() return self.send(self.old_params, no_refresh=True) raise HTTPError(data, response=response)
def query(config, req_func, endpoint, params={}, data=None, headers=None): ''' Query the Todoist REST API using an api token ''' if not config.get('todoist', 'api_token'): raise ValueError('No Todoist API token given in config') params['token'] = config['todoist']['api_token'] resp = req_func(URL.format(endpoint), params=params, data=data, headers=headers) if 200 <= resp.status_code < 400: try: return resp.json() except json.JSONDecodeError: # TODO: confirm that this is the correct error return resp.text raise HTTPError(resp.reason)
def test_initialize_session_kv_authentication_error( self, mock_log, mock_get_kv_secret): reload(sys.modules['c7n_azure.session']) with self.assertRaises(SystemExit): mock_get_kv_secret.side_effect = HTTPError() with patch.dict(os.environ, { constants.ENV_TENANT_ID: 'tenant', constants.ENV_SUB_ID: DEFAULT_SUBSCRIPTION_ID, constants.ENV_KEYVAULT_CLIENT_ID: 'kv_client', constants.ENV_KEYVAULT_SECRET_ID: 'kv_secret' }, clear=True): s = Session() s.get_subscription_id() mock_log.assert_called_once_with( 'Azure Authentication Failure\nError: ' 'Cannot retrieve SP credentials from the Key Vault ' '(KV uses MSI to access) with client id: kv_client')
class TestDownloadedPDF: def test_not_found_error(self, mocker): responce_mock = mocker.Mock() responce_mock.status_code = 404 mocker.patch.object(requests, "get", return_value=responce_mock) with pytest.raises(HTTPDownloadError, match="cannot get PDF contents."): DownloadedPDF("http://dummy.local") @pytest.mark.parametrize( "exception,expected", [ (Timeout("Dummy Error."), "cannot connect to web server."), (HTTPError("Dummy Error."), "cannot connect to web server."), (ConnectionError("Dummy Error."), "cannot connect to web server."), ], ) def test_network_error(self, exception, expected, mocker): mocker.patch.object(requests, "get", side_effect=exception) with pytest.raises(HTTPDownloadError, match=expected): DownloadedPDF("http://dummy.local")
def _refresh_token(self, email='', pwd=''): if self.staticToken: return if not self.session.headers.get('Authorization'): auth_route = 'authenticate' auth_data = {'email': os.getenv('DIRECTUS_AUTH_EMAIL', email), 'password': os.getenv('DIRECTUS_AUTH_PWD', pwd)} else: auth_route = 'refresh' auth_data = {'token': self.session.headers.get( 'Authorization').split()[1]} auth_response = self.session.post( f'{self.url}/auth/{auth_route}', data=auth_data) if auth_response.status_code != 200: raise HTTPError(auth_response.json()[ 'error']['message'], auth_response) self.session.headers.update( {'Authorization': f'Bearer {auth_response.json()["data"]["token"]}'})
def _upload_dir_to_url(base_url, src_dir, headers=None): # type: (str, str, Optional[Dict]) -> None """ Recursively put files from the specified directory to the specified URL. :param base_url: URL to put files to :type base_url: string :param src_dir: directory containing files to upload :type src_dir: string """ for root, dirs, files in os.walk(src_dir): for name in files: path = os.path.join(root, name) rel_path = os.path.relpath(path, src_dir) with open(path, 'rb') as data: resource = '{}/{}'.format(base_url, rel_path) r = requests.put(resource, data=data, headers=headers) if r.status_code != 200: raise HTTPError( 'Failed to upload resource: {} with status code {}'.format( resource, r.status_code))
def assert_status(response: requests.Response, *args, **kwds): if 200 <= response.status_code < 300: return error_message = response.reason try: err_json = response.json() errors = err_json.get("errors", []) error = err_json.get("error") if error: errors.append(error) error_message = ", ".join(errors) error_message = ( f"Request failed with {response.status_code}: {error_message}") except (TypeError, ValueError, json.JSONDecodeError): pass raise HTTPError(error_message, response=response)
def get_crawler(cls, query, force=False): if not force and cls._exist(query): c = cls._get(query) html = c.response return html else: base = 'https://github.com' url = '{}{}'.format(base, query) log('get crawler url', url) agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " \ "AppleWebKit/537.36 (KHTML, like Gecko) " \ "Chrome/62.0.3202.94 Safari/537.36" headers = {'User-Agent': agent} r = requests.get(url=url, headers=headers) if r.status_code == 200: html = r.text cls._set(query, html) return html else: message = 'url {} get error code {}'.format(url, r.status_code) raise HTTPError(message, response=r)
def test_package_sync_404_json_info_keeps_package_on_non_deleting_mirror( mirror, requests): mirror.delete_packages = False mirror.master.package_releases = mock.Mock() mirror.master.package_releases.return_value = {} response = mock.Mock() response.status_code = 404 requests.prepare(HTTPError(response=response), 0) paths = [ Path("web/packages/2.4/f/foo/foo.zip"), Path("web/simple/foo/index.html") ] touch_files(paths) package = Package("foo", 10, mirror) package.sync() for path in paths: assert path.exists()
def test_status(self, mock_logger, mock_db_connections, mock_redis): url = reverse(status_view) mock_db_connections.__getitem__.return_value.cursor.side_effect = Exception('No connection') mock_redis.return_value.ping.side_effect = HTTPError('Bad connection') response = self.client.get(url) self.assertEqual(response.status_code, 400) self.assertDictEqual( response.json(), {'version': 'v1.0', 'dependent_services_ok': False, 'secondary_services_ok': False}) mock_logger.error.assert_has_calls([ mock.call('Database "default" connection error: No connection'), mock.call('Database "reference_data" connection error: No connection'), mock.call('Redis connection error: Bad connection'), mock.call('Elasticsearch connection error: No response from elasticsearch ping'), mock.call('Kibana connection error: Connection refused: HEAD /status'), ]) mock_logger.reset_mock() mock_db_connections.__getitem__.return_value.cursor.side_effect = None mock_redis.return_value.ping.side_effect = None urllib3_responses.add(urllib3_responses.HEAD, '/', status=200) urllib3_responses.add(urllib3_responses.HEAD, '/status', status=500) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertDictEqual( response.json(), {'version': 'v1.0', 'dependent_services_ok': True, 'secondary_services_ok': False}) mock_logger.error.assert_has_calls([ mock.call('Kibana connection error: Error 500: Internal Server Error'), ]) mock_logger.reset_mock() urllib3_responses.replace_json('/status', {'success': True}, method=urllib3_responses.HEAD, status=200) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertDictEqual( response.json(), {'version': 'v1.0', 'dependent_services_ok': True, 'secondary_services_ok': True}) mock_logger.error.assert_not_called()
class TestDownloadedCSV: @pytest.fixture() def csv_content(self): csv_content = """ No,全国地方公共団体コード,都道府県名,市区町村名,公表_年月日,発症_年月日,患者_居住地,患者_年代,患者_性別,患者_職業,患者_状態,患者_症状,患者_渡航歴の有無フラグ,患者_再陽性フラグ,患者_退院済フラグ,備考 1,10006,北海道,,2020-01-28,2020-01-21,中国武漢市,40代,女性,−,−,発熱,1,0,,海外渡航先:中国武漢 2,10006,北海道,,2020-02-14,2020-01-31,石狩振興局管内,50代,男性,自営業,−,発熱;咳;倦怠感,0,0,, 3,10006,北海道,,2020-02-19,2020-02-08,石狩振興局管内,40代,男性,会社員,−,倦怠感;筋肉痛;関節痛;発熱;咳,0,0,, """ return csv_content.encode("cp932") def test_content(self, csv_content, mocker): responce_mock = mocker.Mock() responce_mock.status_code = 200 responce_mock.content = csv_content responce_mock.headers = {"content-type": "text/csv"} mocker.patch.object(requests, "get", return_value=responce_mock) csv_file = DownloadedCSV(url="http://dummy.local", encoding="cp932") assert csv_file.content.getvalue() == csv_content.decode("cp932") def test_not_found_error(self, mocker): responce_mock = mocker.Mock() responce_mock.status_code = 404 mocker.patch.object(requests, "get", return_value=responce_mock) with pytest.raises(HTTPDownloadError, match="cannot get CSV contents."): DownloadedCSV("http://dummy.local") @pytest.mark.parametrize( "exception,expected", [ (Timeout("Dummy Error."), "cannot connect to web server."), (HTTPError("Dummy Error."), "cannot connect to web server."), (ConnectionError("Dummy Error."), "cannot connect to web server."), ], ) def test_network_error(self, exception, expected, mocker): mocker.patch.object(requests, "get", side_effect=exception) with pytest.raises(HTTPDownloadError, match=expected): DownloadedCSV("http://dummy.local")
def dcnmLogon(self, expiry = 180000): """ Builds DCNM_TOKEN from /logon """ url = "https://{}/rest{}".format(self.DCNM_FQDN, "/logon") payload = {"expirationTime": expiry} self.setupRequestsSession() # self.DCNM_SESSION = requests.Session() # # ## Allow for HTTP Retries # # a = requests.adapters.HTTPAdapter(max_retries=3) # adapt = requests.adapters.HTTPAdapter(max_retries=5) # # self.DCNM_SESSION.mount('http://', a) # self.DCNM_SESSION.mount('https://', adapt) logging.debug("[aclm][userLogon] Datetime: {}".format(datetime.now())) self.DCNM_EXPIRY = datetime.now().timestamp() + (int(expiry) / 1000) r = self.DCNM_SESSION.post(url, json=payload, verify=False, auth=HTTPBasicAuth(self.DCNM_USERNAME, self.DCNM_PASSWORD)) #r.raise_for_status() logging.info("[aclm][userLogon] DCNM Token Expiry: {}".format(self.DCNM_EXPIRY)) logging.debug("[aclm][userLogon] Status: {}".format(r.status_code)) logging.debug("[aclm][userLogon] Request URL: {}".format(r.url)) logging.debug("[aclm][userLogon] Request Headers: {}".format(r.request.headers)) if r.status_code != 200: ## Errorlogging.debug("Status: {}".format(r.status)) #raise Exception("Status: {} Response: {}".format(r.status_code, r.text)) self.DCNM_SESSION = None raise HTTPError(r.status_code, r.text, r.headers) else: # return {"Dcnm-Token": XXX } logging.debug("[aclm][userLogon] Json: {}".format(r.json())) self.DCNM_TOKEN = r.json()['Dcnm-Token'] if self.DCNM_TOKEN == None: raise Exception("DCNM Token not found - Exiting") else: return
def test_change_credentials_basic(self, m_req): """ Test changing credentials when using basic access authentication. """ # mock 200 m_response_ok = mock.MagicMock() m_response_ok.json.return_value = ['animaldb'] # mock 401 m_response_bad = mock.MagicMock() m_response_bad.raise_for_status.side_effect = HTTPError( '401 Unauthorized') m_req.side_effect = [m_response_bad, m_response_ok] client = Cloudant('foo', 'bar', url=self.url, use_basic_auth=True) client.connect() self.assertIsInstance(client.r_session, BasicSession) with self.assertRaises(HTTPError): client.all_dbs() # expected 401 m_req.assert_called_with( 'GET', self.url + '/_all_dbs', allow_redirects=True, auth=('foo', 'bar'), # uses HTTP Basic Auth timeout=None) # use valid credentials client.change_credentials('baz', 'qux') all_dbs = client.all_dbs() m_req.assert_called_with( 'GET', self.url + '/_all_dbs', allow_redirects=True, auth=('baz', 'qux'), # uses HTTP Basic Auth timeout=None) self.assertEquals(all_dbs, ['animaldb'])
def delete_pipeline_run(organization_uuid, organization_pipeline_uuid, organization_pipeline_run_uuid): """Delete a OrganizationPipelineRun. Note: assumes that the organization_uuid has already been verified (by validate_organization() mixin) Raises a an HTTPError when there is some unrecoverable downstream error. Raises a ValueError when there is some downstream error (its args[0] contains the json message from the backing server) """ org_pipeline = find_organization_pipeline(organization_uuid, organization_pipeline_uuid) if not org_pipeline: raise ValueError({"message": "organizational_pipeline_uuid not found"}) org_pipeline_run = next( filter( lambda r: r.uuid == organization_pipeline_run_uuid, org_pipeline.organization_pipeline_runs, )) response = requests.delete( f"{current_app.config[WORKFLOW_HOSTNAME]}/v1/pipelines/{org_pipeline.pipeline_uuid}/runs/{org_pipeline_run.pipeline_run_uuid}", headers={ "Content-Type": "application/json", ROLES_KEY: current_app.config[WORKFLOW_API_TOKEN], }, ) try: response.raise_for_status() org_pipeline_run.is_deleted = True db.session.commit() except ValueError as value_error: raise HTTPError("Non JSON payload returned") from value_error except HTTPError as http_error: raise ValueError(response.json()) from http_error
def _post_data(data, user_id): if not data.get("domain"): raise ValueError("Expected domain") if not user_id: notify_exception( None, "Making smsforms request w/o user_id. Will result in non-sticky session.", details={ 'session-id': data.get('session-id'), }) data = _get_formplayer_session_data(data) data_bytes = json.dumps(data).encode('utf-8') response = requests.post( url="{}/{}".format(get_formplayer_url(), data["action"]), data=data_bytes, headers={ "Content-Type": "application/json", "content-length": str(len(data_bytes)), "X-MAC-DIGEST": get_hmac_digest(settings.FORMPLAYER_INTERNAL_AUTH_KEY, data_bytes), "X-FORMPLAYER-SESSION": user_id, }, ) if response.status_code == 404: raise Http404(response.reason) if 500 <= response.status_code < 600: http_error_msg = '{} Server Error: {} for url: {}'.format( response.status_code, response.reason, response.url) notify_error(http_error_msg, details={ 'response': response, 'body': response.text }) raise HTTPError(http_error_msg, response=response) return response.json()
def _check_response(self, req): """ Raise an error if our responses are not what we expect. This is a protected method that should only be used by methods making API calls. The intention is to check out responses for a successful HTTP status code along with a successful return from the Alert Manager API. Parameters ---------- req : requests.Response This is the response object we want to verify. Returns ------- boolean Return True if response check is successful. Raises ------ ValueError Raise a value error if the 'status' key of our response is in our list of error statuses from Alert Manager. HTTPError Raise an http error if our response objects status_code attribute is not in requests.codes.ok (basically not a 200). """ if (req.status_code == requests.codes.ok and req.json()['status'] in self.SUCCESS_STATUSES): return True elif (req.status_code == requests.codes.ok and req.json()['status'] in self.ERROR_STATUSES): raise ValueError('{} ==> {}'.format(req.json()['errorType'], req.json()['error'])) else: raise HTTPError('{} ==> {}'.format(req.status_code, req.text))
def get(self, resource, keys, url_prefix, auth, session, send_opts): """Get metadata key-value pairs associated with the given resource. Args: resource (intern.resource.boss.BossResource): Get key-value pairs associated with this resource. keys (list): Keys to retrieve. url_prefix (string): Protocol + host such as https://api.theboss.io auth (string): Token to send in the request header. session (requests.Session): HTTP session to use for request. send_opts (dictionary): Additional arguments to pass to session.send(). Returns: (dictionary): The requested metadata for the given resource. Raises: HTTPErrorList on failure. """ resDict = {} success = True exc = HTTPErrorList('At least one key-value update failed.') for key in keys: req = self.get_metadata_request(resource, 'GET', 'application/json', url_prefix, auth, key) prep = session.prepare_request(req) resp = session.send(prep, **send_opts) if resp.status_code == 200: resDict[key] = resp.json()['value'] else: err = ('Get failed on {}, got HTTP response: ({}) - {}'.format( resource.name, resp.status_code, resp.text)) exc.http_errors.append( HTTPError(err, request=req, response=resp)) success = False if not success: raise exc return resDict
def _validate_cloudflare_response(response, on_success): try: payload = response.json() except ValueError: raise ValueError( f"Cloudflare API response did not contain valid JSON: {response.content}" ) try: try: response.raise_for_status() except HTTPError as exception: raise HTTPError(f"{exception}: {payload['errors'][0]['message']}") if payload["success"]: return on_success(payload) else: raise ValueError( f"Operation failed: {payload['errors'][0]['message']}") except (KeyError, IndexError): raise ValueError( f"Cloudflare API response was malformed: {response.content}")
async def test_oauth_with_follow(hass): """Test state with oauth and follow.""" twitch_mock = MagicMock() twitch_mock.users.translate_usernames_to_ids.return_value = [USER_ID] twitch_mock.channels.get_by_id.return_value = CHANNEL_OBJECT twitch_mock._oauth_token = True # A replacement for the token twitch_mock.users.get.return_value = OAUTH_USER_ID twitch_mock.users.check_subscribed_to_channel.side_effect = HTTPError() twitch_mock.users.check_follows_channel.return_value = FOLLOW_ACTIVE with patch( "homeassistant.components.twitch.sensor.TwitchClient", return_value=twitch_mock, ): assert await async_setup_component(hass, sensor.DOMAIN, CONFIG_WITH_OAUTH) await hass.async_block_till_done() sensor_state = hass.states.get(ENTITY_ID) assert sensor_state.attributes["subscribed"] is False assert sensor_state.attributes["following"] is True assert sensor_state.attributes["following_since"] == "2020-01-20T21:22:42"
def register_addresses(firewall, block_type, id, ip_list, tag): """Register IP with FortiGate if not already registered""" tagged = contains_tag(block_type, id, 'Blocked:manual') for ip in ip_list: address = firewall.get_firewall_address(ip) if type(address) == int: if address == 404: data = json.dumps({ 'name': ip, 'type': 'iprange', 'start-ip': ip, 'end-ip': ip }) firewall.create_firewall_address(ip, data) address = firewall.get_firewall_address(ip)[0] LOGGER.debug('Address %s registered with FortiGate', address['name']) if not tagged: # only tag if new address is created, and only once update_tags(block_type, id, append=tag) tagged = True else: raise HTTPError(address, 'Error retrieving address data')
def side_effect(org_id): err = HTTPError() err.response = mock.Mock(status_code=403) raise TembaAPIError(caused_by=err)