Skip to main content

Python library for interacting with the Patreon API. OAuth-centric for now.

Project description

patreon-python

Version License Python Version

Note: This is a fork of the official patreon-python library up to version 0.5.0. This fork has been modernized with pyproject.toml and is published as patreon-v2 on PyPI. For legacy support, please use the original repository.

Interact with the Patreon API via OAuth.

Get the package from PyPI, typically via pip:

pip install patreon-v2

or

echo "patreon-v2" >> requirements.txt
pip install -r requirements.txt

Make sure that, however you install patreon-v2, you install its dependencies as well (automatically handled by pip)

Step 1. Get your client_id and client_secret

Visit the Patreon API Documentation for API reference, or go to the Patreon Developer Portal to register your client.

This will provide you with a client_id and a client_secret.

Step 2. Use this library

e.g., in a Flask route

import patreon
from flask import request
...

client_id = None      # Replace with your data
client_secret = None  # Replace with your data
creator_id = None     # Replace with your data

@app.route('/oauth/redirect')
def oauth_redirect():
    oauth_client = patreon.OAuth(client_id, client_secret)
    tokens = oauth_client.get_tokens(request.args.get('code'), '/oauth/redirect')
    access_token = tokens['access_token']

    api_client = patreon.API(access_token)
    user_response = api_client.get_identity()
    user = user_response.data()
    memberships = user.relationship('memberships')
    membership = memberships[0] if memberships and len(memberships) > 0 else None

    # pass user and membership to your view to render as needed

You'll notice that the user_response does not return raw JSON data. Instead, it returns a JSON:API resource object, to simplify traversing the normalized graph data that the Patreon API returns. Some available methods are:

  • response.data() to get the main resource
  • response.data().attribute('full_name') to get the full_name attribute from the response data
  • response.data().relationship('campaign').attribute('pledge_sum') to get the pledge_sum attribute from the campaign resource related to the main response data
  • response.find_resource_by_type_and_id('user', '123') to find an arbitrary resource

Step 3. (Optional) Customize your usage

patreon.API instances have methods for interacting with the API based on the routes described in the docs. All methods include includes and fields parameters.

ie:

  • get_identity(includes=None, fields=None)

List methods take page_size and cursor methods as well, ie:

  • get_campaigns(page_size, cursor=None, includes=None, fields=None)

The includes and fields arguments to these methods specify the related resources and the resource attributes you want returned by our API, as per the JSON:API specification. The lists of valid includes and fields arguments are provided on patreon.schemas. For instance, if you wanted to request the total amount a patron has ever paid to your campaign, which is not included by default, you could do:

api_client = patreon.API(patron_access_token)
patron_response = api_client.get_identity(None, {
    'membership': patreon.schemas.member.Attributes.currently_entitled_amount_cents
})

patreon.API also has a utility method extract_cursor which you can use to extract pagination links from our json:api response documents:

api_client = patreon.API(creator_access_token)
campaign_id = api_client.get_campaigns().data()[0].id()
memberships = []
cursor = None
while True:
    members_response = api_client.get_campaigns_by_id_members(campaign_id, 10, cursor=cursor)
    members += members_response.data()
    cursor = api_client.extract_cursor(members_response)
    if not cursor:
        break
names_and_membershipss = [{
    'full_name': member.relationship('user').attribute('full_name'),
    'amount_cents': member.attribute('amount_cents'),
} for member in members]

Developing

  1. Clone this repo
  2. Install dependencies
    python -m venv venv && source venv/bin/activate && pip install -e .
    
  3. git checkout -b my-branch-name
  4. make your edits, writing tests for any new functionality
  5. make sure tests pass with pytest
  6. git push
  7. Open a pull request, explaining your changes (both problem and solution) clearly

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

patreon_v2-0.5.1.tar.gz (30.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

patreon_v2-0.5.1-py3-none-any.whl (29.6 kB view details)

Uploaded Python 3

File details

Details for the file patreon_v2-0.5.1.tar.gz.

File metadata

  • Download URL: patreon_v2-0.5.1.tar.gz
  • Upload date:
  • Size: 30.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for patreon_v2-0.5.1.tar.gz
Algorithm Hash digest
SHA256 299fedffdcf93828df0e1ab3b1ce687490cc70ec7f7606f3edc24de2f6d1cbf0
MD5 35570e4977e16614500229f556112dba
BLAKE2b-256 961ad596650c7cbcd86e8f1309e996520253ca035ba0b65a2af364914794ce09

See more details on using hashes here.

File details

Details for the file patreon_v2-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: patreon_v2-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 29.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for patreon_v2-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b998701004b3040710f9cffe54a3b53ce3b6bdf609744492fc143543424843bd
MD5 42aa2a801b349e06da809f7273bbc536
BLAKE2b-256 12e8ac5ba132a2f86166a8c52e59209005429e6d44c269b6129616beec5cf550

See more details on using hashes here.

Supported by

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