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
39 changes: 31 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
# Ubersmith API Client for Python
Ubersmith API Client for Python
===============================

.. image:: https://travis-ci.org/internap/python-ubersmithclient.svg?branch=master
:target: https://travis-ci.org/internap/python-ubersmithclient

.. image:: https://img.shields.io/pypi/v/ubersmith_client.svg?style=flat
:target: https://pypi.python.org/pypi/ubersmith_client

# Usage
Usage
-----
.. code:: python

>>> import ubersmith_client
>>> api = ubersmith_client.api.init('http://ubersmith.com/api/2.0/', 'username', 'password')
>>> api.client.count()
u'264'
>>> api.client.latest_client()
1265
import ubersmith_client

api = ubersmith_client.api.init('http://ubersmith.com/api/2.0/', 'username', 'password')
api.client.count()
>>> u'264'
api.client.latest_client()
>>> 1265

API
---------

**ubersmith_client.api.init(url, user, password, timeout, use_http_post)**
:url:
URL of your API

*Example:* ``http://ubersmith.example.org/api/2.0/``

:user: API username
:password: API Password or token
:timeout: api timeout given to requests

*Default:* ``60``
:use_http_post:
Use `POST` requests instead of `GET`

*Default:* ``False``
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests<=2.9.1
six>=1.10.0
81 changes: 61 additions & 20 deletions tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
# limitations under the License.
import base64
import unittest
import six.moves.urllib.parse as urllib_parse

import requests

from flexmock import flexmock, flexmock_teardown
from hamcrest import assert_that, equal_to, raises, calling
import requests_mock
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure that this style of import still works even if you proved it with the exceptions.

from requests_mock.exceptions import NoMockAddress
import ubersmith_client
from ubersmith_client import api
from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized
from ubersmith_client.exceptions import BadRequest, UnknownError, Forbidden, NotFound, Unauthorized
from tests.ubersmith_json.response_data_structure import a_response_data


Expand Down Expand Up @@ -62,7 +65,7 @@ def test_api_method_returns_with_arguments(self, request_mock):
self.expect_a_ubersmith_call(request_mock, method="device.ip_group_list", fac_id='1', client_id='30001',
data=data)

ubersmith_api = api.init(self.url, self.username, self.password)
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
response = ubersmith_api.device.ip_group_list(fac_id=1, client_id=30001)

assert_that(response, equal_to(json_data))
Expand All @@ -71,15 +74,15 @@ def test_api_method_returns_with_arguments(self, request_mock):
def test_api_raises_exception_with_if_data_status_is_false(self, request_mock):
data = a_response_data(status=False, error_code=1, error_message="invalid method specified: client.miss",
data=None)
ubersmith_api = api.init(self.url, self.username, self.password)
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)

self.expect_a_ubersmith_call(request_mock, method="client.miss", data=data)
assert_that(calling(ubersmith_api.client.miss), raises(UbersmithException))
assert_that(calling(ubersmith_api.client.miss), raises(ubersmith_client.exceptions.UbersmithException))

@requests_mock.mock()
def test_api_raises_exception_for_invalid_status_code(self, request_mock):
method = "client.list"
ubersmith_api = api.init(self.url, self.username, self.password)
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)

self.expect_a_ubersmith_call(request_mock, method=method, status_code=400)

Expand All @@ -101,7 +104,7 @@ def test_api_raises_exception_for_invalid_status_code(self, request_mock):
def test_api_with_a_false_identifier(self, request_mock):
method = "client.list"
self.expect_a_ubersmith_call(request_mock, method=method)
ubersmith_api = api.init(self.url, 'not_hapi', 'lol')
ubersmith_api = ubersmith_client.api.init(self.url, 'not_hapi', 'lol')

with self.assertRaises(NoMockAddress) as ube:
ubersmith_api.client.list()
Expand All @@ -119,7 +122,7 @@ def test_api_http_get_method(self, request_mock):
self.expect_a_ubersmith_call(request_mock, method="device.ip_group_list", fac_id='666', client_id='30666',
data=data)

ubersmith_api = api.init(self.url, self.username, self.password)
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
response = ubersmith_api.device.ip_group_list.http_get(fac_id=666, client_id=30666)

assert_that(response, equal_to(json_data))
Expand All @@ -135,7 +138,23 @@ def test_api_http_get_method_default(self, request_mock):
self.expect_a_ubersmith_call(request_mock, method="device.ip_group_list", fac_id='666', client_id='30666',
data=data)

ubersmith_api = api.init(self.url, self.username, self.password)
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
response = ubersmith_api.device.ip_group_list(fac_id=666, client_id=30666)

assert_that(response, equal_to(json_data))

@requests_mock.mock()
def test_api_http_post_method_default(self, request_mock):
json_data = {
'group_id': '666',
'client_id': '30666',
'assignment_count': '1'
}
data = a_response_data(data=json_data)
self.expect_a_ubersmith_call_post(request_mock, method='device.ip_group_list', fac_id='666', client_id='30666',
response_body=data)

ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password, use_http_post=True)
response = ubersmith_api.device.ip_group_list(fac_id=666, client_id=30666)

assert_that(response, equal_to(json_data))
Expand All @@ -151,10 +170,13 @@ def test_api_http_post_method_result_200(self, request_mock):

self.expect_a_ubersmith_call_post(
request_mock,
method='support.ticket_submit',
body='ticket body',
subject='ticket subject',
response_body=json_data,
)

ubersmith_api = api.init(self.url, self.username, self.password)
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
response = ubersmith_api.support.ticket_submit.http_post(body='ticket body', subject='ticket subject')

assert_that(response, equal_to(json_data.get('data')))
Expand All @@ -170,18 +192,19 @@ def test_api_http_post_method_raises_on_result_414(self, request_mock):

self.expect_a_ubersmith_call_post(
request_mock,
method='support.ticket_submit',
response_body=json_data,
status_code=414
)

ubersmith_api = api.init(self.url, self.username, self.password)
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)

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)
ubersmith_api = ubersmith_client.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'}
Expand All @@ -192,7 +215,7 @@ def test_api_http_timeout(self):
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)
ubersmith_api = ubersmith_client.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'}
Expand All @@ -211,28 +234,46 @@ def test_api_http_post_method_raises_on_result_500(self, request_mock):

self.expect_a_ubersmith_call_post(
request_mock,
method='support.ticket_submit',
response_body=json_data,
)

ubersmith_api = api.init(self.url, self.username, self.password)
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)

assert_that(calling(ubersmith_api.support.ticket_submit.http_post), raises(UbersmithException))
assert_that(calling(ubersmith_api.support.ticket_submit.http_post),
raises(ubersmith_client.exceptions.UbersmithException))

def expect_a_ubersmith_call(self, request_mock, method, data=None, status_code=200, **kwargs):
url = self.url + '?method=' + method
if kwargs:
for key, value in kwargs.items():
url += '&' + key + '=' + value
url = self.url + '?' + get_url_params(method, **kwargs)
headers = {
'Content-Type': 'application/json',
}
request_mock.get(url, json=data, headers=headers, request_headers={
'Authorization': self.get_auth_header()
}, status_code=status_code)

def expect_a_ubersmith_call_post(self, request_mock, response_body=None, status_code=200):
request_mock.post(self.url, json=response_body, status_code=status_code)
def expect_a_ubersmith_call_post(self, request_mock, method, response_body=None, status_code=200, **kwargs):
headers = {
'Content-Type': 'application/json',
}
expected_text = get_url_params(method, **kwargs)

def response_callback(request, context):
expected_params = urllib_parse.parse_qs(expected_text)
parsed_params = urllib_parse.parse_qs(request.text)
assert_that(parsed_params, equal_to(expected_params))
return response_body

request_mock.post(self.url, json=response_callback, headers=headers, request_headers={
'Authorization': self.get_auth_header(),
'Content-Type': 'application/x-www-form-urlencoded'
}, status_code=status_code)

def get_auth_header(self):
auth = base64.b64encode((self.username + ':' + self.password).encode('utf-8'))
return 'Basic ' + auth.decode('utf-8')


def get_url_params(method, **kwargs):
kwargs['method'] = method
return urllib_parse.urlencode(kwargs)
4 changes: 3 additions & 1 deletion ubersmith_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from . import api
from . import exceptions
17 changes: 11 additions & 6 deletions ubersmith_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,42 @@
from ubersmith_client.exceptions import UbersmithException, get_exception_for


def init(url, user, password, timeout=60):
return UbersmithApi(url, user, password, timeout)
def init(url, user, password, timeout=60, use_http_post=False):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document this new parameter and what it does.

return UbersmithApi(url, user, password, timeout, use_http_post)


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

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


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

def __getattr__(self, function):
self.methods.append(function)
return self

def __call__(self, **kwargs):
return self.http_get(**kwargs)
if self.use_http_post:
return self.http_post(**kwargs)
else:
return self.http_get(**kwargs)

def process_request(self, http_method, **kwargs):
callable_http_method = getattr(requests, http_method)
Expand Down