コード例 #1
0
    def request(self, method, url, **kwargs):
        domain = urlparse(url).netloc

        try:
            request = self.api_breaker.call(super().request,
                                            method=method,
                                            url=url,
                                            **kwargs)
        except requests.exceptions.Timeout:
            timeout_counter.labels(domain=domain).inc()

            raise ApiTimeoutError(
                "The request to {} took too long".format(url))
        except requests.exceptions.ConnectionError:
            connection_failed_counter.labels(domain=domain).inc()

            raise ApiConnectionError(
                "Failed to establish connection to {}.".format(url))
        except CircuitBreakerError:
            raise ApiCircuitBreaker(
                "Requests are closed because of too many failures".format(url))

        latency_histogram.labels(domain=domain,
                                 code=request.status_code).observe(
                                     request.elapsed.total_seconds())

        return request
コード例 #2
0
ファイル: requests.py プロジェクト: pmahnke/snapcraft.io
 def request(self, method, url, timeout=12, **kwargs):
     try:
         return super().request(method=method,
                                url=url,
                                timeout=timeout,
                                **kwargs)
     except Timeout:
         raise ApiTimeoutError(
             "The request to {} took too long".format(url))
     except ConnectionError:
         raise ApiConnectionError(
             "Failed to establish connection to {}.".format(url))
コード例 #3
0
    def request(self, method, url, **kwargs):
        try:
            request = self.api_breaker.call(super().request,
                                            method=method,
                                            url=url,
                                            **kwargs)
        except requests.exceptions.Timeout:
            raise ApiTimeoutError(
                "The request to {} took too long".format(url))
        except requests.exceptions.ConnectionError:
            raise ApiConnectionError(
                "Failed to establish connection to {}.".format(url))
        except CircuitBreakerError:
            raise ApiCircuitBreaker(
                "Requests are closed because of too many failures".format(url))

        return request
コード例 #4
0
def get(url, headers, json=None, data=None, method=None, files=None):
    """
    WARNING: The cache is disabled due to a bug on our caching system.

    Retrieve the response from the requests cache.
    If the cache has expired then it will attempt to update the cache.
    If it gets an error, it will use the cached response, if it exists.
    """

    if method is None:
        method = "POST" if json else "GET"

    # TODO allow user to choose it's own user agent
    storefront_header = 'storefront ({commit_hash};{environment})'.format(
        commit_hash=os.getenv('COMMIT_ID', 'commit_id'),
        environment=os.getenv('ENVIRONMENT', 'devel'))
    headers.update({'User-Agent': storefront_header})
    domain = urlparse(url).netloc

    try:
        response = requests.request(method=method,
                                    url=url,
                                    headers=headers,
                                    json=json,
                                    files=files,
                                    data=data,
                                    timeout=3)
    except requests.exceptions.Timeout:
        timeout_counter.labels(domain=domain).inc()

        raise ApiTimeoutError(
            'The request to {} took longer than 3 seconds'.format(url), )
    except requests.exceptions.ConnectionError:
        connection_failed_counter.labels(domain=domain).inc()

        raise ApiConnectionError(
            'Failed to establish connection to {}.'.format(url))

    latency_histogram.labels(domain=domain, code=response.status_code).observe(
        response.elapsed.total_seconds())

    return response
コード例 #5
0
    def request(self, method, url, **kwargs):
        domain = urlparse(url).netloc

        try:
            request = super().request(method=method, url=url, **kwargs)
        except requests.exceptions.Timeout:
            timeout_counter.labels(domain=domain).inc()

            raise ApiTimeoutError(
                'The request to {} took too long'.format(url), )
        except requests.exceptions.ConnectionError:
            connection_failed_counter.labels(domain=domain).inc()

            raise ApiConnectionError(
                'Failed to establish connection to {}.'.format(url))

        latency_histogram.labels(domain=domain,
                                 code=request.status_code).observe(
                                     request.elapsed.total_seconds())

        return request