Skip to main content

Small, opinionated library for building REST APIs.

Project description

Introduction

Apian is an opinionated library for setting up a Python-based service with a minimum of boilerplate. It is a thin wrapper around flask-restplus and provides:

  • Documentation using OpenAPI.

  • Info and health resources.

  • JWT-based authentication.

  • Configuration injection using miniscule.

Example

Add a configuration file config.yaml in the root of the project, with the following contents:

environment: production
debug: False
authentication:
  enabled: True
  secret: secret

To create a Flask application and run it on localhost:5000:

from apian import read_config, create_api, create_app, authenticated
from flask_restplus import Namespace, Resource

ns = Namespace("user")

@ns.route("")
class UserItem(Resource):

    @authenticated
    def get(self, user_id):
        return user_id

config = read_config()
api = create_app("my-app", config)
api.add_namespace(ns)
app = create_app(api, config)
app.run()

The application has endpoints at the paths:

  • GET /my-app/api/info - Return information about the service.

  • GET /my-app/api/health - Return the health status of the service.

  • GET /my-app/api/user - Return the user ID set in the Bearer token.

To access the user resource, ensure that the requests package is installed and execute the following snippet:

import jwt
import requests

def auth_token():
  user_id = 10
  claims = {"iat": dt.datetime.utcnow(), "sub": user_id}
  key = "secret"
  return jwt.encode(claims, key, "HS256")

headers = {"Authorization": "Bearer {}".format(
requests.get("http://localhost:5000/my-app/api/user", headers=headers)

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

apian-0.5.0.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file apian-0.5.0.tar.gz.

File metadata

  • Download URL: apian-0.5.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.7.0 requests/2.25.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.6.8

File hashes

Hashes for apian-0.5.0.tar.gz
Algorithm Hash digest
SHA256 a9ac4231d4b62288adca8125f8de0d6dd71dc307573d7985f104a80e403dfaa1
MD5 470e289a021fc0d40733181cf5897d8d
BLAKE2b-256 b2563c23e1aa832329363dec3b987ef7ddfbdf0323a521726cffc464050f1594

See more details on using hashes here.

Supported by

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