How to use the predicthq.exceptions.ServerError function in predicthq

To help you get started, we’ve selected a few predicthq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github predicthq / sdk-py / tests / client_tests.py View on Github external
self.assertDictEqual(self.client.get("/v1/"), responses.calls[0].response.json())

        self.assertIsNone(self.client.get("/no-content/"))

        self.assertIsNone(self.client.get("/invalid-json/"))

        with self.assertRaises(ClientError) as ctx:
            self.client.get("/not-found/")
            self.assertEqual(ctx.exception.message, responses.calls[3].response.json())

        with self.assertRaises(ServerError) as ctx:
            self.client.get("/server-error/")
            self.assertEqual(ctx.exception.message, responses.calls[4].response.json())

        with self.assertRaises(ServerError) as ctx:
            self.client.get("/no-json/")
            self.assertEqual(ctx.exception.message, responses.calls[5].response.content)

        # Test headers
        self.client.authenticate(client_id='client_id', client_secret='client_secret', scope=['account'])
        self.assertEqual(responses.calls[6].request.headers['Authorization'], 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=')
        self.assertEqual(responses.calls[6].request.headers['Content-Type'], 'application/x-www-form-urlencoded')

        self.client.accounts.self()
        self.assertEqual(responses.calls[7].request.headers['Authorization'], 'Bearer token123')
        self.assertEqual(responses.calls[7].request.headers['Accept'], 'application/json')
github predicthq / sdk-py / tests / client_tests.py View on Github external
def test_request(self, responses):
        self.client.logger.setLevel('DEBUG')

        self.assertDictEqual(self.client.get("/v1/"), responses.calls[0].response.json())

        self.assertIsNone(self.client.get("/no-content/"))

        self.assertIsNone(self.client.get("/invalid-json/"))

        with self.assertRaises(ClientError) as ctx:
            self.client.get("/not-found/")
            self.assertEqual(ctx.exception.message, responses.calls[3].response.json())

        with self.assertRaises(ServerError) as ctx:
            self.client.get("/server-error/")
            self.assertEqual(ctx.exception.message, responses.calls[4].response.json())

        with self.assertRaises(ServerError) as ctx:
            self.client.get("/no-json/")
            self.assertEqual(ctx.exception.message, responses.calls[5].response.content)

        # Test headers
        self.client.authenticate(client_id='client_id', client_secret='client_secret', scope=['account'])
        self.assertEqual(responses.calls[6].request.headers['Authorization'], 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=')
        self.assertEqual(responses.calls[6].request.headers['Content-Type'], 'application/x-www-form-urlencoded')

        self.client.accounts.self()
        self.assertEqual(responses.calls[7].request.headers['Authorization'], 'Bearer token123')
        self.assertEqual(responses.calls[7].request.headers['Accept'], 'application/json')
github predicthq / sdk-py / predicthq / client.py View on Github external
def request(self, method, path, **kwargs):
        headers = self.get_headers(kwargs.pop("headers", {}))
        response = requests.request(method, self.build_url(path), headers=headers, **kwargs)
        self.logger.debug(response.request.url)
        try:
            response.raise_for_status()
        except requests.HTTPError:
            try:
                error = response.json()
            except ValueError:
                error = response.content

            if 400 <= response.status_code <= 499:
                raise ClientError(error)
            else:
                raise ServerError(error)

        try:
            result = response.json() or None
        except:
            result = None

        if self.logger.getEffectiveLevel() == logging.DEBUG:
            self.logger.debug(json.dumps(result, indent=True))

        return result