Skip to main content

Python REST tools

A collection of tools to help interacting with JSON REST APIs.

Install

From Pypi

pip install rest-tools

Rationale

I was writing a lot of code to speak to different API, and while most of them used common interfaces (REST verbs, JSON data...) almost every API had different ways to handle authentication and pagination. Since I ended up writing a "common ground" method to access these APIs, I made it into a package.

In this package you'll find a common function that just mounts various parts into the actual request (using, not surprisingly, requests), and some other basic api client for the services I came across.

Common client

Every function uses a common client with basic support for JSON body request/response and query parameters. Things get a bit more useful when you use a pre-baked client for a popular service.

# common
from rest_tools.common import common_client

result = common_client(method="get",
                        base_url="https://example.com",
                        path="/api/v1/things",
                        parameters={'page': 1, 'per_page': 10})

# wordpress
from rest_tools.wordpress import get_wordpress_client

wp_client = get_wordpress_client(base_url="https://wp.example.com/wp-json", 
                                    api_key="api_key", 
                                    api_secret="api_secret")
wp_client("get", "/wp/v2/post", resource="posts")

Once you set up the client with the base_url and the auth parameters, you'll be able to query the resources in your Wordpress installation. Infact, if you get a resource like your blog's posts and include the name of the resource (resource="posts"), the client will automatically issue a number of requests to fetch all the resources from the paginated endpoint.

All of the clients share the same API; minor variations are detailed later on.

get_<service>_client(base_url, **auth_details)

  • base_url: if the service is in cloud (SaaS), this info is already configured.
  • **auth_details: every client does this differently (see details)

<service>_client(method, path="/", parameters=None, url=None, data=None, resource=None)

  • method: one of the common http verbs: get, post, put, patch, delete...
  • path: always starts with a /.
  • parameters: this dictionary becomes ?search=foo&exclude=bar.
  • url: if specified, path and parameters will be ignored (this should be a full URL, eg. https://example.com/api/v1/things).
  • data: the optional body content of a POST/PATCH/PUT request. It will be encoded as JSON according to the service.
  • resource: this could be a bool or a string, depending on how the service responds. It is used to pull all the resources from a paginated endpoint.
from rest_tools.example import get_example_client

example_client = get_example_client(base_url="https://www.example.com/api/v1",
                                    api_key="apikey",
                                    api_secret="apisecret")

foo_things = example_client(method="get",
                            path="/things",
                            parameters={'search': 'foo', 'exclude': 'bar'},
                            url=None,
                            data=None,
                            resource="things")

Clients

All these clients are packaged and documented (in code).

Canvas

get_canvas_client(access_token, base_url)

ClickUp

get_clickup_client(api_token, base_url)

Directus

get_directus_client(token, base_url)

Eventbrite

get_eventbrite_client(token, base_url)

Fusionauth

get_fusionauth_client(api_key, base_url)

Livestorm

get_livestorm_client(apikey, base_url)

Mailchimp

get_mailchimp_client(access_token, base_url)

Prestashop

get_prestashop_client(access_key, base_url)

Before being able to access the REST API, please follow the instructions to enable the Webservice.

Wordpress

get_wordpress_client(api_key, api_secret, base_url)

This client requires the JWT Auth plugin to generate key pair.

  • api_key: Key pair, key
  • api_secret: Key pair, secret.
  • base_url: The installation path of your WP installation; please include /wp-json at the end.

Pipedrive

get_pipedrive_client(api_token, domain)

This client requires a personal API token and the company domain.

  • api_token: A personal API token
  • domain: The company domain name assigned (es.: yourcompany will become https://yourcompany.pipedrive.com)

Metadata

Release files for rest-tools 0.7.2

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

Source distribution (sdist)

Source distribution for rest-tools 0.7.2
File Size Uploaded
rest_tools-0.7.2.tar.gz 24.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rest-tools 0.7.2
File Interpreter ABI Platform
rest_tools-0.7.2-py3-none-any.whl Python 3 none any Details

Total release size: 53.7 kB

Release files / rest_tools-0.7.2.tar.gz

Download URL rest_tools-0.7.2.tar.gz
Size 24.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4dd943f3ee6acf63134f51115cb8e94541eda077b5c81ba66a00b4c41b73661e
BLAKE2b-256 checksum
How to use checksums
90ec4e8110507c9f558b9c724784d63e546b95154878abb9f5451f4361967894
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.3

Release files / rest_tools-0.7.2-py3-none-any.whl

Download URL rest_tools-0.7.2-py3-none-any.whl
Size 29.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1774fe08449ae7cab0a0f89980d8a9b14ee0610075dc0e46822088b9fef0ab28
BLAKE2b-256 checksum
How to use checksums
871428431e6e68da96ec61705e52404c77a397fa827e8041f81ae6f4c592fc25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.3

Release history Release notifications | RSS feed

This release

0.7.2 This release

2 release files

0.7.1

2 release files

0.7

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

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