python client for managing AWS appsync graphql subscriptions
Project description
appsync-subscription-manager
epiphani AWS AppSync Subscription Manager
Installation
$ pip install epiphani-appsync-subscription-manager
A package for managing GraphQL subscriptions for AWS AppSync
Currently supported AppSync authentication is AWS Cognito only. Future enhancements will add support for API Key, AWS IAM & OpenID.
For AWS Cognito, this package currently supports 2 modes of authentication:
- Provide an Access Token as argument:
- access_token - An authenticated, unexpired, Access Token
- Provide the following arguments, the user will be authenticated and the resulting access_token used for making subscriptions:
- username - Username of user in AWS Cognito Pool
- passwd - Password of username
- aws_cognito_pool_id - AWS Cognito Pool ID
- aws_cognito_pool_client_id - AWS Cognito Pool Client ID (one with no secret key)
Other Arguments
- aws_region The AWS Region of the AppSync API to use (default: us-west-2) (optional)
- appsync_api_id The API ID of the AppSync API to make subscriptions to (required)
- on_connection_error Callback function to notify of errors while connecting to GraphQL API endpoint (required)
- on_error Callback function to notify non-connection related errors (required)
- cb_data Opaque data that is passed back with any callback functions that the manager calls (optional)
- logger An instance of python logging getLogger (optional)
Using the AppSync Subscription Manager
Here is an example of using the subscription manager to register for notifications when a user is created or updated.
import appsync_subscription_manager as asm
AWS_APPSYNC_GQL_ENDPOINT_ID = 'dnj38asdfkn344nmkfndfnkl4nlk'
ID_TOKEN = 'JWT_ID_TOKEN'
USER_CREATE_SUBSCRIPTION = """
subscription OnCreateUser {
onCreateUser {
id
createdAt
updatedAt
userName
fullName
}
}
"""
USER_UPDATE_SUBSCRIPTION = """
subscription OnUpdateUser {
onUpdateUser {
id
createdAt
updatedAt
userName
fullName
}
}
"""
my_cb_data = {
'current_env': 'dev',
}
def on_connection_error(error, cb_data):
print("Got an error while making WebSocket connection: %r" % (error))
def on_close(cb_data):
print("WebSocket connection closed")
def on_error(error, cb_data):
print("Got an error on WebSocket connection: %r" % (error))
def user_create_subscription_error(error, cb_data):
print("Subscription failed: %r" % (error))
def user_create_subscription_success(cb_data):
print("Subscription succeeded")
def user_created(user_msg, cb_data):
print("user created")
def user_update_subscription_error(error, cb_data):
print("Subscription failed: %r" % (error))
def user_update_subscription_success(cb_data):
print("Subscription succeeded")
def user_updated(user_msg, cb_data):
print("user updated")
# sub: subscription object that you got (my_sub variable below)
# when you made the subscription request is returned
# back in the callback
def on_subscription_success(cb_data, sub):
print("Got subscription success...")
# create an instance of the subscription manager using token
# based authentication. Refer to the README.md to subscribe
# using username/password for a cognito pool user
my_mgr = asm.AppSyncSubscriptionManager(id_token = ID_TOKEN,
appsync_api_id = AWS_APPSYNC_GQL_ENDPOINT_ID,
on_connection_error = on_connection_error,
on_error = on_error,
on_close = on_close,
cb_data = my_cb_data)
# subscribe for user creation notifications
user_create_sub = my_mgr.subscribe(USER_CREATE_SUBSCRIPTION, user_created,
user_create_subscription_error, user_create_subscription_success)
user_update_sub = my_mgr.subscribe(USER_UPDATE_SUBSCRIPTION, user_updated,
user_update_subscription_error, user_update_subscription_success)
# Start the read loop to wait for subscription notifications
my_mgr.run_forever()
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
Built Distribution
Close
Hashes for epiphani-appsync-subscription-manager-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9042ce015c3016879472a28ddb5fea54a28c2ffaedd470708007ee55b12f72b5 |
|
MD5 | e66cde33071e5e3a9d84e89f9cc59b5b |
|
BLAKE2b-256 | eb66370d32f9f31520cc3f7ef2d1d70fb753b320faa01bc6b63e8ac7c2083628 |
Close
Hashes for epiphani_appsync_subscription_manager-0.1.2-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c3e4aa68d8ece0727c09a7dd2b94d17b8ae5278d523d2602f4c1c022e72a147 |
|
MD5 | fb09664ecff153c45eb4794c45b16592 |
|
BLAKE2b-256 | 8846ea11669da7a0346dac55e62d16bf9ccc230c077a6ca686c5d64834f15eec |