Uplink

A Declarative HTTP Client for Python, inspired by Retrofit.

Useful Links

  • Uplink @ PyPI
  • Uplink @ Github
  • Issue Tracker
  • Release History

Table of Contents

  • Installation
  • Quickstart
  • Authentication
  • Serialization
  • Clients
  • Tips, Tricks, & Extras
  • API
  • Changelog

Quick search

Uplink 📡¶

A Declarative HTTP Client for Python. Inspired by Retrofit.

Release Python Version License Codecov GitHub Discussions

Note

Uplink is in beta development. The public API is still evolving, but we expect most changes to be backwards compatible at this point.

Uplink turns your HTTP API into a Python class.

from uplink import Consumer, get, Path, Query


class GitHub(Consumer):
    """A Python Client for the GitHub API."""

    @get("users/{user}/repos")
    def get_repos(self, user: Path, sort_by: Query("sort")):
       """Get user's public repositories."""

Build an instance to interact with the webservice.

github = GitHub(base_url="https://api.github.com/")

Then, executing an HTTP request is as simply as invoking a method.

repos = github.get_repos(user="octocat", sort_by="created")

The returned object is a friendly requests.Response:

print(repos.json())
# Output: [{'id': 64778136, 'name': 'linguist', ...

For sending non-blocking requests, Uplink comes with support for aiohttp and twisted (example).

Features¶

  • Quickly Define Structured API Clients
    • Use decorators and type hints to describe each HTTP request
    • JSON, URL-encoded, and multipart request body and file upload
    • URL parameter replacement, request headers, and query parameter support
  • Bring Your Own HTTP Library
    • Non-blocking I/O support for Aiohttp and Twisted
    • Supply your own session (e.g., requests.Session) for greater control
  • Easy and Transparent Deserialization/Serialization
    • Define custom converters for your own objects
    • Support for marshmallow schemas and handling collections (e.g., list of Users)
    • Support for pydantic models and handling collections (e.g., list of Repos)
  • Extendable
    • Install optional plugins for additional features (e.g., protobuf support)
    • Compose custom response and error handling functions as middleware
  • Authentication
    • Built-in support for Basic Authentication
    • Use existing auth libraries for supported clients (e.g., requests-oauthlib)

Uplink officially supports Python 2.7 & 3.5+.

Note

Python 2.7 suport will be removed in v0.10.0.

User Testimonials¶

Michael Kennedy (@mkennedy), host of Talk Python and Python Bytes podcasts-

Of course our first reaction when consuming HTTP resources in Python is to reach for Requests. But for structured APIs, we often want more than ad-hoc calls to Requests. We want a client-side API for our apps. Uplink is the quickest and simplest way to build just that client-side API. Highly recommended.

Or Carmi (@liiight), notifiers maintainer-

Uplink’s intelligent usage of decorators and typing leverages the most pythonic features in an elegant and dynamic way. If you need to create an API abstraction layer, there is really no reason to look elsewhere.

User Manual¶

Follow this guide to get up and running with Uplink.

  • Installation
    • Using pip
    • Download the Source Code
    • Extras
  • Quickstart
    • Defining an API Client
    • Making a Request
    • Path Parameters
    • Query Parameters
    • Request Headers
    • Request Body
    • Form Encoded, Multipart, and JSON Requests
    • Handling JSON Responses
    • Persistence Across Requests from a Consumer
    • Response and Error Handling
    • Retrying
    • Client-Side Rate Limiting
  • Authentication
    • Basic Authentication
    • Proxy Authentication
    • Other Authentication
    • Using Auth Support for Requests and aiohttp
  • Serialization
    • Using Marshmallow Schemas
    • Using Pydantic Models
    • Serializing Method Arguments
    • Custom JSON Conversion
    • Converting Collections
    • Writing A Custom Converter
  • Clients
    • Swapping Out the Default HTTP Session
    • Synchronous vs. Asynchronous
    • Handling Exceptions From the Underlying HTTP Client Library
  • Tips, Tricks, & Extras
    • Decorating All Request Methods in a Class
    • Adopting the Argument’s Name
    • Annotating Your Arguments For Python 2.7
    • Annotating __init__() Arguments
    • The Consumer’s _inject() Method
    • Extend Consumer Methods to Reduce Boilerplate

API Reference¶

This guide details the classes and methods in Uplink’s public API.

  • API
    • The Base Consumer Class
      • Consumer
      • Session
    • Decorators
      • headers
      • params
      • json
      • form_url_encoded
      • multipart
      • timeout
      • args
      • response_handler
      • error_handler
      • inject
      • returns.*
      • retry
      • ratelimit
    • Function Annotations
      • Path
      • Query
      • QueryMap
      • Header
      • HeaderMap
      • Field
      • FieldMap
      • Part
      • PartMap
      • Body
      • Url
      • Timeout
      • Context
    • HTTP Clients
      • Requests
      • Aiohttp
      • Twisted
    • Converters
      • Marshmallow
      • Pydantic
      • Converting Collections
      • Writing Custom JSON Converters
    • Auth Methods
      • BasicAuth
      • ProxyAuth
      • BearerToken
      • MultiAuth
      • ApiTokenParam
      • ApiTokenHeader

Miscellaneous¶

  • Changelog
    • 0.9.7 - 2022-03-10
    • 0.9.6 - 2022-01-24
    • 0.9.5 - 2022-01-04
    • 0.9.4 - 2021-02-15
    • 0.9.3 - 2020-11-22
    • 0.9.2 - 2020-10-18
    • 0.9.1 - 2020-02-08
    • 0.9.0 - 2019-06-05
    • 0.8.0 - 2019-02-16
    • 0.7.0 - 2018-12-06
    • 0.6.1 - 2018-9-14
    • 0.6.0 - 2018-9-11
    • 0.5.5 - 2018-8-01
    • 0.5.4 - 2018-6-26
    • 0.5.3 - 2018-5-31
    • 0.5.2 - 2018-5-30
    • 0.5.1 - 2018-4-10
    • 0.5.0 - 2018-4-06
    • 0.4.1 - 2018-3-10
    • 0.4.0 - 2018-2-10
    • 0.3.0 - 2018-1-09
    • 0.2.2 - 2017-11-23
    • 0.2.0 - 2017-11-03
    • 0.1.1 - 2017-10-21
    • 0.1.0 - 2017-10-19
©2018, P. Raj Kumar. | Page source
Fork me on GitHub