def test_connection_failure(self): responses.add(responses.GET, 'http://example.com', body=RequestException()) with pytest.raises(BadSource): fetch_file('http://example.com') assert len(responses.calls) == 1 # ensure we use the cached domain-wide failure for the second call with pytest.raises(BadSource): fetch_file('http://example.com/foo/bar') assert len(responses.calls) == 1
def list(self): uri = self.rootUri + "/list" parameters = {} result = self.netraClient.request(uri, HttpMethod.POST, params=parameters) if result['errCode'] != 0: raise RequestException(result['errMsg']) if 'data' in result: return True, result['data'] return True, None
def test_retry_triggered(self): exc = RequestException() exc.response = mock.Mock() exc.request = mock.Mock() self.mock_pusher.return_value.push.side_effect = exc with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'), \ mock.patch('inspirehep.modules.orcid.tasks.orcid_push.retry', side_effect=RequestException) as mock_orcid_push_task_retry, \ pytest.raises(RequestException): orcid_push(self.orcid, self.recid, self.oauth_token) self.mock_pusher.assert_called_once_with(self.orcid, self.recid, self.oauth_token) self.mock_pusher.return_value.push.assert_called_once() mock_orcid_push_task_retry.assert_called_once()
def test_make_request_should_raise_general_exception_for_RequestException( mocker): mock_session = mocker.patch('lojaintegrada.requests.Session') mock_request = mock.Mock() mock_session.return_value.request.side_effect = RequestException( request=mock_request) api = Api(api_key='fake-api-key', app_key='fake-app-key') with pytest.raises(ApiError) as e: res = api._make_request( 'GET', 'https://api.awsli.com.br/api/v1/pedido/search') assert e.value.request == mock_request
def changeEmail(self, user, email): uri = self.rootUri + "/changeEmail" parameters = {'user': user, 'email': email} result = self.netraClient.request(uri, HttpMethod.POST, params=parameters) if result['errCode'] != 0: raise RequestException(result['errMsg']) if 'data' in result: return True, result['data'] return True, None
def _connection(self, url, username, password, post=False, data=None): """ Main connection method Returns: session object if got succesfull HTTP response with _is_request_ok() """ if username: self._LOGIN_KEYS['username'] = username if password: self._LOGIN_KEYS['password'] = password try: if post: r = self._session.post(url, headers=self._HEADERS, timeout=self._connection_timeout, data=data) else: r = self._session.get(url, headers=self._HEADERS, timeout=self._connection_timeout, data=data) self._soup = self._bs4(r) if not self._is_request_ok(r): raise RequestException('Something wrong with request') return r except (RequestException or ConnectionError or ConnectTimeout or SSLError or MissingSchema or RetryError or ProxyError or InvalidHeader or ReadTimeout or UnrewindableBodyError or ChunkedEncodingError or HTTPError or StreamConsumedError or Timeout or TooManyRedirects or ContentDecodingError) as e: raise RequestException('Error making request: ' + str(e))
def test_handle_exception(self): tests = [(RequestException(), [''], "7", "7"), (ConnectTimeout(), [''], "7", "7"), (RequestsConnectionError(), [''], "7", "7"), (Exception("Any other exception"), ["Any other exception"], "8 Any other exception", "8" ), ] for exception, exp_data, exp_english, exp_code in tests: self.check_st_exception(ConnectionError, exp_data, exp_english, exp_code, self.http_client._handle_exception, func_args=(exception,))
def __showInformationImage(self, url=None): self.Picture.clear() if url is not None: try: res = getURL(url, timeout=20) if res.status_code != 200: raise RequestException() image = QImage.fromData(res.content) self.Picture.setPixmap(QPixmap.fromImage(image)) except RequestException: self.Picture.setText('海报加载失败') else: self.Picture.setText('暂无海报')
def create(self, groupName): uri = self.rootUri + "/create" parameters = {'groupName': groupName} result = self.netraClient.request(uri, HttpMethod.POST, params=parameters) if result['errCode'] != 0: raise RequestException(result['errMsg']) if 'data' in result: return True, result['data'] return True, None
def delete(self, url, expected_status_code=requests.codes.ok, timeout=30, **kwargs): '''DELETE REST Command to delete information from the device Arguments --------- url (string): API url expected_status_code (int): Expected result timeout (int): Maximum time ''' if not self.connected: raise Exception("'{d}' is not connected for " "alias '{a}'".format(d=self.device.name, a=self.alias)) full_url = '{f}{url}'.format(f=self.url, url=url) log.debug("Sending DELETE command to '{d}':"\ "\nurl: {url}".format(d=self.device.name, url=full_url)) # Send to the device response = self.session.delete(full_url, auth=(self.username, self.password), headers=self.headers, timeout=timeout, **kwargs) try: output = response.json() except Exception: output = response.text log.info("Output received:\n{output}".format(output=output)) # Make sure it returned requests.codes.ok if response.status_code != expected_status_code: # Something bad happened raise RequestException("'{c}' result code has been returned " "instead of the expected status code " "'{e}' for '{d}'"\ .format(d=self.device.name, c=response.status_code, e=expected_status_code)) return output
def __init__(self, filmID, parent=None, flags=Qt.WindowFlags()): super().__init__(parent=parent, flags=flags) self.setupUi(self) directorFetcher = FilmInterface(False) directorFetcher.selectFilmDirectingInfo(filmID) result = directorFetcher.fetchResult() self.tableWidget.setRowCount(len(result)) self.alts = {} for index, row in enumerate(result): avatar = getColumn(row, DIRECTOR_TABLE.avatar) name = getColumn(row, DIRECTOR_TABLE.name) role = getColumn(row, DIRECTING_TABLE.role) alt = getColumn(row, DIRECTOR_TABLE.alt) if avatar is not None: try: avatarResponse = getURL(avatar, timeout=20) if avatarResponse.status_code != 200: raise RequestException() avatarImage = QImage.fromData(avatarResponse.content) avatarItem = QTableWidgetItem( QIcon(QPixmap.fromImage(avatarImage)), '') except RequestException: avatarItem = QTableWidgetItem('海报加载失败') else: avatarItem = QTableWidgetItem('暂无海报') self.tableWidget.setItem(index, 0, avatarItem) self.tableWidget.setItem( index, 1, QTableWidgetItem('--' if name is None else name)) self.tableWidget.setItem( index, 2, QTableWidgetItem('--' if role is None else role)) if alt is None: self.tableWidget.setItem(index, 3, QTableWidgetItem('--')) else: self.tableWidget.setItem(index, 3, QTableWidgetItem(alt)) self.alts[index] = alt self.tableWidget.setIconSize(QSize(90, 127)) self.tableWidget.horizontalHeader().setSectionResizeMode( QHeaderView.ResizeToContents) self.tableWidget.verticalHeader().setSectionResizeMode( QHeaderView.ResizeToContents)
def show_data(data): title_list=[] if data.get('items'): for item in data['items']: title = item.get('volumeInfo').get('title') if title in title_list: continue else: title_list.append(title) author=",".join(data['items'][0].get('volumeInfo').get('authors')) print("{},{}".format(title.encode('utf-8'), author.encode('utf-8'))) else: raise RequestException('Search did not return anything! .. ') return
def get_url_content(url:str): """ Method returns raw html content from given url Arguments: url (str) Returns: str -- raw html from given url """ try: with closing(get(url, stream=True)) as reply: if reply.status_code == 200: return reply.content else: return None except RequestException as e: raise RequestException(f'Error during requests to {url} : {str(e)}')
def test_pytest_configure_on_conn_error(mocked_get, mocked_config): """Test plugin configuration in case of HTTP error. The value of the _reportportal_configured attribute of the pytest Config object should be changed to False, stopping plugin configuration, if HTTP error occurs getting HTTP response from the ReportPortal. :param mocked_get: Instance of the MagicMock :param mocked_config: Pytest fixture """ mock_response = mock.Mock() mock_response.raise_for_status.side_effect = RequestException() mocked_get.return_value = mock_response mocked_config.option.rp_enabled = True mocked_config.option.rp_skip_connection_test = 'False' pytest_configure(mocked_config) assert mocked_config._rp_enabled is False
def test_import_ssh_keys_crashes_for_RequestException(self): protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH] ) auth_id = factory.make_name("auth_id") ks = "%s:%s" % (protocol, auth_id) mock_get_protocol_keys = self.patch( keysource_module, "get_protocol_keys" ) mock_get_protocol_keys.side_effect = RequestException("error") response = self.client.post( reverse("sshkeys_handler"), data=dict(op="import", keysource=ks) ) self.assertEqual( http.client.BAD_REQUEST, response.status_code, response.content )
def test_createadmin_raises_ssh_key_error(self): stderr = StringIO() stdout = StringIO() username = factory.make_string() password = factory.make_string() email = '*****@*****.**' % factory.make_string() ssh_import = "%s:%s" % ( random.choice([KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]), factory.make_name('user-id')) self.patch( keysource_module.KeySource.objects, 'save_keys_for_user').side_effect = (RequestException('error')) self.assertRaises( createadmin.SSHKeysError, call_command, 'createadmin', username=username, password=password, email=email, ssh_import=ssh_import, stderr=stderr, stdout=stdout)
def wrapper(*args, **kwargs): try: result = method(*args, **kwargs) return result except ProxyError: _LOGGER.exception('ProxyError when try to get %s.', args) raise ProxyError('A proxy error occurred.') except ConnectionException: _LOGGER.exception('ConnectionError when try to get %s.', args) raise ConnectionException('DNS failure, refused connection, etc.') except Timeout: _LOGGER.exception('Timeout when try to get %s', args) raise Timeout('The request timed out.') except RequestException: _LOGGER.exception('RequestException when try to get %s.', args) raise RequestException('Please check out your network.')
def test_get_token_raises_request(self, mock_config): mock_config.return_value.session.post.side_effect = RequestException() mock_config.return_value.config_file = { 'domain': ['fake_domain'], 'vcac_servers': { 'PRD': '' }, 'tenant': { 'PRD': '' } } with self.assertRaises(VraSdkRequestException): VraAuthenticate('PRD').get_token("", "") mock_config.assert_called_once()
def test_webhook_request_error_send_message(mock_post, app): valid_webhook_message = {"message": "valid"} mock_post.return_value = mock_response( 500, "500 Internal Server Error", raise_exception=RequestException("500 INTERNAL SERVER ERROR")) response = webhook.send_message(app.config["WEBHOOK_URL"], valid_webhook_message, app.logger) url = mock_post.call_args[0][0] message = mock_post.call_args[1]['json'] assert mock_post.called assert url == app.config["WEBHOOK_URL"] assert message == valid_webhook_message assert response is None
def test_push_to_registry_does_not_override_when_cant_check_status( mock_log_audit, mock_log, mock_run, mock_validate_service_name, mock_build_command, mock_is_docker_image_already_in_registry, mock_load_system_paasta_config, ): args, _ = parse_args(['push-to-registry', '-s', 'foo', '-c', 'abcd' * 10]) mock_run.return_value = (0, 'Success') mock_is_docker_image_already_in_registry.side_effect = RequestException() assert paasta_push_to_registry(args) == 1 assert not mock_build_command.called assert not mock_run.called assert not mock_log_audit.called
def test_bugzilla_error_creates_error_notification(self): self.assertEqual(Notification.objects.count(), 0) self.mock_bugzilla_requests_post.side_effect = RequestException() with self.assertRaises(bugzilla.BugzillaError): with MetricsMock() as mm: tasks.create_experiment_bug_task(self.user.id, self.experiment.id) self.assertTrue( mm.has_record( markus.INCR, "experiments.tasks.create_experiment_bug.started", value=1, ) ) self.assertTrue( mm.has_record( markus.INCR, "experiments.tasks.create_experiment_bug.failed", value=1, ) ) # Failures should abort timing metrics. self.assertFalse( mm.has_record( markus.TIMING, "experiments.tasks.create_experiment_bug.timing" ) ) # Completed metric should not be sent. self.assertFalse( mm.has_record( markus.INCR, "experiments.tasks.create_experiment_bug.completed" ) ) self.mock_bugzilla_requests_post.assert_called() self.assertEqual(Notification.objects.count(), 1) experiment = Experiment.objects.get(id=self.experiment.id) self.assertEqual(experiment.bugzilla_id, None) notification = Notification.objects.get() self.assertEqual(notification.user, self.user) self.assertEqual( notification.message, tasks.NOTIFICATION_MESSAGE_CREATE_BUG_FAILED )
def _execute_request(self, method: str, url: str, params: dict, **kwargs): """Function to execute and handle a request.""" # Execute Request try: if method == "GET": encoded_params = "&".join(f"{key}={quote(str(value))}" for key, value in params.items()) response = self._session.get(url, params=encoded_params, timeout=self._timeout, **kwargs) elif method == "POST": data = {} data.update(params) data.update(kwargs.pop("data", {})) data["mimeType"] = "application/json" kwargs["data"] = data self._debuglog("POST data: " + str(data)) response = self._session.post(url, params=params, timeout=self._timeout, **kwargs) self._debuglog("Request url: " + response.url) self._debuglog("Request status_code: " + str(response.status_code)) self._debuglog("Request headers: " + str(response.headers)) if response.status_code == 200: # We got a DSM response content_type = response.headers.get("Content-Type", "").split(";")[0] if content_type in [ "application/json", "text/json", "text/plain", # Can happen with some API ]: return response.json() return response.content # We got a 400, 401 or 404 ... raise RequestException(response) except (RequestException, JSONDecodeError) as exp: raise SynologyDSMRequestException(exp) from exp
def post(self, dn, payload, expected_status_code=requests.codes.ok, timeout=30): '''POST REST Command to configure information from the device Arguments --------- dn (string): Unique distinguished name that describes the object and its place in the tree. payload (dict): Dictionary containing the information to send via the post expected_status_code (int): Expected result timeout (int): Maximum time ''' if not self.connected: raise Exception("'{d}' is not connected for " "alias '{a}'".format(d=self.device.name, a=self.alias)) # Deal with the dn full_url = '{f}{dn}'.format(f=self.url, dn=dn) log.info("Sending POST command to '{d}':"\ "\nDN: {furl}\nPayload:{p}".format(d=self.device.name, furl=full_url, p=payload)) # Send to the device response = self.session.post(full_url, payload, timeout=timeout, \ verify=False) output = response.json() log.info("Output received:\n{output}".format(output=output)) # Make sure it returned requests.codes.ok if response.status_code != expected_status_code: # Something bad happened raise RequestException("'{c}' result code has been returned " "instead of the expected status code " "'{e}' for '{d}', got:\n {msg}"\ .format(d=self.device.name, c=response.status_code, e=expected_status_code, msg=response.text)) return output
def test_stop_plugin_configuration_on_conn_error(mocked_get, mocked_config): """Test plugin configuration in case of HTTP error. The value of the _reportportal_configured attribute of the pytest Config object should be changed to False, stopping plugin configuration, if HTTP error occurs getting HTTP response from the ReportPortal. :param mocked_get: Instance of the MagicMock :param mocked_config: Pytest fixture """ mock_response = mock.Mock() mock_response.raise_for_status.side_effect = RequestException() mocked_get.return_value = mock_response expect(pytest_configure(mocked_config) is None, 'Received unexpected return value from pytest_configure.') expect(mocked_config._reportportal_configured is False, 'The value of the _reportportal_configured is not False.') assert_expectations()
async def _handleReturned(self, data): ''' Handles returned data from musicbrainz data must be valid json''' try: out = _loads(data) except Exception as e: self.logger.error('in func: %s \nException: %s' % (__name__, e)) raise else: if 'error' in out: err = RequestException('MusicbrainzError %s' % (out['error'])) self.logger.error('in func: %s \nException: %s' % (__name__, err)) return out return None
def test_ds_bug_fails_updating_with_exp_bug_id(self): self.assertEqual(Notification.objects.count(), 0) self.mock_bugzilla_requests_put.side_effect = RequestException() tasks.update_exp_id_to_ds_bug_task(self.user.id, self.experiment.id) self.mock_bugzilla_requests_put.assert_called_with( self.expected_url, self.expected_data) self.assertEqual(Notification.objects.count(), 1) notification = Notification.objects.get() self.assertEqual(notification.user, self.user) self.assertEqual( notification.message, tasks.NOTIFICATION_MESSAGE_DS_UPDATE_ERROR.format( bug_url=self.experiment.data_science_bugzilla_url), )
def get(self, url, \ expected_status_code=requests.codes.ok, timeout=30, **kwargs): '''GET REST Command to retrieve information from the device Arguments --------- url (string): REST API url expected_status_code (int): Expected result ''' if not self.connected: raise Exception("'{d}' is not connected for " "alias '{a}'".format(d=self.device.name, a=self.alias)) full_url = "{f}{url}"\ .format(f=self.url, url=url) log.debug("Sending GET command to '{d}':"\ "\nurl: {url}".format(d=self.device.name, url=full_url)) response = self.session.get(full_url, auth=(self.username, self.password), timeout=timeout, headers=self.headers, **kwargs) try: output = response.json() except Exception: output = response.text log.info("Output received:\n{output}".format(output=output)) # Make sure it returned requests.codes.ok if response.status_code != expected_status_code: # Something bad happened raise RequestException("Sending '{furl} to '{d} has returned the " "following code '{c}', instead of the " "expected status code '{e}'" "'{e}'".format(furl=full_url, d=self.device.name, c=response.status_code, e=expected_status_code)) return output
def fetch_url_from_github(self, url, as_user=None, requestor_id=None, **kwargs): if "method" in kwargs: method = kwargs.pop("method") else: method = "GET" if method.upper() == "HEAD": kwargs.setdefault("allow_redirects", False) username = "******" if as_user: github.blueprint.config["user"] = as_user username = "******".format(login=as_user.login) elif requestor_id: github.blueprint.config["user_id"] = int(requestor_id) username = "******".format(requestor_id) logger.info("{method} {url} as {username}".format( method=method, url=url, username=username, )) try: resp = github.request(method=method, url=url, **kwargs) except RateLimited as exc: logger.info("rate limited: {url}".format(url=url)) # if this task is being executed inline, let the exception raise # so that Flask's error-handling mechanisms can catch it if self.request.is_eager: raise # otherwise, schedule this task to retry when the rate limit is reset else: logger.warn("Retrying {url} at {reset}".format(url=url, reset=exc.reset)) self.retry(exc=exc, eta=exc.reset) if resp.status_code == 404: logger.info("not found: {url}".format(url=url)) raise NotFound(url) if not resp.ok: raise RequestException(resp.text) return resp
def _get_auth_token(self, r): token_base = self._find_bearer_base(r) token_request_url = urljoin(token_base, "token") post_data = { "grant_type": "password", "username": self.username, "password": self.password } token_response = requests.post(token_request_url, data=post_data) try: return token_response.json()['access_token'] except Exception: raise RequestException( None, r, "Unable to retrieve access token for request: %s" % token_response.content)
def node_get(self, namespace): """ Returns the DPN node with the specified namespace. :param namespace: The namespace of the node. ('tdr', 'sdr', etc.) :returns: requests.Response :raises RequestException: Check the response property for details. """ url = "{0}/api-v1/node/{1}/".format(self.url, namespace) response = requests.get(url, headers=self.headers(), verify=self.verify_ssl) if response.status_code != 200: raise RequestException(response.text, response=response) return response