Skip to main content

Light-weight Python framework for building REST APIs.

Project description

https://travis-ci.org/petr-s/cleaREST.svg?branch=master https://coveralls.io/repos/github/petr-s/cleaREST/badge.svg?branch=master

cleaREST

Light-weight Python framework for building REST APIs

  • WSGI

  • minimalistic

  • easy to use

  • advanced variables processing

  • automatic html documentation generation

URL Routing

is done by decorating your handling function with one of these decorators:

  • @GET

  • @POST

only one argument is required (url to handle) ie:

@GET("/my/awesome/url")
def my_function():
    ...

optionally you can specify successful http result status (default is HTTP_OK) ie:

@GET("/my/awesome/url", status=HTTP_CREATED)
def my_function():
    ...

list of status tuples:

  • HTTP_OK

  • HTTP_CREATED

Variables

  • GET variables from query string

  • POST variables can be send as:

  • application/x-www-form-urlencoded

  • multipart/form-data

  • application/json

  • url path variables (identifier inside curly brackets) ie:

    @GET("/my/awesome/url/{variable}")
    def my_function(variable):
        ...

Parsing

per parameter parsing function assigned as default value ie:

@GET("/my/awesome/url")
def my_function(myid=int):
    ...

optional parameter:

@GET("/my/awesome/url")
def my_function(myid=(int, 0)):
    ...

reduction multiple into one:

@GET("/my/awesome/url")
def my_function(user_id=lambda user, password: login):
    ...

Returning data

build-in support:

Class

Content-type

str

text/plain

dict

application/json

minidom

application/xml

etree

application/xml

you can also register any custom data-type or override table above with:

register_content_type(type_, content_type, handler)

Errors

to return a http error raise one of these exceptions:

  • HttpBadRequest

  • HttpNotFound

  • HttpUnsupportedMediaType

  • HttpNotImplemented

ie:

@GET("/my/awesome/url")
def my_function(myid=int):
    if myid == -1:
         raise HttpNotFound()
    ...

HTML Documentation

when is url opened by web-browser html documentation is shown instead of actual function call.

  • “application/xhtml+xml” in accept http header

  • similar tags to sphinx/docutils

  • extra example tags: “:example:” and for response “:rexample”:

ie:

@GET("/my/api")
def my_function(a, b=int):
    """
    Describe your function here.

    :param str a: Describe a here.
    :param int b: Describe b here.
    :return: W/E you function returns.

    :example::

        GET /my/api?a=someting&b=42

    :rexample::

        {
            "something": "something"
            ...
        }
    """
    ...

“real app” example:

https://cloud.githubusercontent.com/assets/4590121/15144637/01abb660-16b1-11e6-85b4-bdb46d33e3cf.png

Examples:

GET Hello world:

from wsgiref.simple_server import make_server
from clearest import application, GET


@GET("/")
def hello():
    return "hello world!"

 httpd = make_server("", 8000, application)
 httpd.serve_forever()

Output:

curl localhost:8000

hello world!

POST var:

from wsgiref.simple_server import make_server
from clearest import application, POST


@POST("/")
def hello(what):
    return "hello {what}!".format(what=what)


httpd = make_server("", 8000, application)
httpd.serve_forever()

Output:

curl –data “what=world” localhost:8000

hello world!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cleaREST-0.3.5.zip (23.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cleaREST-0.3.5-py2.py3-none-any.whl (21.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cleaREST-0.3.5.zip.

File metadata

  • Download URL: cleaREST-0.3.5.zip
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cleaREST-0.3.5.zip
Algorithm Hash digest
SHA256 972308f983509ddf3b61799d54539ff0f950dea56f10460abe5d89b622f8aa45
MD5 1849e6c3ab1023403864301dd0fb800c
BLAKE2b-256 7ecbbf8d324ed709bff62b04579508b7bb015f80e095d46aefb2a1043368834b

See more details on using hashes here.

File details

Details for the file cleaREST-0.3.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cleaREST-0.3.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 23b10a6286498534bcfae75da76d0a613d69da8e5d2561928d5cbb9555972aa2
MD5 aab4b68db136f2942b059626b99bf651
BLAKE2b-256 33af60b90eda0b67ffbe0d7801300fe84165410401b7815a6f0b97435765b771

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page