-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdropbox_hook.py
73 lines (60 loc) · 2.51 KB
/
dropbox_hook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import argparse
from hashlib import sha256
import hmac
import json
import sys
import click
import requests
@click.group()
def cli():
'''This tool makes it easier to test Dropbox webhooks, particularly on localhost. It generates fake requests, mimicking what Dropbox itself sends. Usage:
\b
dropbox_hook.py verify URL
dropbox_hook.py notify URL -s APP_SECRET -u USER_ID
For detailed help, try this:
dropbox_hook.py COMMAND --help
'''
pass
@cli.command()
@click.argument('url', metavar='URL', required=True)
@click.option('--challenge', '-c', help='The challenge string to send in a verification request (defaults to "challenge123").', default='challenge123', metavar='CHALLENGE', required=True)
def verify(url, challenge):
'''Send a verification request. Example usage:
dropbox_hook.py verify http://www.example.com
'''
response = requests.get(url, params={ 'challenge': challenge })
if response.status_code == 200:
if response.text == challenge:
print('Verification passed!')
else:
text = response.text
if len(text) > 30:
text = '(truncated) "%s..."' % text[:30]
else:
text = '"%s"' % text
print('Invalid verification response. Expected "%s", but server responded with %s' % (challenge, text))
else:
print('Invalid verification response. Server responded with status code %d.' % response.status_code)
@cli.command()
@click.argument('url', metavar='URL', required=True)
@click.option('--secret', '-s', help='Your app secret', metavar='APP_SECRET', required=True)
@click.option('--account', '-a', help='The account IDs to send to the webhook URI (may be specified multiple times).', multiple=True, metavar='ACCOUNT_ID', required=True, type=str)
def notify(url, secret, account):
'''Send a notification request. Example usage:
dropbox_hook.py notify http://www.example.com --secret ABC123 --account 12345
'''
body = json.dumps({ 'list_folder': { 'accounts': account } })
response = requests.post(
url,
data=body,
headers={
'X-Dropbox-Signature': hmac.new(secret.encode(), body.encode(), sha256).hexdigest(),
'Content-type' : 'application/json'
})
if response.status_code == 200:
print('Webhook invoked successfully.')
else:
print('Invalid webhook response. Server responded with status code %d.' % response.status_code)
sys.exit(1)
if __name__=='__main__':
cli()