Skip to content

Fix url exceptions #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
2.1.x
- Fixed HTTP status code exceptions
2.1.0
- Added enabled labels
- Added impressions by sdk and version including bucketing key
2.0.5
- Added SDK Factory Method with Manager API and Sdk Client
- Added Bucketing key support
- Added Bucketing key support
14 changes: 12 additions & 2 deletions splitio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import requests
import logging

from splitio.config import SDK_API_BASE_URL, EVENTS_API_BASE_URL, SDK_VERSION

Expand Down Expand Up @@ -38,6 +39,7 @@ def __init__(self, api_key, sdk_api_base_url=None, events_api_base_url=None,
:param read_timeout: The HTTP read timeout. Default: 1000 (seconds)
:type read_timeout: float
"""
self._logger = logging.getLogger(self.__class__.__name__)
self._api_key = api_key
self._sdk_api_url_base = sdk_api_base_url if sdk_api_base_url is not None \
else SDK_API_BASE_URL
Expand Down Expand Up @@ -83,19 +85,27 @@ def _build_headers(self):

return headers

def _logHttpError(self, response):
if response.status_code < 200 or response.status_code >= 400 :
respJson = response.json()
if 'message' in respJson:
self._logger.error("HTTP Error (status: %s) connecting with split servers: %s" % (response.status_code, respJson['message']))
else:
self._logger.error("HTTP Error connecting with split servers")

def _get(self, url, params):
headers = self._build_headers()

response = requests.get(url, params=params, headers=headers, timeout=self._timeout)
response.raise_for_status()
self._logHttpError(response)

return response.json()

def _post(self, url, data):
headers = self._build_headers()

response = requests.post(url, json=data, headers=headers, timeout=self._timeout)
response.raise_for_status()
self._logHttpError(response)

return response.status_code

Expand Down
18 changes: 10 additions & 8 deletions splitio/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,14 @@ def test_request_exceptions_are_raised(self):
with self.assertRaises(RequestException):
self.api._get(self.some_url, self.some_params)

def test_request_status_exceptions_are_raised(self):
"""Tests that if requests succeeds but its status is not 200 (Ok) an exception is raised
and , it is not handled within the call"""
def test_request_status_exceptions_are_not_raised(self):
"""Tests that if requests succeeds but its status is not 200 (Ok) an exception is not raised"""
self.requests_get_mock.return_value.raise_for_status.side_effect = HTTPError()

with self.assertRaises(HTTPError):
try:
self.api._get(self.some_url, self.some_params)
except:
self.assertTrue(False)

def test_json_exceptions_are_raised(self):
"""Tests that if requests succeeds but its payload is not JSON, an exception is raised and
Expand Down Expand Up @@ -214,13 +215,14 @@ def test_request_exceptions_are_raised(self):
with self.assertRaises(RequestException):
self.api._post(self.some_url, self.some_data)

def test_request_status_exceptions_are_raised(self):
"""Tests that if requests succeeds but its status is not 200 (Ok) an exception is raised
and , it is not handled within the call"""
def test_request_status_exceptions_are_not_raised(self):
"""Tests that if requests succeeds but its status is not 200 (Ok) an exception is not raised"""
self.requests_post_mock.return_value.raise_for_status.side_effect = HTTPError()

with self.assertRaises(HTTPError):
try:
self.api._post(self.some_url, self.some_data)
except:
self.assertTrue(False)


class SdkApiSplitChangesTest(TestCase, MockUtilsMixin):
Expand Down