def test_exception(self, m_errorlog): from rest_client import RestClient, RequestException client = RestClient(self.TEST_BASE) with mock.patch.object(client, 'session') as m_session: m_request = m_session.request m_request.side_effect = RequestException() with self.assertRaises(RequestException): client.call('GET', ()) m_errorlog.assert_called_once_with( 'failure', 'RequestException', 'GET', 'http://host/base', m_request.side_effect)
def test_redirect(self, m_errorlog): from rest_client import RestClient client = RestClient(self.TEST_BASE) with mock.patch.object(client, 'session') as m_session: m_response = m_session.request.return_value m_response.is_redirect = True m_response.status_code = 301 m_response.json.return_value = {'error': 'ERROR', 'message': 'MSG'} m_response.headers = {'location': 'LOCATION'} with self.assertRaises(IOError): client.call('GET', ()) m_errorlog.assert_called_once_with( 'redirect', 'redirect', 'GET', 'http://host/base', 'LOCATION', body=m_response.content, status=301)
def test_client_error(self, m_errorlog): from rest_client import RestClient, HTTPError client = RestClient(self.TEST_BASE) with mock.patch.object(client, 'session') as m_session: m_response = m_session.request.return_value m_response.is_redirect = False m_response.raise_for_status.side_effect = HTTPError() m_response.status_code = 400 m_response.json.return_value = {'error': 'ERROR', 'message': 'MSG'} with self.assertRaises(HTTPError): client.call('GET', ()) m_errorlog.assert_called_once_with( 'client', 'ERROR', 'GET', 'http://host/base', 'MSG', body=m_response.content, status=400)
def test_legacy(self): from rest_client import RestClient client = RestClient(self.TEST_BASE) client.requests_legacy = True # Force detection of requests 1.x with mock.patch.object(client, 'session') as m_session: m_response = m_session.request.return_value m_response.is_redirect = False m_response.status_code = 200 client.call('GET', [], json='[1, 2, 3]') m_session.request.assert_called_once_with( allow_redirects=False, data='"[1, 2, 3]"', headers={'Content-Type': 'application/json'}, method='GET', url='http://host/base')
def test_segment_type(self): from rest_client import RestClient client = RestClient(self.TEST_BASE) with self.assertRaises(TypeError): client.call('GET', 'thisIsNotATupleOrList')
from pprint import pprint import logging from rest_client import RestClient logging.basicConfig(level=logging.ERROR) if __name__ == '__main__': client = RestClient('http://jsonplaceholder.typicode.com', auth=('username', 'password'), options={'timeout': 3.0}, user_agent='PlaceHolderClient/1.0') print "\n=== PUT /posts/1 ===" response = client.call('PUT', ('posts', 1)) pprint(response.json()) print "\n=== GET /comments?postId=1 ===" response = client.call('GET', ('comments',), params=dict(postId=1)) pprint(response.json())