Skip to content

Commit db05bb0

Browse files
added example
1 parent 6aa7a45 commit db05bb0

File tree

11 files changed

+288
-140
lines changed

11 files changed

+288
-140
lines changed

app/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .click import views as click_views
1212
from .monitor import views as monitor_views
1313
from .admin import views as admin_views
14+
from .connect import views as connect_views
1415
from .views import core
1516

1617
session_path = "/tmp/python_recipe_sessions"
@@ -110,6 +111,8 @@
110111
app.register_blueprint(esignature_views.eg043)
111112
app.register_blueprint(esignature_views.eg044)
112113

114+
app.register_blueprint(connect_views.cneg001)
115+
113116
if "DYNO" in os.environ: # On Heroku?
114117
import logging
115118

app/connect/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .views import cneg001
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from flask import request
2+
import hmac
3+
import hashlib
4+
import base64
5+
6+
class Eg001ValidateWebhookMessageController:
7+
@staticmethod
8+
def get_args():
9+
"""Get required session and request arguments"""
10+
return {
11+
"secret": request.form.get("secret"),
12+
"payload": request.form.get("payload"),
13+
}
14+
15+
@staticmethod
16+
def worker(args):
17+
"""
18+
1. Create an API client with headers
19+
2. Get your monitor data via SDK
20+
"""
21+
#ds-snippet-start:Connect1Step2
22+
key = bytes(args['secret'], 'utf-8')
23+
payload = bytes(args['payload'], 'utf-8')
24+
25+
hmac_hash = hmac.new(key, payload, hashlib.sha256)
26+
result = base64.b64encode(hmac_hash.digest()).decode('utf-8')
27+
#ds-snippet-end:Connect1Step2
28+
29+
return result

app/connect/views/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .eg001_validate_webhook_message import cneg001
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Example 001: Validate webhook message using HMAC. """
2+
3+
from docusign_monitor.client.api_exception import ApiException
4+
from flask import Blueprint, render_template, session
5+
6+
from app.docusign import authenticate, ensure_manifest, get_example_by_number
7+
from app.error_handlers import process_error
8+
from ..examples.eg001_validate_webhook_message import Eg001ValidateWebhookMessageController
9+
from ...ds_config import DS_CONFIG
10+
from ...consts import API_TYPE
11+
12+
example_number = 1
13+
api = API_TYPE["CONNECT"]
14+
eg = f"cneg00{example_number}" # Reference (and URL) for this example
15+
cneg001 = Blueprint(eg, __name__)
16+
17+
@cneg001.route(f"/{eg}", methods=["POST"])
18+
@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"])
19+
def get_monitoring_data():
20+
"""
21+
1. Get required arguments
22+
2. Call the worker method
23+
3. Render the response
24+
"""
25+
example = get_example_by_number(session["manifest"], example_number, api)
26+
27+
# 1. Get required arguments
28+
args = Eg001ValidateWebhookMessageController.get_args()
29+
try:
30+
# 2. Call the worker method to compute hash
31+
results = Eg001ValidateWebhookMessageController.worker(args)
32+
except ApiException as err:
33+
return process_error(err)
34+
35+
return render_template(
36+
"example_done.html",
37+
title=example["ExampleName"],
38+
message=example["ResultsPageText"].format(results)
39+
)
40+
41+
@cneg001.route(f"/{eg}", methods=["GET"])
42+
@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"])
43+
def get_view():
44+
""" Responds with the form for the example"""
45+
example = get_example_by_number(session["manifest"], example_number, api)
46+
47+
return render_template(
48+
"connect/eg001_validate_webhook_message.html",
49+
title=example["ExampleName"],
50+
example=example,
51+
source_file= "eg001_validate_webhook_message.py",
52+
source_url=DS_CONFIG["monitor_github_url"] + "eg001_validate_webhook_message.py",
53+
documentation=DS_CONFIG["documentation"] + eg,
54+
)
55+

app/consts.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,6 @@
108108
"MONITOR": "Monitor",
109109
"CLICK": "Click",
110110
"ROOMS": "Rooms",
111-
"ADMIN": "Admin"
111+
"ADMIN": "Admin",
112+
"CONNECT": "Connect"
112113
}

0 commit comments

Comments
 (0)