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()
async def test_async_step_finish_error(hass: HomeAssistant) -> None: """Test finish step with error.""" with patch("pyvera.VeraController") as vera_controller_class_mock: controller = MagicMock() controller.refresh_data = MagicMock(side_effect=RequestException()) vera_controller_class_mock.return_value = controller result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={CONF_CONTROLLER: "http://127.0.0.1:123/"}, ) assert result["type"] == "abort" assert result["reason"] == "cannot_connect" assert result["description_placeholders"] == { "base_url": "http://127.0.0.1:123" }
def try_fetch(url, session): response = session.get(url) if response is None: # trap no response raise RequestException() # status codes above 203 are reduced content status codes - that is bad if response.status_code >= 204: # trap bad status code raise response.raise_for_status() try: json = response.json() except JSONDecodeError as json_err: # put the response text on the exception json_err.doc = response.text raise json_err return json
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.debug("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 post_request(url, data, retry_count=10, cookies=None): ip = get_proxy() # 获取代理ip proxy = {'https': "https://{}".format(str(ip, encoding="utf-8"))} # proxy = {'https': "http://180.124.36.47:20968"} # proxy = {'https': "http://111.74.220.112:4217"} while retry_count > 0: header = { 'User-Agent': random.choice(user_agent_list), 'Referer': 'www.aihuishou.com', 'Connection': 'keep-alive', 'Accept': "*/*", 'Origin': 'https://www.aihuishou.com', # 'Accept-Encoding': 'gzip, deflate, br', # 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', # 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', # 'Sec-Fetch-Site': 'same-origin', # 'X-Requested-With': 'XMLHttpRequest' } # print("当前代理ip:{}".format(proxy)) try: # 使用代理访问 resp = requests.post(url, headers=header, data=data, proxies=proxy, timeout=6, cookies=cookies) if resp.status_code == 502: raise RequestException("502 badGateWay") return resp except (RequestException, Timeout) as e: print("{}, 切换到下一个代理ip进行处理".format(e)) delete_proxy(ip) # 删除有问题的代理ip ip = get_proxy() # 获取代理ip proxy = {'https': "http://{}".format(str(ip, encoding="utf-8"))} retry_count -= 1 except Exception as e: print(e) retry_count -= 1 # 出错n次, 删除代理池中代理 # delete_proxy(proxy) return None
def get_company_url(code:str)->str: """ Method for retrieving URL in Rekvizitai.lt website of any commercial company in Lithunia by company code Arguments: code (str) -- Litnianian company code Returns: str -- ural of desired company """ params = {'name':'', 'city':0,'word':'','code':code,'catUrlKey':'','ok':'','resetFilter':0,'order':1} url = "https://rekvizitai.vz.lt/en/companies/1" try: with closing(post(url, params)) as response: if response.status_code == 200: soup = BeautifulSoup(response.content, 'html.parser') return soup.find_all("div", class_="info")[0].find('a').get('href') else: return None except RequestException as e: raise RequestException(f'Error during requests to {url} : {str(e)}')
def test_stop_plugin_configuration_on_conn_error(mocked_get): """Test plugin configuration in case of HTTP error. The value of the _reportportal_configured attribute of the pytest Config object should be change to False, stopping plugin configuration, if HTTP error occurs getting HTTP response from the ReportPortal. :param mocked_get: Instance of the MagicMock """ mocked_config = create_autospec(Config) mocked_config.return_value._reportportal_configured = True mock_response = 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()
def __init__(self, filmID, parent=None, flags=Qt.WindowFlags()): super().__init__(parent=parent, flags=flags) self.setupUi(self) castFetcher = FilmInterface(False) castFetcher.selectFilmPlayInfo(filmID) result = castFetcher.fetchResult() self.tableWidget.setRowCount(len(result)) self.alts = {} for index, row in enumerate(result): avatar = getColumn(row, CAST_TABLE.avatar) name = getColumn(row, CAST_TABLE.name) role = getColumn(row, PLAY_TABLE.role) alt = getColumn(row, CAST_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, '--') 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 test_request__really_not_ok(self, mocked_super_request, mocked_logger): raise_for_status = mock.Mock() mocked_super_request.return_value = mock.Mock( headers={"Content-Length": 1024}, raise_for_status=raise_for_status, ) raise_for_status.side_effect = RequestException() wrapper = SessionWrapper() with self.assertRaises(RequestException): wrapper.request("GET", "test_url", is_test=True) mocked_super_request.assert_called_once_with("GET", "test_url", is_test=True) mocked_logger.error.assert_called_once_with( "RequestException Reason: (no response)")
def check_status_code(status_code, allowed_codes=(status.HTTP_200_OK, )): """ TODO: À renseigner """ if status_code == status.HTTP_403_FORBIDDEN: raise PermissionDenied(_('Accès interdit.')) if status_code == status.HTTP_404_NOT_FOUND: raise Http404(_('Cette fiche n’existe pas.')) if status_code == status.HTTP_409_CONFLICT: raise UserMessageError(_('Une fiche avec ce code existe déjà.')) if status.HTTP_400_BAD_REQUEST <= status_code < status.HTTP_500_INTERNAL_SERVER_ERROR: raise RequestException() if status_code not in allowed_codes: raise Exception(HTTP_ERRORS[status_code])
def download_image(const: Constant, img, session): url = const.img_url_prefix + img filename = os.path.join(const.saved_dir, img) if (not const.overwrite) and os.path.isfile(filename): return url try: resp = session.get(url, cookies=const.cookies, proxies=const.proxies, stream=True, timeout=const.timeout) if resp.status_code != 200: raise Exception(f'Response status code: {resp.status_code}') except Exception as err: raise RequestException(err) with open(filename, 'wb') as f: for chunk in resp.iter_content(chunk_size=8192): if chunk: f.write(chunk) return url
def _send_request(ses, url, retry=5, sleep=30, timeout=120, *args, **kwargs): for retry_idx in range(retry): try: params = kwargs.get('params', {}) data = kwargs.get('data', {}) method = kwargs.get('method', 'GET') if method == 'GET': res = ses.get(url, params=params, timeout=timeout) else: res = ses.post(url, data=data, timeout=timeout) if res.status_code != 200: raise RequestException('Response not OK for {}'.format(url)) return res except RequestException: if retry_idx == retry - 1: raise time.sleep(retry_idx * sleep + random.random() * sleep) except Exception: raise
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 restore_get(self, restore_id): """ Returns the restore request with the specified event id. *** RESTORE IS NOT YET IMPLEMENTED *** :param obj_id: The restore_id of the restore request. :returns: requests.Response :raises RequestException: Check the response property for details. """ url = "{0}/api-v1/restore/{1}/".format(self.url, restore_id) response = requests.get(url, headers=self.headers(), verify=self.verify_ssl) if response.status_code != 200: raise RequestException(response.text, response=response) return response
def changePassword(self, user, password, rePassword): uri = self.rootUri + "/changePassword" parameters = { 'user': user, 'password': password, 'rePassword': rePassword } 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 bag_update(self, obj): """ Updates a bag entry. Only the repository admin can make this call, which means you can issue this call only against your own node. :param obj: The object to create. :returns: requests.Response :raises RequestException: Check the response property for details. """ url = "{0}/api-v1/bag/{1}/".format(self.url, obj['dpn_object_id']) response = requests.put(url, headers=self.headers(), data=json.dumps(obj), verify=self.verify_ssl) if response.status_code != 200: raise RequestException(response.text, response=response) return response
def get_token(self, client_id, client_secret, audience="https://pmd", grant_type="client_credentials"): """Authenticate using client id and secret.""" response = requests.post(AUTH0_URL, headers={"content-type": "application/json"}, json={ "client_id": client_id, "client_secret": client_secret, "audience": audience, "grant_type": grant_type }) if response.status_code == 200: self.access_token = response.json()["access_token"] else: raise RequestException(str(response.content, "utf-8"))
def crawl_image(const: Constant, url, session): try: resp = session.get(url, cookies=const.cookies, proxies=const.proxies, stream=True, timeout=const.timeout) except Exception as err: raise RequestException(err) try: soup = BeautifulSoup(resp.json().get('data'), 'html.parser') for a in soup.find_all('a', class_='ph_ar_box'): img = const.rex_pattern.search(a.find('img').get('src')).group(0) downloading_jobs.put(img) except Exception as err: raise CookiesExpiredException('Cookie has expired, please get a new one and paste to here:\n') try: const.photo_api.action_data = soup.find('div', class_='WB_cardwrap').get('action-data') except Exception as err: raise NoImagesException('No more images to crawl')
def test_fiat_too_many_requests_response(mocker, caplog): # Because CryptoToFiatConverter is a Singleton we reset the listings req_exception = "429 Too Many Requests" listmock = MagicMock(return_value="{}", side_effect=RequestException(req_exception)) mocker.patch.multiple( 'freqtrade.rpc.fiat_convert.CoinGeckoAPI', get_coins_list=listmock, ) # with pytest.raises(RequestEsxception): fiat_convert = CryptoToFiatConverter() fiat_convert._coinlistings = {} fiat_convert._load_cryptomap() assert len(fiat_convert._coinlistings) == 0 assert fiat_convert._backoff > datetime.datetime.now().timestamp() assert log_has( 'Too many requests for Coingecko API, backing off and trying again later.', caplog )
def transfer_create(self, obj): """ Creates a transfer request. Only the repository admin can make this call, which means you can issue this call only against your own node. :param obj: The request to create. :returns: requests.Response :raises RequestException: Check the response property for details. """ url = "{0}/api-v1/replicate/".format(self.url) response = requests.post(url, headers=self.headers(), data=json.dumps(obj), verify=self.verify_ssl) if response.status_code != 201: raise RequestException(response.text, response=response) return response
def _execute_request(self, method, url, params, **kwargs): """Function to execute and handle a request.""" # Execute Request try: if method == "GET": if six.PY2: items = params.iteritems() else: items = params.items() encoded_params = "&".join( "%s=%s" % (key, quote(str(value))) for key, value in items ) response = self._session.get( url, params=encoded_params, timeout=self._timeout, **kwargs ) elif method == "POST": 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)
def restore_update(self, obj): """ Updates a restore request. *** RESTORE IS NOT YET IMPLEMENTED *** :param obj_id: The ID of the restore request (NOT the ID of a DPN bag). :returns: requests.Response :raises RequestException: Check the response property for details. """ url = "{0}/api-v1/restore/{1}/".format(self.url, obj['restore_id']) response = requests.put(url, headers=self.headers(), data=json.dumps(obj), verify=self.verify_ssl) if response.status_code != 200: raise RequestException(response.text, response=response) return response
def upload(self, groupName, personName, imagePathNames): uri = self.rootUri + "/upload" parameters = {'groupName': groupName, 'personName': personName} files = [] for n, imagePathName in enumerate(imagePathNames): files.append(('file' + str(n + 1), open(imagePathName, 'rb'))) result = self.netraClient.request(uri, HttpMethod.PUT, files=files, data=parameters) if result['errCode'] != 0: raise RequestException(result['errMsg']) if 'data' in result: return True, result['data'] return True, None
def test_request_exception_raised(self): responses.add( responses.GET, "http://localhost/api/v1/test", body=RequestException(), ) request = APIRequest(session=Session(), base_url="http://localhost", method="GET", endpoint="/api/v1/test") with self.assertRaises(EndpointRequestError) as e: request.execute() self.assertEqual(e.exception.reason, "an error occurred while executing request") self.assertEqual(e.exception.base_url, "http://localhost") self.assertEqual(e.exception.method, "GET") self.assertEqual(e.exception.endpoint, "/api/v1/test")
def node_list(self, **kwargs): """ Returns a list of DPN nodes. :param replicate_to: Boolean value. :param replicate_from: Boolean value. :param page_size: Number of max results per page. :returns: requests.Response :raises RequestException: Check the response property for details. """ url = "{0}/api-v1/node/".format(self.url) response = requests.get(url, headers=self.headers(), params=kwargs, verify=self.verify_ssl) if response.status_code != 200: raise RequestException(response.text, response=response) return response
def dps_by_jewel_from_report(report): save_dir = "cache/" filestr = str(report) fights_file_ext = "_fights.json" fights_obj = {} try: f = open(save_dir + filestr + fights_file_ext, "r") fights_obj = loads(f.read()) # print("Got report " + str(report) + " fights from file") f.close() except IOError: # print("Could not get report " + str(report) + " fights from file. Requesting") # find the opulence fight(s) from our report # fights contains a list of all the fights, their start and end time, and # what was being fought. see https://www.warcraftlogs.com/v1/docs#!/Report/report_fights_code_get resp = get("https://www.warcraftlogs.com:443/v1/report/fights/" + report, params={ "api_key": key, 'translate': True }) if (resp.status_code == 429): print("Too many requests - raising RequestException") raise RequestException("Too many requests") # other errors elif (resp.status_code != 200): raise Exception("Other non-success response received: " + str(resp.status_code)) fights_obj = resp.json() f = open(save_dir + filestr + fights_file_ext, "w") f.write(dumps(fights_obj)) opulence_kills = [] for fight in fights_obj["fights"]: if fight["boss"] == OPULENCE_ID and fight["kill"] and fight[ "difficulty"] == 3: opulence_kills.append(fight) # get dps for each player for fight in opulence_kills: dps_by_jewel_from_fight(fight, report)
def claim(self): """ Sets the Draftset’s current-owner to be the same as the user performing this operation. This is necessary to prevent other’s from making changes to the data contained within the Draftset. Each role in the system has a pool of 0 or more claimable draftsets associated with it. Claimable draftsets are draftsets in a pool where the rank of the pools role is less than or equal to the user’s role’s rank. """ response = requests.post( f"https://cogs-staging-drafter.publishmydata.com/v1/draftset/{self.id}/claim", headers={ "Accept": "application/json", "Authorization": f"Bearer {self._requester.access_token}" }) if response.status_code == 200: return self._requester.get_draftset(id=self.id) else: raise RequestException(str(response.content, "utf-8"))
def get_govuk_payment_events(self, govuk_id): """ :return: list with events information about a certain govuk payment. :param govuk_id: id of the govuk payment :raise HTTPError: if GOV.UK Pay returns a 4xx or 5xx response :raise RequestException: if the response body cannot be parsed """ response = requests.get( govuk_url(f'/payments/{govuk_id}/events'), headers=govuk_headers(), timeout=15, ) response.raise_for_status() try: data = response.json() return data['events'] except (ValueError, KeyError): raise RequestException('Cannot parse response', response=response)
def send(self, request, **kwargs): # Catch urllib3 warnings for HTTPS related errors. insecure = False with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always') r = super(ArchiveSession, self).send(request, **kwargs) if self.protocol == 'http:': return r insecure_warnings = ['SNIMissingWarning', 'InsecurePlatformWarning'] if w: for e in w: if any(x in str(e) for x in insecure_warnings): insecure = True break if insecure: from requests.exceptions import RequestException msg = ('You are attempting to make an HTTPS request on an insecure platform,' ' please see:\n\n\thttps://internetarchive.readthedocs.org' '/en/latest/troubleshooting.html#https-issues\n') raise RequestException(msg) return r
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 transfer_update(self, obj): """ Updates a transfer request. The only fields in the transfer object relevant to this request are the replication_id, fixity_value, and status, which you must set to either 'A' (Accept) or 'R' (Reject). :param obj_id: The ID of the restore request (NOT the ID of a DPN bag). :returns: requests.Response :raises RequestException: Check the response property for details. """ url = "{0}/api-v1/replicate/{1}/".format(self.url, obj['replication_id']) response = requests.put(url, headers=self.headers(), data=json.dumps(obj), verify=self.verify_ssl) if response.status_code != 200: raise RequestException(response.text, response=response) return response