Skip to content

Commit 524047f

Browse files
RomanBachaloSigmaSoftwarepaigesrossimeihDS
authored
Added post web query code example (#69)
* added example * fixes * fixes * removed empty row * fixing text * removed period on homepage * text changes and default dates * lowercase g * move period after close tag * update Monitor 2 results page title and description * delete redundant title * updated title/description of Monitor 1 results page Co-authored-by: Paige Rossi <paige.rossi@docusign.com> Co-authored-by: meihDS <70775251+meihDS@users.noreply.github.com>
1 parent 0c84256 commit 524047f

11 files changed

Lines changed: 181 additions & 7 deletions

app/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
elif EXAMPLES_API_TYPE["Monitor"]:
5757
app.register_blueprint(monitor_views.eg001)
58+
app.register_blueprint(monitor_views.eg002)
5859

5960
elif EXAMPLES_API_TYPE["Admin"]:
6061
app.register_blueprint(admin_views.eg001)

app/monitor/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .views import eg001
2+
from .views import eg002

app/monitor/examples/eg001_get_monitoring_data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from app.monitor.utils import create_monitor_api_client
55

6-
76
class Eg001GetMonitoringDataController:
87
@staticmethod
98
def get_args():
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from docusign_monitor import DataSetApi
2+
from flask import session, request
3+
from datetime import datetime
4+
5+
from app.monitor.utils import create_monitor_api_client
6+
7+
8+
class Eg002PostWebQueryController:
9+
@staticmethod
10+
def get_args():
11+
"""Get required session and request arguments"""
12+
return {
13+
"account_id": session["ds_account_id"], # Represents your {ACCOUNT_ID}
14+
"access_token": session["ds_access_token"], # Represents your {ACCESS_TOKEN}
15+
"start_date": request.form.get("start_date"),
16+
"end_date": request.form.get("end_date")
17+
}
18+
19+
@classmethod
20+
def worker(cls, args):
21+
"""
22+
1. Create an API client with headers
23+
2. Get your monitor data via SDK
24+
"""
25+
26+
# Step 2 start
27+
# Create an API client with headers
28+
api_client = create_monitor_api_client(
29+
access_token=args["access_token"]
30+
)
31+
32+
# Step 2 end
33+
34+
# Step 4 start
35+
dataset_api = DataSetApi(api_client=api_client)
36+
result = dataset_api.post_web_query(
37+
data_set_name="monitor",
38+
version="2.0",
39+
web_query=cls.get_query(args))
40+
# Step 4 end
41+
42+
43+
return result
44+
45+
# Step 3 start
46+
@classmethod
47+
def get_query(cls, args):
48+
return {
49+
"filters": [
50+
{
51+
"FilterName": "Time",
52+
"BeginTime": datetime.strptime(args['start_date'], "%Y-%m-%d"),
53+
"EndTime": datetime.strptime(args['end_date'], "%Y-%m-%d")
54+
},
55+
{
56+
"FilterName": "Has",
57+
"ColumnName": "AccountId",
58+
"Value": args["account_id"]
59+
}
60+
],
61+
"aggregations": [
62+
{
63+
"aggregationName": "Raw",
64+
"limit": "1",
65+
"orderby": [
66+
"Timestamp, desc"
67+
]
68+
}
69+
]
70+
}
71+
72+
# Step 3 end

app/monitor/templates/eg001_get_monitoring_data.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ <h4>1. Get monitoring data</h4>
88
{% endif %}
99

1010
<p>API method used:
11-
<a target='_blank' href="https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/getstream/">DataSet:getStream</a>
11+
<a target='_blank' href="https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/getstream/">DataSet:getStream</a>.
1212
</p>
1313
<p>
1414
View source file <a target="_blank" href="{{ source_url | safe }}">{{ source_file }}</a> on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!-- extend base layout --> {% extends "base.html" %} {% block content %}
2+
3+
<h4>2. Query monitoring data with filters</h4>
4+
<p>Demonstrates how to query an organization's data based on specified filters and aggregations.</p>
5+
6+
{% if show_doc %}
7+
<p><a target='_blank' href='{{ documentation | safe }}'>Documentation</a> about this example.</p>
8+
{% endif %}
9+
10+
<p>API method used:
11+
<a target='_blank' href="https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/postwebquery/">DataSet:postWebQuery</a>.
12+
</p>
13+
<p>
14+
View source file <a target="_blank" href="{{ source_url | safe }}">{{ source_file }}</a> on GitHub.
15+
</p>
16+
17+
<p>Please select start and end dates.</p>
18+
19+
<form class="eg" action="" method="post" data-busy="form">
20+
<div class="form-group">
21+
<label for="start_date">Start date</label>
22+
<input type="date" class="form-control" id="start_date" name="start_date" value="{{start_date}}" required>
23+
</div>
24+
<div class="form-group">
25+
<label for="end_date">End date</label>
26+
<input type="date" class="form-control" id="end_date" name="end_date" value="{{end_date}}" required>
27+
</div>
28+
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
29+
<button type="submit" class="btn btn-docu">Continue</button>
30+
</form>
31+
32+
{% endblock %}

app/monitor/templates/home_monitor.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<tr>
1010
<td>
1111
<h1 class="display-4">Python Launcher</h1>
12-
<p class="Xlead">Welcome to the Python code examples for DocuSign Monitor API with JWT Grant authentication</p>
12+
<p class="Xlead">Run and explore Monitor API code examples with JWT Grant authentication</p>
1313
</td>
1414
<td>
1515
<img src="/static/assets/banner-code.png" />
@@ -25,13 +25,20 @@ <h1 class="display-4">Python Launcher</h1>
2525
<p><a target='_blank' href='{{ documentation | safe }}'>Documentation</a> on using JWT from a Python Flask application.</p>
2626
{% endif %}
2727

28-
<h2>Monitor API Code Examples</h2>
28+
2929
<h4 id="example001">1. <a href="eg001">Get monitoring data</a></h4>
3030
<p>Demonstrates how to get and display all of your organization’s monitoring data.</p>
3131

3232
<p>API method used:
3333
<a target='_blank' href="https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/getstream/">DataSet:getStream</a>
3434
</p>
35+
36+
<h4 id="example002">2. <a href="eg002">Query monitoring data with filters</a></h4>
37+
<p>Demonstrates how to query an organization's data based on specified filters and aggregations.</p>
38+
39+
<p>API method used:
40+
<a target='_blank' href="https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/postwebquery/">DataSet:postWebQuery</a>
41+
</p>
3542
</div>
3643
</div>
3744

app/monitor/views/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .eg001_get_monitoring_data import eg001
2+
from .eg002_post_web_query import eg002

app/monitor/views/eg001_get_monitoring_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def get_monitoring_data():
3535
return render_template(
3636
"example_done.html",
3737
title="Get monitoring data",
38-
h1="Monitoring data result",
39-
message="Results from DataSet:GetStream method:",
38+
h1="Get monitoring data",
39+
message="Results from DataSet:getStream method:",
4040
json=json.dumps(json.dumps(results, default=str))
4141
)
4242

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Example 002: Post web query. """
2+
3+
import json
4+
from os import path
5+
from datetime import date, timedelta
6+
7+
from docusign_monitor.client.api_exception import ApiException
8+
from flask import Blueprint, render_template, current_app
9+
10+
from app.docusign import authenticate
11+
from app.error_handlers import process_error
12+
from ..examples.eg002_post_web_query import Eg002PostWebQueryController
13+
from ...ds_config import DS_CONFIG
14+
15+
eg = "eg002" # Reference (and URL) for this example
16+
eg002 = Blueprint(eg, __name__)
17+
18+
@eg002.route(f"/{eg}", methods=["POST"])
19+
@authenticate(eg=eg)
20+
def get_monitoring_data():
21+
"""
22+
1. Get required arguments
23+
2. Call the worker method
24+
3. Render the response
25+
"""
26+
27+
# 1. Get required arguments
28+
args = Eg002PostWebQueryController.get_args()
29+
try:
30+
# 2. Call the worker method
31+
results = Eg002PostWebQueryController.worker(args)
32+
current_app.logger.info(f"""Got your monitor data""")
33+
except ApiException as err:
34+
return process_error(err)
35+
36+
return render_template(
37+
"example_done.html",
38+
title="Query monitoring data with filters",
39+
h1="Query monitoring data with filters",
40+
message="Results from DataSet:postWebQuery method:",
41+
json=json.dumps(json.dumps(results.to_dict()))
42+
)
43+
44+
@eg002.route(f"/{eg}", methods=["GET"])
45+
@authenticate(eg=eg)
46+
def get_view():
47+
""" Responds with the form for the example"""
48+
49+
current_date = date.today()
50+
start_date = date.today() - timedelta(10)
51+
52+
return render_template(
53+
f"{eg}_post_web_query.html",
54+
title="Get monitoring data",
55+
source_file=f"{eg}_post_web_query.py",
56+
source_url=DS_CONFIG["monitor_github_url"] + f"{eg}_post_web_query.py",
57+
documentation=DS_CONFIG["documentation"] + eg,
58+
start_date=start_date,
59+
end_date=current_date
60+
)
61+

0 commit comments

Comments
 (0)