Skip to main content

A Python API Client

https://travis-ci.org/madisona/apyclient.svg?branch=py3_json_fix

Overview

Allows you to easily create client APIs in a highly customizable way.

Installation

Requires python >= 2.6 and apysigner. Tests require ‘mock’ package.

pip install apyclient

Usage

You are able to easily create a client Api class. The only requirement of the API class is that it must have a “HOST_NAME” attribute declared. The API uses this host name to prepend to the endpoint when building the request.

class MyAPIClient(object):
    HOST_NAME = "http://www.example.com"

    @api_request("/api-endpoint/")
    def fetch_some_stuff(some_var):
        return {"the_variable": some_var}

my_client = MyApiClient()
my_client.fetch_some_stuff(3)

And that’s it. The client will make an HTTP GET request by default with the data provided by the decorated method.

You can also do a POST request by declaring method="POST" in the api_request.

@api_request("/api-endpoint/", method="POST")
def fetch_some_stuff(some_var):
    return {"the_variable": some_var}

And finally, you are able to return a custom response class if you so desire. Just either provide a RESPONSE_CLASS on the api client class, or a response_class on the api_request decorator. If you have a custom response class declared both on the API client and on the api_request decorator, the decorator will win because it is more specific. The response class must take one argument on initialization, the original response.

class MyApiClient(object):
    HOST_NAME = "http://www.example.com/api
    RESPONSE_CLASS = MyDefaultResponseClass

    @api_request("/api-endpoint/")
    def fetch_some_stuff(some_var):
        return {"the_variable": some_var}

    @api_request("/api-endpoint/", response_class=SpecializedResponseClass)
    def fetch_some_stuff(some_var):
        return {"the_variable": some_var}

New addition: Signed Requests This was modeled after Google Maps signed request. When you have a situation where you want to secure an endpoint without login you can use the signed request.

The concept is the API provider shares a client id and private key with the client applications. The client applications then “sign” their requests to the provider using the client id and private key. When the provider receives the request, it verifies that it can create the same signature using the given client id.

Take a look at the django-request-signer python package for some more information.

Metadata

Release files for apyclient 0.3.1

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

Source distribution (sdist)

Source distribution for apyclient 0.3.1
File Size Uploaded
apyclient-0.3.1.tar.gz 8.1 kB Details

Release files / apyclient-0.3.1.tar.gz

Download URL apyclient-0.3.1.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
174e0ac65a9d4dd126266669a0eb586149908981bb6f620501e3de93000fbcbd
BLAKE2b-256 checksum
How to use checksums
8bcf3eb033c6662a350aa313def5786b2da2025a20c8ebeda332b73a74ec23dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.1 This release

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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