<commit_before># Demands

[![Build Status](https://travis-ci.org/yola/demands.png)](https://travis-ci.org/yola/demands)
[![Coverage Status](https://coveralls.io/repos/yola/demands/badge.png?branch=master)](https://coveralls.io/r/yola/demands?branch=master)
[![Latest Version](https://pypip.in/v/demands/badge.png)](https://pypi.python.org/pypi/demands/)
[![Downloads](https://pypip.in/d/demands/badge.png)](https://pypi.python.org/pypi/demands/)

A base HTTP service client.

By default it "demands" successful responses from API endpoints,
otherwise it raises an exception.

Demands accepts all the same parameters as `requests.request` and extends
the `requests.Session` class, documentation for both:
[Requests Developer Interface][2].

Written and used by the folks at Yola to support our [free website builder][1].

## HTTPServiceClient Overview

* base class for creating service clients
* provides flexible way of http error handling for descendants
* `HTTPServiceError` raised on unexpected service response

* Supports pre-send and post-send hooks

## Usage
```python
from demands import HTTPServiceClient

class MyService(HTTPServiceClient):
    def get_user(self, user_id):
        return self.get('/users/%s/' % user_id).json

    def safe_get_user(self, user_id, default_user):
        response = self.get(
            '/users/%s/' % user_id, 
            expected_response_codes=[404])
        return response.json if response.is_ok else default_user


service = MyService(url='http://localhost/')
user = service.get_user(1234)
```

Any parameters passed to the constructor will also be used for 
each and every request:
```python
service = MyService(
    url='http://localhost/',
    headers={'h1':'value'},
    auth=('username','pass'),
)

# sent with auth and both headers
user = service.get('/some-path', headers={'h2': 'kittens'})
```


## Testing

Install development requirements:

    pip install -r requirements.txt<commit_msg>requests made json callable. Update README example<commit_after># Demands

[![Build Status](https://travis-ci.org/yola/demands.png)](https://travis-ci.org/yola/demands)
[![Coverage Status](https://coveralls.io/repos/yola/demands/badge.png?branch=master)](https://coveralls.io/r/yola/demands?branch=master)
[![Latest Version](https://pypip.in/v/demands/badge.png)](https://pypi.python.org/pypi/demands/)
[![Downloads](https://pypip.in/d/demands/badge.png)](https://pypi.python.org/pypi/demands/)

A base HTTP service client.

By default it "demands" successful responses from API endpoints,
otherwise it raises an exception.

Demands accepts all the same parameters as `requests.request` and extends
the `requests.Session` class, documentation for both:
[Requests Developer Interface][2].

Written and used by the folks at Yola to support our [free website builder][1].

## HTTPServiceClient Overview

* base class for creating service clients
* provides flexible way of http error handling for descendants
* `HTTPServiceError` raised on unexpected service response

* Supports pre-send and post-send hooks

## Usage
```python
from demands import HTTPServiceClient

class MyService(HTTPServiceClient):
    def get_user(self, user_id):
        return self.get('/users/%s/' % user_id).json()

    def safe_get_user(self, user_id, default_user):
        response = self.get(
            '/users/%s/' % user_id, 
            expected_response_codes=[404])
        return response.json() if response.is_ok else default_user()


service = MyService(url='http://localhost/')
user = service.get_user(1234)
```

Any parameters passed to the constructor will also be used for 
each and every request:
```python
service = MyService(
    url='http://localhost/',
    headers={'h1':'value'},
    auth=('username','<PASSWORD>'),
)

# sent with auth and both headers
user = service.get('/some-path', headers={'h2': 'kittens'})
```


## Testing

Install development requirements:

    pip install -r requirements.txt
{'fraction_non_alphanumeric': 0.13446442137249937, 'fraction_numerical': 0.005571030640668524, 'mean_word_length': 4.470914127423823, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3242605', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1185, 'n_words': 327}