Esempio n. 1
0
class BaseSession:
    """A base session interface to implement common functionality

    Create an interface to manage exceptions and return API exceptions
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # 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 = {"User-Agent": storefront_header}
        self.headers.update(headers)
        self.api_breaker = CircuitBreaker(fail_max=5, reset_timeout=60)

    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
Esempio n. 2
0
class BaseSession:
    """A base session interface to implement common functionality

    Create an interface to manage exceptions and return API exceptions
    """

    def __init__(self, timeout=(0.5, 3), *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.mount("http://", TimeoutHTTPAdapter(timeout=timeout))
        self.mount("https://", TimeoutHTTPAdapter(timeout=timeout))

        # 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 = {"User-Agent": storefront_header}
        self.headers.update(headers)
        self.api_breaker = CircuitBreaker(fail_max=5, reset_timeout=60)

    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
Esempio n. 3
0
class BaseSession:
    """A base session interface to implement common functionality

    Create an interface to manage exceptions and return API exceptions
    """
    def __init__(self, timeout=(0.5, 3), *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.mount("http://", TimeoutHTTPAdapter(timeout=timeout))
        self.mount("https://", TimeoutHTTPAdapter(timeout=timeout))

        # 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 = {"User-Agent": storefront_header}
        self.headers.update(headers)
        self.api_breaker = CircuitBreaker(fail_max=5, reset_timeout=60)

    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