All Projects β†’ pactum-org β†’ pactum

pactum-org / pactum

Licence: GPL-3.0 license
Create API specifications and documentation using Python

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to pactum

MARCspec
πŸ“„ MARCspec - A common MARC record path language
Stars: ✭ 21 (-12.5%)
Mutual labels:  specification
ui5-cap-event-app
Showcase of SAP Cloud Application Programming Model and OData V4 with draft mode in a freestyle SAPUI5 app and an SAP Fiori elements app.
Stars: ✭ 70 (+191.67%)
Mutual labels:  wip
klever
Read-only mirror of the Klever Git repository
Stars: ✭ 18 (-25%)
Mutual labels:  specification
KanColle-English-Patch-KCCP
English Patch for the original KanColle browser game, to be used with KCCacheProxy. Translates most of the game into english.
Stars: ✭ 28 (+16.67%)
Mutual labels:  wip
spec-pattern
Specification design pattern for JavaScript and TypeScript with bonus classes
Stars: ✭ 43 (+79.17%)
Mutual labels:  specification
visual-graph-explorer
A simple graph explorer leveraging yFiles for HTML, neo4j bolt, implemented using VueJS and Vuetify
Stars: ✭ 13 (-45.83%)
Mutual labels:  wip
donate-spec
The Missing Donation Specification for Open Source Software
Stars: ✭ 13 (-45.83%)
Mutual labels:  specification
specifications
Track specification elaboration.
Stars: ✭ 17 (-29.17%)
Mutual labels:  specification
go-external-ip
a Golang library to get your external ip from multiple services
Stars: ✭ 55 (+129.17%)
Mutual labels:  wip
standard-components
A specification for functional UI components
Stars: ✭ 52 (+116.67%)
Mutual labels:  specification
specifications-ITS-REST
openEHR REST API Specifications
Stars: ✭ 20 (-16.67%)
Mutual labels:  specification
pepatch
A hacky tool to patch PE binaries.
Stars: ✭ 21 (-12.5%)
Mutual labels:  wip
WebinoImageThumb
βœ‚οΈ Webinoβ„’ Image thumbnailer for Zend Framework [LTS] http://webino.github.io/WebinoImageThumb
Stars: ✭ 40 (+66.67%)
Mutual labels:  wip
f2e-spec
Alibaba Front-end Coding Guidelines and Relevant Tools
Stars: ✭ 548 (+2183.33%)
Mutual labels:  specification
chat-ui
πŸ’¬ rich message handling chat interface for bot projects
Stars: ✭ 19 (-20.83%)
Mutual labels:  wip
OctoPrint-Octoremote
A hardware remote for Octoprint controlled 3D Printers
Stars: ✭ 23 (-4.17%)
Mutual labels:  wip
webspicy
A technology agnostic specification and test framework that yields better coverage for less testing effort.
Stars: ✭ 42 (+75%)
Mutual labels:  specification
Specs-on-Spec
A collection of language specifications which don't belong anywhere else
Stars: ✭ 13 (-45.83%)
Mutual labels:  specification
adamant.language.reference
The Adamant Programming Language Reference
Stars: ✭ 18 (-25%)
Mutual labels:  specification
tinyspec
Simple syntax for describing REST APIs
Stars: ✭ 95 (+295.83%)
Mutual labels:  specification

Pactum

The HTTP-API specification sketchbook for pythonistas

Circle CI Coverage Status

pip install pactum

With Pactum you can specify HTTP-APIs using pure python.

Pactum is easy to use, easy to extend and easy to contribute:

Easy to use

The only requirements to start writing an API specification with pactum is pactum package itself and some knowledge of python.

import pactum

class MyAPI(pactum.API):
    name = 'My API'
    versions = [...]

Easy to extend

Using the visitor pattern you can create exporters and extensions for any format or service you want.

Take a look at pactum/exporters/openapi.py.

Architecture

Always keep this diagram in mind when defining your APIs.

Tutorial

Create a file called specs.py and start defining your API.

You can define a Resource object for your API.

from pactum import Action, API, Resource, Response, Version
from pactum import fields, verbs

class Order(Resource):
    fields = [
        fields.IntegerField(name='code', required=True),
        fields.TimestampField(name='created_at'),
        fields.StringField(name='item')
    ]
resource = Order()

error_resource = Resource(
    name = 'ErrorResource'
    fields = [fields.StringField(name='error', required=False)]
)

You can define any element of your specification by calling it directly as in error_resource or by class definition as in MyResource and then calling it.

List resources are definitions of lists of the same resource.

list_order_resource = ListResource(resource=resource)

You can define Response objects with status, description(optional) a header(optional) and a Resource/ListResource object as body (optionally)...

list_response = Response(
    status=200, description='Here is your orders list.', body=list_resource
)

detail_response = Response(
    status=200, description='Here is your order.', body=resource
)

error_response = Response(status=404, resource=error_resource, headers=[('Content-type': 'application-json')])

... and Request objects with verb, description, header(optional) and a Resource/ListResource object as payload.

get_request = Request(verb=verbs.GET)

An Action groups your request and a list of responses for a specified action passed in the description parameter.

list_action = Action(
    description='Returns a list of resources.',
    request=get_request,
    responses=[error_response, list_response]
)

detail_action = Action(
    description='Returns a resource based on its code.',
    request=get_request,
    responses=[error_response, detail_response]
)

The Action object, as all other elements in Pactum, receive a description string that sets the .__doc__ attribute and can be the docstring of the class if the object is defined by class definition.

A route can have a list of actions in an HTTP path.

class OrderListRoute(Route):
    path = '/orders'
    actions = [list_action]

list_route = OrderListRoute()

detail_route = OrderRoute(path='/orders/{code}', actions=detail_action)

Your routes can be grouped in API versions.

class V1(Version):
    name = 'V1'
    routes = [list_route, detail_route]

v1 = V1()

Then you can define your API. ;)

class OrdersAPI(API):
    name = 'Orders API'
    versions = [v1]

api = OrdersAPI()

Be happy and ready to export your specification to any format you want.

Exporting to openapi specs.

Pactum has a command that exports your specification to OpenAPI. You can call it by using:

pactum-openapi <spec_file.py> <output_file> [--format=<json or yaml>]

Thanks to

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].