Skip to content

Added API example for creating new webhooks #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
81 changes: 81 additions & 0 deletions cratejoy_api_examples/new_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
This command-line utility demonstrates how to create
a webhook using the Cratejoy API.

Documentation at: http://docs.cratejoy.com/docs/hook
http://docs.cratejoy.com/docs/web-hook-methods

Example Usage:
python newHook.py --user user-id --pw password --event subscription_new --target https://yourwebhooktarget.com/id/ --request_type POST --name postNewSubToTarget

"""

import json
import requests


def new_webhook(auth, event, target, request_type, name,
base_url='https://api.cratejoy.com/v1/'):
"""
Create a new webhook for your Cratejoy store.
"""

payload = json.dumps({
u'event': event,
u'target': target,
u'name': name,
u'request_type': request_type
})

base_url = 'https://api.cratejoy.com/v1/'

webhooks_endpoint = '{}hooks/'.format(base_url)

resp = requests.post(
webhooks_endpoint,
data=payload,
auth=auth
)

print('POST request to {} responded with status '
'code: {}'.format(webhooks_endpoint,
resp.status_code))


if __name__ == '__main__':
from argparse import ArgumentParser

parser = ArgumentParser()

parser.add_argument(u'--user',
type=unicode,
required=True, help=u'Basic auth user')
parser.add_argument(u'--pw',
type=unicode,
required=True, help=u'Basic auth password')
parser.add_argument(u'--event',
type=unicode,
required=True,
help=u'(string): name of a cratejoy event that will trigger this hook')
parser.add_argument(u'--target',
type=unicode,
help=u'(string): the target URI of your endpoint')
parser.add_argument(u'--request_type',
type=unicode,
help=u'(string): either POST or GET', default='POST')
parser.add_argument(u'--name',
type=unicode,
help=u'name of the new webhook')

args = parser.parse_args()
auth = (args.user, args.pw)

fn_args = {
'auth': auth,
'event': args.event,
'target': args.target,
'request_type': args.request_type,
'name': args.name
}

new_webhook(**fn_args)