Webhooks (v2) with Python SDK - Getting Folder ID, Creating Webhook, Getting Address
Goal: create a folder that when I upload a file will call my own API to access and read the file
I have a few questions so looking for tips on how to do this with any best practices, resources, or tips.
Getting Folder ID
The docs for webhooks start with a folder id but don't describe how to get the id. Is one of these the best practices way to do it?
# import and initialize client auth not shown
# get folder_id by searching
folder_name = 'my-watchdir'
results = client.search().query(folder_name, result_type='folder')
for folder in results:
if folder.name == folder_name:
return folder.id
# get folder_id by shared link
link = 'https://...' # copied from Box interface
folder = client.get_shared_item(link)
return folder.id
Creating Webhook
Once having the folder_id can then create a webhook. The interface seems to only support v1 webhooks so can't be set on a per-folder basis.
https://developer.box.com/guides/webhooks/manage/for-folder/
folder_id = '1234'
listener = 'https://...' # my api endpoint
# Actually just needed the folder, not the id in retrospect
folder = client.folder(folder_id=folder_id)
webhook = client.create_webhook(folder, ['FILE.UPLOAD'], listener)
Verifying Webhook
I'm just using ipython to try and figure things out, is there Python documentation somewhere I missed? How do I get the address of the listener I just created or serialize / print its values to make sure it is the instance I thought it should be?
folder_id = '1234'
webhooks = list(client.get_webhooks(folder_id))
for hook in webhooks:
print(hook.get_url())
# how do I get address for my API listener?
Token Expiration
It seems the token expires, do I have to keep re-creating the webhook? How do I make sure I can read the file on the receiving end of the API? For example, this is what I see in my CloudWatch logs of a lambda function:
{'type': 'webhook_event', 'id': '1234', 'created_at': '2020-06-17T16:44:55-07:00', 'trigger': 'NO_ACTIVE_SESSION', 'webhook': {'id': '1234', 'type': 'webhook'}, 'created_by': {'type': 'user', 'id': '2', 'name': 'Anonymous User', 'login': ''}, 'source': {'id': '1234', 'type': 'file'}, 'additional_info': []
If there is working sample code for this in a blog post, tutorial, etc. please point me to it. I've done some searching around without success. Thanks!
-
The documentation in here is useful:
https://github.com/box/box-python-sdk/tree/master/docs/usage
was surprised it isn't also included with the readthedocs:
https://box-python-sdk.readthedocs.io/en/stable/index.html
or landing page:
http://opensource.box.com/box-python-sdk/
When I look at the response of this endpoint: https://developer.box.com/reference/get-webhooks-id#response-example
there is an address returned that doesn't seem to be available through the SDK, am I missing it or is it a minor bug?
response = client.make_request('GET', 'https://api.box.com/2.0/webhooks/1234') print(response.json()['address'])
サインインしてコメントを残してください。
コメント
1件のコメント