33
44Quickstart for this example:
551) Run Flask locally withg the following command:
6- - flask --app examples/subscription_account_webhook .py run --debug
6+ - flask --app examples/subscriptions_example .py run --debug
772) Expose HTTPS via a tunnel to your localhost:5000:
88 - Free: pinggy (https://pinggy.io/) to get https://<subdomain>.pinggy.link -> http://localhost:5000
99 - Paid/free-tier: ngrok (https://ngrok.com/): ngrok http 5000, note the https URL.
10103) Use the tunnel HTTPS URL as notification_url pointing to /webhook, URL-encoded.
11114) To create a subscription, follow the example request below:
1212 - https://<your-tunnel-host>/subscriptions?notification_url=https%3A%2F%2F<your-tunnel-host>%2Fwebhook&client_state=abc123
13- 4) To renew a subscription, follow the example request below:
13+ 5) To list subscriptions, follow the example request below:
14+ - http://<your-tunnel-host>/subscriptions/list
15+ 6) To renew a subscription, follow the example request below:
1416 - http://<your-tunnel-host>/subscriptions/<subscription_id>/renew?expiration_minutes=55
15- 5 ) To delete a subscription, follow the example request below:
17+ 7 ) To delete a subscription, follow the example request below:
1618 - http://<your-tunnel-host>/subscriptions/<subscription_id>/delete
1719Graph will call https://<your-tunnel-host>/webhook; this app echoes validationToken and returns 202 for notifications.
1820"""
3840 ])
3941
4042RESOURCE = "/me/mailFolders('inbox')/messages"
41- DEFAULT_EXPIRATION_MINUTES = 55 # Graph requires renewals before the limit
43+ DEFAULT_EXPIRATION_MINUTES = 10069 # Maximum expiration is 10,070 in the future.
4244
4345app = Flask (__name__ )
4446
@@ -63,7 +65,7 @@ def create_subscription():
6365 client_state = request .args .get ("client_state" )
6466 resource = request .args .get ("resource" , RESOURCE )
6567
66- subscription = account .subscriptions .create_subscription (
68+ subscription = account .subscriptions () .create_subscription (
6769 notification_url = notification_url ,
6870 resource = resource ,
6971 change_type = "created" ,
@@ -73,10 +75,26 @@ def create_subscription():
7375 return jsonify (subscription ), 201
7476
7577
78+ @app .get ("/subscriptions/list" )
79+ def list_subscriptions ():
80+ limit_raw = request .args .get ("limit" )
81+ limit = None
82+ if limit_raw is not None :
83+ try :
84+ limit = int (limit_raw )
85+ except ValueError :
86+ abort (400 , description = "limit must be an integer" )
87+ if limit <= 0 :
88+ abort (400 , description = "limit must be a positive integer" )
89+
90+ subscriptions = account .subscriptions ().list_subscriptions (limit = limit )
91+ return jsonify (list (subscriptions )), 200
92+
93+
7694@app .get ("/subscriptions/<subscription_id>/renew" )
7795def renew_subscription (subscription_id : str ):
7896 expiration_minutes = _int_arg ("expiration_minutes" , DEFAULT_EXPIRATION_MINUTES )
79- updated = account .subscriptions .renew_subscription (
97+ updated = account .subscriptions () .renew_subscription (
8098 subscription_id ,
8199 expiration_minutes = expiration_minutes ,
82100 )
@@ -85,7 +103,7 @@ def renew_subscription(subscription_id: str):
85103
86104@app .get ("/subscriptions/<subscription_id>/delete" )
87105def delete_subscription (subscription_id : str ):
88- deleted = account .subscriptions .delete_subscription (subscription_id )
106+ deleted = account .subscriptions () .delete_subscription (subscription_id )
89107 if not deleted :
90108 abort (404 , description = "Subscription not found" )
91109 return ("" , 204 )
0 commit comments