Skip to main content

Declarative and handy RESTful client factory

Project description

version circleci status


apap makes you can ap ply RESTful ap i to your client handy.




This exposes apap.Client class to define your api client. apap.Client must be defining api_base_url and _method_map as class attribute.

import apap

class YourAPI(apap.Client):
   api_base_url = ''

   _method_map = apap.MethodMap(
      ('get_all', apap.Method.Get, 'things'),  # all resources from GET http method.
      ('get_one', apap.Method.Get, 'things/:id'),  # one of resources filterd by id from GET http method.
      ('create', apap.Method.Post, 'things'),  # create a new item from POST http method.
      ('update', apap.Method.Put, 'things/:id'),  # update something filterd by id from PUT http method.
      ('delete', apap.Method.Delete, 'things/:id'),  # delete something filterd by id from Delete http method.


You know apap.MethodMap makes your client declarative.

apap.MethodMap is just a tuple of client method name, HTTP method and endpoint url.

At last, you need to register your client to entrypoint via apap.apply.

Those request returns requests’s Response object (because apap uses requests internally).

import apap

client = apap.apply(YourAPI)()

client.get_all() # same as `curl`
client.get_one(id=1)()  # same as `curl`
client.create(x=1, y=2)  # same as `curl -X POST -d "x=1" -d "y=2"`
client.update(id=1)(x=10)  # same as `curl -X PUT -d "x=1"`
client.delete(id=1)()  # same as `curl -X DELETE`

When you need to set any headers for requesting, you can use header_map attribute to translate actual header from python world.

So if you want to use Authorization as http-header, please define header_map looks like below.

class YourAPI(apap.Client):
   header_map = {'Authorization': 'auth_key'}

apap.apply(YourAPI)(headers={'auth_key': 'token xxxxx'})


All you need is just creating a class which inherits apap.Client class.

import os

from apap import MethodMap, Client, Method, apply

class GithubAPI(Client):
  api_base_url = ''
  header_map = {'Authorization': 'access_token'}

class UserRepo(GithubAPI):
  name = 'user_repo'

  _method_map = MethodMap(
     ('get', Method.Get, 'users/:username/repos'),

class MyRepo(GithubAPI):
    name = 'my_repo'

    _method_map = MethodMap(
        ('get', Method.Get, 'user/repos'),

access_token = os.environ['ACCESS_TOKEN']

endpoints = [UserRepo, MyRepo]

gh_client = apply(*endpoints)(headers={'access_token': f'token {access_token}'})
user_repo_resp = gh_client.user_repo.get(username='mtwtkman')()
my_repo_resp = gh_client.my_repo.get(visibility='private')

Project details

Download files

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

Files for apap, version 0.0.0b3
Filename, size File type Python version Upload date Hashes
Filename, size apap-0.0.0b3-py3-none-any.whl (4.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size apap-0.0.0b3.tar.gz (4.7 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page