Skip to content
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
3 changes: 2 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
nose==1.2.1
requests-mock==0.7.0
pyhamcrest==1.8.1
pyhamcrest==1.8.1
flexmock
27 changes: 27 additions & 0 deletions tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
# limitations under the License.
import base64
import unittest
import requests

from flexmock import flexmock, flexmock_teardown
from hamcrest import assert_that, equal_to, raises, calling
import requests_mock
from requests_mock.exceptions import NoMockAddress
Expand All @@ -28,6 +30,9 @@ def setUp(self):
self.username = 'admin'
self.password = 'test'

def tearDown(self):
flexmock_teardown()

@requests_mock.mock()
def test_api_method_returns_without_arguments(self, request_mock):
json_data = [
Expand Down Expand Up @@ -173,6 +178,28 @@ def test_api_http_post_method_raises_on_result_414(self, request_mock):

assert_that(calling(ubersmith_api.support.ticket_submit.http_post), raises(UnknownError))

def test_api_http_timeout(self):
payload = dict(status=True, data="plop")
response = flexmock(status_code=200, json=lambda: payload)
ubersmith_api = api.init(self.url, self.username, self.password, 666)

flexmock(requests).should_receive("get").with_args(
url=self.url, auth=(self.username, self.password), timeout=666, params={'method': 'uber.method_list'}
).and_return(response)

ubersmith_api.uber.method_list()

def test_api_http_default_timeout(self):
payload = dict(status=True, data="plop")
response = flexmock(status_code=200, json=lambda: payload)
ubersmith_api = api.init(self.url, self.username, self.password)

flexmock(requests).should_receive("get").with_args(
url=self.url, auth=(self.username, self.password), timeout=60, params={'method': 'uber.method_list'}
).and_return(response)

ubersmith_api.uber.method_list()

@requests_mock.mock()
def test_api_http_post_method_raises_on_result_500(self, request_mock):
json_data = {
Expand Down
20 changes: 14 additions & 6 deletions ubersmith_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,30 @@
from ubersmith_client.exceptions import UbersmithException, get_exception_for


def init(url, user, password):
return UbersmithApi(url, user, password)
def init(url, user, password, timeout=60):
return UbersmithApi(url, user, password, timeout)


class UbersmithApi(object):
def __init__(self, url, user, password):
def __init__(self, url, user, password, timeout):
self.url = url
self.user = user
self.password = password
self.timeout = timeout

def __getattr__(self, module):
return UbersmithRequest(self.url, self.user, self.password, module)
return UbersmithRequest(self.url, self.user, self.password, module, self.timeout)


class UbersmithRequest(object):
def __init__(self, url, user, password, module):
def __init__(self, url, user, password, module, timeout):
self.url = url
self.user = user
self.password = password
self.module = module
self.methods = []
self.http_methods = {'GET': 'get', 'POST': 'post'}
self.timeout = timeout

def __getattr__(self, function):
self.methods.append(function)
Expand All @@ -48,7 +50,13 @@ def __call__(self, **kwargs):

def process_request(self, http_method, **kwargs):
callable_http_method = getattr(requests, http_method)
response = callable_http_method(self.url, auth=(self.user, self.password), **kwargs)

response = callable_http_method(
self.url,
auth=(self.user, self.password),
timeout=self.timeout,
**kwargs
)

if response.status_code < 200 or response.status_code >= 400:
raise get_exception_for(status_code=response.status_code)
Expand Down