Example #1
0
    def _parse_and_handle_errors(
        response
    ) -> Union[MutableMapping[str, Any], List[MutableMapping[str, Any]]]:
        """Handle response"""
        message = "Unknown error"
        if response.headers.get(
                "content-type"
        ) == "application/json;charset=utf-8" and response.status_code != HTTPStatus.OK:
            message = response.json().get("message")

        if response.status_code == HTTPStatus.FORBIDDEN:
            raise HubspotAccessDenied(message, response=response)
        elif response.status_code in (HTTPStatus.UNAUTHORIZED,
                                      CLOUDFLARE_ORIGIN_DNS_ERROR):
            raise HubspotInvalidAuth(message, response=response)
        elif response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
            retry_after = response.headers.get("Retry-After")
            raise HubspotRateLimited(
                f"429 Rate Limit Exceeded: API rate-limit has been reached until {retry_after} seconds."
                " See https://developers.hubspot.com/docs/api/usage-details",
                response=response,
            )
        elif response.status_code in (HTTPStatus.BAD_GATEWAY,
                                      HTTPStatus.SERVICE_UNAVAILABLE):
            raise HubspotTimeout(message, response=response)
        else:
            response.raise_for_status()

        return response.json()
Example #2
0
    def _parse_and_handle_errors(
        response
    ) -> Union[MutableMapping[str, Any], List[MutableMapping[str, Any]]]:
        """Handle response"""
        message = "Unknown error"
        if response.headers.get(
                "content-type"
        ) == "application/json;charset=utf-8" and response.status_code != 200:
            message = response.json().get("message")

        if response.status_code == 403:
            raise HubspotAccessDenied(message, response=response)
        elif response.status_code == 401:
            raise HubspotInvalidAuth(message, response=response)
        elif response.status_code == 429:
            retry_after = response.headers.get("Retry-After")
            raise HubspotRateLimited(
                f"429 Rate Limit Exceeded: API rate-limit has been reached until {retry_after} seconds."
                " See https://developers.hubspot.com/docs/api/usage-details",
                response=response,
            )
        elif response.status_code in (502, 504):
            raise HubspotTimeout(message, response=response)
        else:
            response.raise_for_status()

        return response.json()