Client library for the Knock API
Project description
Knock Python library
Knock API access for applications written in Python.
Documentation
See the documentation for Python usage examples.
Installation
To install from PyPi, run the following:
pip install knockapi
To install from source, clone the repo and run the following:
python setup.py install
Configuration
To use the library you must provide a secret API key, provided in the Knock dashboard.
from knockapi import Knock
client = Knock(api_key="sk_12345")
Usage
Identifying users
from knockapi import Knock
client = Knock(api_key="sk_12345")
client.users.identify(id="jhammond", data={"name": "John Hammond", "email": "jhammond@ingen.net"})
Retrieving users
from knockapi import Knock
client = Knock(api_key="sk_12345")
client.users.get_user(id="jhammond")
Sending notifies
from knockapi import Knock
client = Knock(api_key="sk_12345")
client.notify(
key="dinosaurs-loose",
actor="dnedry",
recipients=["jhammond", "agrant", "imalcolm", "esattler"],
cancellation_key=alert.id,
tenant="jurassic-park",
data={
"type": "trex",
"priority": 1
}
)
Getting and setting channel data
from knockapi import Knock
client = Knock(api_key="sk_12345")
# Set channel data for an APNS
client.users.set_channel_data(
id="jhammond",
channel_id=KNOCK_APNS_CHANNEL_ID,
channel_data={
"tokens": [apns_token]
}
)
# Get channel data for the APNS channel
client.users.get_channel_data(id="jhammond", channel_id=KNOCK_APNS_CHANNEL_ID)
Canceling notifies
from knockapi import Knock
client = Knock(api_key="sk_12345")
client.workflows.cancel(
key="dinosaurs-loose",
cancellation_key=alert.id,
recipients=["jhammond", "agrant", "imalcolm", "esattler"],
)
User preferences
from knockapi import Knock
client = Knock(api_key="sk_12345")
# Replaces the preferences for the user
client.users.set_preferences(
user_id="jhammond",
channel_types={'email': True},
workflows={'dinosaurs-loose': False}
)
# Retrieve the current preferences
client.users.get_preferences(user_id="jhammond")
Signing JWTs
You can use the pyjwt
package to sign JWTs easily.
You will need to generate an environment specific signing key, which you can find in the Knock dashboard.
If you're using a signing token you will need to pass this to your client to perform authentication. You can read more about client-side authentication here.
import jwt
import os
private_key = os.getenv("KNOCK_SIGNING_KEY")
encoded = jwt.encode({"sub": "jhammond"}, private_key, algorithm="RS256")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file knockapi-0.4.5.tar.gz
.
File metadata
- Download URL: knockapi-0.4.5.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9fead1c51312656d14de41d8617e1c1e9f7c935653f0f6e4c733bcaea98ff2d |
|
MD5 | b577d1917ada79137971f0da568d64a5 |
|
BLAKE2b-256 | d28e1e563fdc24ecfbac759b52901881e64604345268c68efde24237dbcb5a59 |
File details
Details for the file knockapi-0.4.5-py3-none-any.whl
.
File metadata
- Download URL: knockapi-0.4.5-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebff35e0dcb9fceb59a9d75beeb7395489458f2aed4c4ab85e91b26fd38d7fed |
|
MD5 | bd524ae89c69c6f7e68fe3a85a8c7b19 |
|
BLAKE2b-256 | fc8a1761958180f83f422aac866793254ae00f842b043efd7f467055146d2846 |