Python requests.exceptions.RetryError() Examples

The following are 7 code examples of requests.exceptions.RetryError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module requests.exceptions , or try the search function .
Example #1
Source File: http.py    From osbs-client with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def request(self, url, *args, **kwargs):
        try:
            stream = HttpStream(url, *args, verbose=self.verbose, **kwargs)
            if kwargs.get('stream', False):
                return stream

            with stream as s:
                content = s.req.content
                return HttpResponse(s.status_code, s.headers, content)
        # Timeout will catch both ConnectTimout and ReadTimeout
        except (RetryError, Timeout) as ex:
            raise OsbsNetworkException(url, str(ex), '',
                                       cause=ex, traceback=sys.exc_info()[2])
        except HTTPError as ex:
            raise OsbsNetworkException(url, str(ex), ex.response.status_code,
                                       cause=ex, traceback=sys.exc_info()[2])
        except Exception as ex:
            raise OsbsException(cause=ex, traceback=sys.exc_info()[2]) 
Example #2
Source File: http_request.py    From agogosml with MIT License 6 votes vote down vote up
def post_with_retries(url: str, data: dict, retries: int, backoff: float) -> int:
    """
    Make a POST request with retries.

    >>> post_with_retries('http://httpstat.us/503', {}, retries=1, backoff=0)
    500
    >>> post_with_retries('https://httpstat.us/200', {}, retries=1, backoff=0)
    200
    """
    retry_adapter = HTTPAdapter(max_retries=Retry(
        total=retries,
        backoff_factor=backoff,
        status_forcelist=[500, 502, 503, 504],
        method_whitelist=frozenset(['POST'])
    ))

    with Session() as session:
        session.mount('http://', retry_adapter)
        session.mount('https://', retry_adapter)

        try:
            response = session.post(url, data=data)
        except RetryError:
            return 500

        return response.status_code 
Example #3
Source File: util.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_manifest(self, image, version):
        saved_not_found = None
        media_type = get_manifest_media_type(version)
        try:
            response = query_registry(self._session, image, digest=None, version=version)
        except (HTTPError, RetryError) as ex:
            if ex.response is None:
                raise
            if ex.response.status_code == requests.codes.not_found:
                saved_not_found = ex
            # If the registry has a v2 manifest that can't be converted into a v1
            # manifest, the registry fails with status=400 (BAD_REQUEST), and an error code of
            # MANIFEST_INVALID. Note that if the registry has v2 manifest and
            # you ask for an OCI manifest, the registry will try to convert the
            # v2 manifest into a v1 manifest as the default type, so the same
            # thing occurs.
            if version != 'v2' and ex.response.status_code == requests.codes.bad_request:
                logger.warning('Unable to fetch digest for %s, got error %s',
                               media_type, ex.response.status_code)
                return None, saved_not_found
            # Returned if the manifest could not be retrieved for the given
            # media type
            elif (ex.response.status_code == requests.codes.not_found or
                  ex.response.status_code == requests.codes.not_acceptable):
                logger.debug("skipping version %s due to status code %s",
                             version, ex.response.status_code)
                return None, saved_not_found
            else:
                raise

        if not manifest_is_media_type(response, media_type):
            logger.warning("content does not match expected media type")
            return None, saved_not_found
        logger.debug("content matches expected media type")
        return response, saved_not_found 
Example #4
Source File: pre_pull_base_image.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_manifest_list(self, image):
        """try to figure out manifest list"""
        if image in self.manifest_list_cache:
            return self.manifest_list_cache[image]

        reg_client = self._get_registry_client(image.registry)

        manifest_list = reg_client.get_manifest_list(image)
        if '@sha256:' in str(image) and not manifest_list:
            # we want to adjust the tag only for manifest list fetching
            image = image.copy()

            try:
                config_blob = reg_client.get_config_from_registry(image, image.tag)
            except (HTTPError, RetryError, Timeout) as ex:
                self.log.warning('Unable to fetch config for %s, got error %s',
                                 image, ex.response.status_code)
                raise RuntimeError('Unable to fetch config for base image')

            release = config_blob['config']['Labels']['release']
            version = config_blob['config']['Labels']['version']
            docker_tag = "%s-%s" % (version, release)
            image.tag = docker_tag

            manifest_list = reg_client.get_manifest_list(image)
        self.manifest_list_cache[image] = manifest_list
        return self.manifest_list_cache[image] 
Example #5
Source File: test_vocabulary.py    From hdx-python-api with MIT License 5 votes vote down vote up
def vocabulary_delete(url, datadict):
    if 'show' in url:
        return vocabulary_mockshow(url, datadict)
    if 'update' in url:
        resultdictcopy = copy.deepcopy(resultdict)
        resultdictcopy['tags'] = list()
        result = json.dumps(resultdictcopy)
        return MockResponse(200,
                            '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=vocabulary_update"}' % result)
    if 'delete' not in url:
        return MockResponse(404,
                            '{"success": false, "error": {"message": "TEST ERROR: Not delete", "__type": "TEST ERROR: Not Delete Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=vocabulary_delete"}')
    if datadict['id'] == '1731e7fc-ff62-4551-8a70-2a5878e1142b':
        if len(datadict['tags']) == 0:
            return MockResponse(200,
                                '{"success": true, "result": null, "help": "http://test-data.humdata.org/api/3/action/help_show?name=vocabulary_delete"}')
        else:
            raise RetryError(
                "HTTPSConnectionPool(host='test-data.humdata.org', port=443): Max retries exceeded with url: /api/action/vocabulary_delete (Caused by ResponseError('too many 500 error responses',))")

    return MockResponse(404,
                        '{"success": false, "error": {"message": "Not found", "__type": "Not Found Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=vocabulary_delete"}') 
Example #6
Source File: utils.py    From dcard-spider with MIT License 5 votes vote down vote up
def get_json(self, url, **kwargs):
        response = None
        try:
            response = self.session.get(url, **kwargs)
            data = response.json()
            if type(data) is dict and data.get('error'):
                raise ServerResponsedError
            return data
        except ValueError as e:
            retries = kwargs.get('retries', 0)
            logger.error('when get <%d> %s, error %s (retry#%d)',
                         response.status_code, url, e, retries)
            return {} if retries <= self.max_retries else \
                self.get_json(url, retries=retries + 1)
        except ServerResponsedError:
            logger.error('when get <%d> %s, response: %s',
                         response.status_code, url, data)
            return {}
        except httplib.IncompleteRead as e:
            logger.error('when get %s, error %s; partial: %s',
                         url, e, e.partial)
            return {}  # or shall we return `e.partial` ?
        except RetryError as e:
            logger.error('when get %s, retry error occurs. %s', url, e)
            return {}
        except Exception as e:
            logger.error('error %s', e)
            return {} 
Example #7
Source File: mocked.py    From dcard-spider with MIT License 3 votes vote down vote up
def request(*args, **kwargs):

        url = kwargs.get('url') or \
            (args[0] if isinstance(args[0], str) else args[2])
        params = kwargs.get('params')

        if kwargs.get('stream'):
            return StreamResponse('./tests/data/' + 'sample.jpg')

        for i, bundle in enumerate(MockedRequest.mapping):

            regex, path = bundle

            if regex.search(url) is not None:
                json = JsonResponse('./tests/data/' + path)
                if (i == 1
                        and kwargs.get('params')
                        and kwargs.get('params').get('before')):
                    global post_metas_requests
                    post_metas_requests += 1
                    if post_metas_requests >= 50:  # cheating hacks Orz
                        return JsonResponse()
                elif i == 4:
                    json.comments_case = True
                    json.start = params.get('after', 0) if params else 0
                json.load_mockeddata()
                return json
        else:
            if kwargs.get('error') == 'ValueError':
                raise ValueError
            elif kwargs.get('error') == 'IncompleteRead':
                e = httplib.IncompleteRead(partial='some error here')
                raise e
            elif kwargs.get('error') == 'RetryError':
                raise RetryError
            elif kwargs.get('error') == 'Exception':
                raise Exception
            elif kwargs.get('resp_error'):
                return JsonResponse(error=kwargs.get('resp_error'))
            else:
                error_json = JsonResponse()
                error_json.result = {'error': 'Not found Ya'}
                error_json.status_code = 404
                return error_json