예제 #1
0
 def test_proxyerror_exception(self):
     args = [ "GET", "http://localhost:8080", HTTPError("oh no") ]
     kwargs = { "method": "GET", "url": "http://localhost:8080", "exception": HTTPError("oh no") }
     with pytest.raises(ProxyError):
         raise ProxyError(*args)
     with pytest.raises(DatadogException):
         raise ProxyError(*args)
     with pytest.raises(ProxyError):
         raise ProxyError(**kwargs)
     with pytest.raises(DatadogException):
         raise ProxyError(**kwargs)
예제 #2
0
    def request(cls, method, url, headers, params, data, timeout, proxies,
                verify, max_retries):
        try:

            with cls._session_lock:
                if cls._session is None:
                    cls._session = requests.Session()
                    http_adapter = requests.adapters.HTTPAdapter(
                        max_retries=max_retries)
                    cls._session.mount("https://", http_adapter)
                    cls._session.headers.update(
                        {"User-Agent": _get_user_agent_header()})

            result = cls._session.request(method,
                                          url,
                                          headers=headers,
                                          params=params,
                                          data=data,
                                          timeout=timeout,
                                          proxies=proxies,
                                          verify=verify)

            result.raise_for_status()

        except requests.exceptions.ProxyError as e:
            raise _remove_context(ProxyError(method, url, e))
        except requests.ConnectionError as e:
            raise _remove_context(ClientError(method, url, e))
        except requests.exceptions.Timeout:
            raise _remove_context(HttpTimeout(method, url, timeout))
        except requests.exceptions.HTTPError as e:
            if e.response.status_code in (400, 401, 403, 404, 409, 429):
                # This gets caught afterwards and raises an ApiError exception
                pass
            else:
                raise _remove_context(
                    HTTPError(e.response.status_code, result.reason))
        except TypeError:
            raise TypeError(
                u"Your installed version of `requests` library seems not compatible with"
                u"Datadog's usage. We recommand upgrading it ('pip install -U requests')."
                u"If you need help or have any question, please contact [email protected]"
            )

        return result