Skip to main content

Build Status Coverage Status

hapic in a nutshell

hapic is a framework-agnostic library for coding professionnal REST APIs.

Philosophy

hapic as been developed by algoo in the context of a large service oriented project. The lack of a tool allowing real auto-documentation of Rest API has decided us to develop hapic.

target usage is not for "quick and dirty" stuff but for professionnal, maintainable, long-term targeted projects.

The separation of concerns between REST APIs layer and business stuff layer is in the DNA of hapic.

hapic is just the HTTP layer glue code over your business code.

Direct benefits of using hapic

When you decide to base your development on hapic, you'll get direct benefits:

Ready-to-use

  • supports aiohttp, flask, pyramid and bottle
  • ready-to-use with your existing libraries
  • effortless mapping of exceptions to HTTP errors
  • serialisation based on marshmallow schemas or serpyco dataclasses

Full API documentation ready

  • your code IS the documentation
  • swagger generated documentation
  • embed the documentation in 1 line of code
  • supports python 3.5, 3.6 and 3.7

Professionnal and maintanable source code

  • separation of concerns between business logic and HTTP stuff
  • very fast when used in conjunction with both aiohttp and serpyco
  • extensible framework for supporting other web framework and serialisation libraries

Licence

hapic is licenced under the MIT licence. You can use it in your projects, closed or open sourced.

status, contributions

hapic source code is ready for production. Some refactoring are identified and required for maintainability, but public APIs are stable so you can rely on hapic for your developments.

hapic is under active development, based on different professional projects. we will answer your questions and accept merge requests if you find bugs or want to include features.

hapic is automatically tested on python 3.5, 3.6 and 3.7

TODO references

TODOs in the code can include some #xxx - these are github issues references.

Installation

From source code

virtualenv -p /usr/bin/python3 venv
source venv/bin/activate
python setup.py develop

To work with Marshmallow schemas, install necessary dependencies:

pip install -e ".[marshmallow]"

To work with Serpyco dataclasses, install necessary dependencies:

pip install -e ".[serpyco]"

Give it a try

short Flask example

from datetime import datetime
import flask
import marshmallow
import hapic
from hapic.ext.flask import FlaskContext
import json

hapic = hapic.Hapic()
app = flask.Flask(__name__)


class UriPathSchema(marshmallow.Schema):  # schema describing the URI and allowed values
    name = marshmallow.fields.String(required=True)
    age = marshmallow.fields.Integer(required=False)


class HelloResponseSchema(marshmallow.Schema): # schema of the API response
    name = marshmallow.fields.String(required=True)
    now = marshmallow.fields.DateTime(required=False)
    greetings = marshmallow.fields.String(required=False)


@app.route('/hello/<name>')  # flask route. must always be before hapic decorators
@hapic.with_api_doc()  # the first hapic decorator. Register the method for auto-documentation
@hapic.input_path(UriPathSchema())  # validate the URI structure
@hapic.output_body(HelloResponseSchema())  # define output structure
def hello(name='<No name>', hapic_data=None):
    return {
        'name': name,
        'now': datetime.now(),
        'dummy': { 'some': 'dummy' }  # will be ignored
    }

class UriPathSchemaWithAge(marshmallow.Schema):  # schema describing the URI and allowed values
    name = marshmallow.fields.String(required=True)
    age = marshmallow.fields.Integer(required=False)


@app.route('/hello/<name>/age/<age>')
@hapic.with_api_doc()
@hapic.input_path(UriPathSchemaWithAge())
@hapic.output_body(HelloResponseSchema())
def hello2(name='<No name>', age=42, hapic_data=None):
    return {
        'name': name,
        'age': age,
        'greetings': 'Hello {name}, it looks like you are {age}'.format(
            name=name,
            age=age
        ),
        'now': datetime.now(),
        'dummy': { 'some': 'dummy' }  # will be ignored
    }


hapic.set_context(FlaskContext(app))
print(json.dumps(hapic.generate_doc(title='API Doc', description='doc desc.')))  # Generate the documentation
app.run('127.0.0.1', 8080, debug=True)

How to use it:

Nominal cases:

$ curl "http://127.0.0.1:8080/hello/michel"
# {"now": "2017-12-18T12:37:10.751623+00:00", "name": "michel"}
$ curl "http://127.0.0.1:8080/hello/michel/age/17"
# {"name": "damien", "greetings": "Hello damien, it looks like you are 17", "now": "2017-12-18T12:41:58.229679+00:00"}

Error case (returns a 400):

$ curl "http://127.0.0.1:8080/hello/michel/age/mistaken"
# {"details": {"age": ["Not a valid integer."]}, "message": "Validation error of input data"}

A complete user API

In the example/usermanagement directory you can find a complete example of an API allowing to manage users.

Features are:

  • get list of all users
  • get detail of a given user
  • create a user
  • delete a user

In order to test it :

Install the required dependencies:

pip install bottle flask pyramid`

Run the instance you wan to test (one of the three following lines):

python example/usermanagement/serve_bottle.py
python example/usermanagement/serve_flask.py
python example/usermanagement/serve_pyramid.py

Features shown :

  • auto-generation of the documentation
  • managing parameters in the uri path
  • managing input schemas
  • managing output schema
  • management of error cases (404, 500, etc)
  • nice exception handling
  • automatic dict/object serialization

Release files for hapic 0.64

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hapic 0.64
File Size Uploaded
hapic-0.64.tar.gz 599.5 kB Details

Release files / hapic-0.64.tar.gz

Download URL hapic-0.64.tar.gz
Size 599.5 kB
Tags Source
SHA-256 checksum
How to use checksums
354986bc7b80d5cc6a7384c5802cb37aff9d6ca584e15899ecee7dd3e1950a58
BLAKE2b-256 checksum
How to use checksums
1c0815e29424316adeb1dbae2d22dddd7f835fcdc9195ae93124ef2dc4dbfa61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.5.3

Release history Release notifications | RSS feed

1.0

1 release file

0.98

1 release file

0.97

1 release file

0.96

1 release file

0.95

1 release file

0.94

1 release file

0.93

1 release file

0.92

1 release file

0.91

1 release file

0.90

1 release file

0.89

1 release file

0.88

1 release file

0.87

1 release file

0.86

1 release file

0.85

1 release file

0.84

1 release file

0.83

1 release file

0.82

1 release file

0.81

1 release file

0.80

1 release file

0.79

1 release file

0.78

1 release file

0.77

1 release file

0.76

1 release file

0.75

1 release file

0.74

1 release file

0.73

1 release file

0.72

1 release file

0.71

1 release file

0.70

1 release file

0.69

1 release file

0.68

1 release file

0.67

1 release file

0.66

1 release file

0.65

1 release file

This release

0.64 This release

1 release file

0.63

1 release file

0.62

1 release file

0.61

1 release file

0.60

1 release file

0.59

1 release file

0.58

1 release file

0.57

1 release file

0.56

1 release file

0.55

1 release file

0.54

1 release file

0.53

1 release file

0.52

1 release file

0.51

1 release file

0.50

1 release file

0.49

1 release file

0.48

1 release file

0.47

1 release file

0.46

1 release file

0.45

1 release file

0.44

1 release file

0.43

1 release file

0.42

1 release file

0.41

1 release file

0.40

1 release file

0.39

1 release file

0.38

1 release file

0.37

1 release file

0.36

1 release file

0.35

1 release file

0.34

1 release file

0.33

1 release file

0.32

1 release file

0.31

1 release file

0.30

1 release file

0.29

1 release file

0.28

1 release file

0.27

1 release file

0.26

1 release file

0.25

1 release file

0.24

1 release file

0.23

1 release file

0.22

1 release file

0.21

1 release file

0.20

1 release file

0.19

1 release file

0.18

1 release file

0.17

1 release file

0.16

1 release file

0.15

1 release file

0.14

1 release file

0.13

1 release file

0.12

1 release file

0.11

1 release file

0.10

1 release file

0.9

1 release file

0.8

1 release file

0.7

1 release file

0.6

1 release file

0.5

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page