Welcome to noseapp_requests’s documentation!¶
noseapp_requests is intended for use with noseapp library. It provides an extension for working with HTTP requests and REST APIs, based on the requests library.
Contents:
Basics¶
Setting up the extension¶
from noseapp.ext.requests import RequestsEx, make_config
endpoint = make_config()
endpoint.configure(
base_url='http://httpbin.org/',
key='httpbin'
)
endpoint.session_configure(
always_return_json=True,
raise_on_http_error=True
)
requests_ex = RequestsEx(endpoint)
Making requests¶
Get an API session:
>>> api = requests_ex.get_endpoint_session('httpbin', auth=('user', 'pass'))
Simple GET request (if always_return_json option is set, response is automatically parsed as JSON into a noseapp.datastructures.ModifyDict which allows access to values via dot):
>>> api.get('basic-auth/user/pass')
{u'authenticated': True, u'user': u'user'}
>>> r = api.get('basic-auth/user/pass')
>>> r.authenticated
True
>>> r.user
u'user'
GET with query-string parameters:
>>> api.get('get', key1='val1')
{u'args': {u'key1': u'val1'},
<...>
u'url': u'http://httpbin.org/get?key1=val1'}
POST form-encoded data:
>>> api.post('post', key1='val1')
{u'form': {u'key1': u'val1'},
<...>
u'url': u'http://httpbin.org/post'}
POST JSON data:
>>> api.post('post', {'key1': 'val1'})
{u'data': u'{"key1": "val1"}',
u'json': {u'key1': u'val1'},
<...>
u'url': u'http://httpbin.org/post'}
If raise_on_http_error option is set, an exception is raised if response status code is 4xx or 5xx
>>> api.get('status/400')
HTTPError: 400 Client Error: BAD REQUEST
Authentication¶
You can provide custom authentication class for an endpoint via auth_cls config parameter:
>>> from requests_oauthlib import OAuth2
>>> endpoint.configure(auth_cls=OAuth2)
Then you can pass authentication parameter in runtime when getting an API session:
>>> api = requests_ex.get_endpoint_session('oauth2', auth=(client_id, client, token))
Reference¶
noseapp_requests.base module¶
- class noseapp_requests.base.Endpoint(config)[source]¶
Represents an API endpoint.
Parameters: config (Config) – endpoint config
- class noseapp_requests.base.RequestsEx(*configs)[source]¶
Extension initializer.
Usage:
>>> requests_ex = RequestsEx(ENDPOINT1_CONFIG, ENDPOINT2_CONFIG, ...)
Each endpoint config represents an API endpoint (basically, a base URL) that you need access to in tests. So, for example, if you need to call your app’s API at http://yourapp.test/api/v1/ and also get data from some external source at http://databank.test:6847/, you’ll provide two configs to the extension::
app_config = make_config() app_config.configure(base_url='http://yourapp.test/api/v1/') databank_config = make_config() databank_config.configure(base_url='http://databank.test:6847/') requests_ex = RequestsEx(app_config, databank_config)
Each config can be provided a key that will be used to refer to it. By default, base_url is used as key.
- get_endpoint_session(endpoint_key, **kwargs)[source]¶
Get API session for endpoint endpoint_key. Any additional kwargs will be passed to Endpoint.get_session().
- name = 'requests'¶
- class noseapp_requests.base.Session(base_url=None, url_builder=None, always_return_json=False, raise_on_http_error=False, **session_kwargs)[source]¶
Represents an API/requests session. Actual interface is dependent on the url_builder provided; by default it mimics requests interface.
Parameters: - base_url – Base URL for endpoint; if provided, default URL builder will prepend it to all URLs provided in method calls.
- url_builder – A custom URL builder callable. See noseapp_requests.urlbuilder documentation for details.
- always_return_json (bool) – Parse all responses to JSON if set.
- raise_on_http_error (bool) – Raise HTTPError if response status code >= 400.
All other keyword arguments are set directly on requests.Session.
Default interface:
- get(url, **params)¶
- post(url, json_object=None, **params)¶
- put(url, json_object=None, **params)¶
- delete(url, **params)¶
All keyword parameters for get go into query string, for other methods they are urlencoded into request body.
noseapp_requests.config module¶
- class noseapp_requests.config.Config[source]¶
Extension configuration.