def test_httperror_exception(self): args = [ 500, "oh no" ] kwargs = { "status_code": 500, "reason": "oh no" } with pytest.raises(HTTPError): raise HTTPError(*args) with pytest.raises(DatadogException): raise HTTPError(*args) with pytest.raises(HTTPError): raise HTTPError(**kwargs) with pytest.raises(DatadogException): raise HTTPError(**kwargs)
def test_clienterror_exception(self): args = [ "GET", "http://localhost:8080", HTTPError("oh no") ] kwargs = { "method": "GET", "url": "http://localhost:8080", "exception": HTTPError("oh no") } with pytest.raises(ClientError): raise ClientError(*args) with pytest.raises(DatadogException): raise ClientError(*args) with pytest.raises(ClientError): raise ClientError(**kwargs) with pytest.raises(DatadogException): raise ClientError(**kwargs)
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) result = cls._session.request( method, url, headers=headers, params=params, data=data, timeout=timeout, proxies=proxies, verify=verify) result.raise_for_status() 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 as e: 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
def raise_on_status(cls, result): """ Raise on HTTP status code errors. """ status_code = result.status_code if (status_code / 100) != 2: if status_code in (400, 401, 403, 404, 409, 429): pass else: raise HTTPError(status_code)