Skip to content

Commit 4b8c97d

Browse files
authored
Added post web query code example (#69)
* Added the post web query code example * fixed code style issues * removed extra spaces
1 parent 0649a35 commit 4b8c97d

File tree

9 files changed

+259
-10
lines changed

9 files changed

+259
-10
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"docusign/click-client": "1.1.0",
1111
"docusign/esign-client": "6.8.0",
1212
"docusign/rooms-client": "2.0.0",
13-
"docusign/monitor-client": "1.0.0",
13+
"docusign/monitor-client": "1.1.0",
1414
"twig/twig": "3.3.8",
1515
"league/oauth2-client": "2.6.1",
1616
"ext-json": "*",

composer.lock

Lines changed: 54 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Example\Controllers\Examples\Monitor;
4+
5+
use Example\Controllers\MonitorBaseController;
6+
use Example\Services\Examples\Monitor\WebQueryEndpointService;
7+
use Example\Services\JWTService;
8+
9+
class Eg002WebQueryEndpoint extends MonitorBaseController
10+
{
11+
const EG = 'meg002'; # reference (and URL) for this example
12+
const FILE = __FILE__;
13+
14+
/**
15+
* Create a new controller instance.
16+
* @return void
17+
*/
18+
public function __construct()
19+
{
20+
parent::__construct();
21+
parent::controller();
22+
}
23+
24+
/**
25+
* Check the access token and call the worker method
26+
* @return void
27+
*/
28+
public function createController(): void
29+
{
30+
$accessToken = $_SESSION['ds_access_token'];
31+
$tokenExpirationTime = $_SESSION['ds_expiration'];
32+
if (
33+
is_null($accessToken) ||
34+
(time() + JWTService::TOKEN_REPLACEMENT_IN_SECONDS) > $tokenExpirationTime
35+
) {
36+
$auth = new JWTService();
37+
$auth->login();
38+
} else {
39+
$monitoringData = WebQueryEndpointService::postWebQueryMethod(
40+
$this->clientService,
41+
$this->args['account_id'],
42+
$this->args['start_date'],
43+
$this->args['end_date']
44+
);
45+
46+
if ($monitoringData) {
47+
$this->clientService->showDoneTemplate(
48+
"Post web query",
49+
"Query monitoring data with filters",
50+
"Results from DataSet:postWebQuery method:",
51+
json_encode($monitoringData)
52+
);
53+
}
54+
}
55+
}
56+
57+
/**
58+
* Get specific template arguments
59+
* @return array
60+
*/
61+
public function getTemplateArgs(): array
62+
{
63+
return [
64+
'account_id' => $_SESSION['ds_account_id'],
65+
'start_date' => $this->checkInputValues($_POST['start_date']),
66+
'end_date' => $this->checkInputValues($_POST['end_date'])
67+
];
68+
}
69+
}

src/Controllers/MonitorBaseController.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,19 @@ private function getController(
8383
*/
8484

8585
abstract function createController(): void;
86-
86+
87+
/**
88+
* Check input values using regular expressions
89+
* @param $value
90+
* @return string
91+
*/
92+
protected function checkInputValues($value): string
93+
{
94+
return preg_replace('/([^\w \-\@\.\,])+/', '', $value);
95+
}
96+
8797
/**
98+
* Provides the default template arguments
8899
* @return array
89100
*/
90101
protected function getDefaultTemplateArgs(): array
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Example\Services\Examples\Monitor;
4+
5+
use DocuSign\Monitor\Api\DataSetApi;
6+
use DocuSign\Monitor\Model\WebQuery;
7+
use DocuSign\Monitor\Client\ApiException;
8+
use Example\Services\MonitorApiClientService;
9+
10+
class WebQueryEndpointService
11+
{
12+
/**
13+
* Method to get companies data from monitor
14+
* @param MonitorApiClientService $clientService
15+
* @param string $accountId
16+
* @param string $filterStartDate
17+
* @param string $filterEndDate
18+
* @return array
19+
*/
20+
public static function postWebQueryMethod(
21+
MonitorApiClientService $clientService,
22+
string $accountId,
23+
string $filterStartDate,
24+
string $filterEndDate): array
25+
{
26+
// Create an ApiClient and construct API headers
27+
$apiClient = $clientService->getApiClient();
28+
29+
try {
30+
$datasetApi = new DataSetApi($apiClient);
31+
32+
$webQueryResult = $datasetApi->postWebQuery(
33+
'monitor',
34+
'2.0',
35+
self::preparePostWebQuery($accountId, $filterStartDate, $filterEndDate)
36+
);
37+
38+
} catch (ApiException $e) {
39+
$clientService->showErrorTemplate($e);
40+
exit;
41+
}
42+
43+
// Cleaning the data from unsupported symbols
44+
return str_replace("'", "", $webQueryResult->getResult());
45+
}
46+
47+
public static function preparePostWebQuery(string $accountId, string $filterStartDate, string $filterEndDate): WebQuery
48+
{
49+
$webQueryOptions = new WebQuery();
50+
51+
$datesFilter = (object)[
52+
"FilterName" => "Time",
53+
"BeginTime" => $filterStartDate,
54+
"EndTime" => $filterEndDate
55+
];
56+
57+
$accountIdFilter = (object)[
58+
"FilterName" => "Has",
59+
"ColumnName" => "AccountId",
60+
"Value" => $accountId
61+
];
62+
63+
$webQueryOptions->setFilters([$datesFilter, $accountIdFilter]);
64+
65+
$aggregation = (object)[
66+
"aggregationName" => "Raw",
67+
"limit" => "1",
68+
"orderby" => ["Timestamp, desc"]
69+
];
70+
71+
$webQueryOptions->setAggregations([$aggregation]);
72+
73+
return $webQueryOptions;
74+
}
75+
}

src/Services/MonitorApiClientService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct($args)
3030
# Exceptions will be caught by the calling function
3131
# step 2 start
3232
$config = new Configuration();
33-
$accessToken = $args['ds_access_token'];
33+
$accessToken = $_SESSION['ds_access_token'];
3434

3535
$config->setAccessToken($accessToken);
3636
$config->setHost('https://lens-d.docusign.net');

src/Services/RouterService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class RouterService
6969
'reg008' => 'Rooms\EG008GrantOfficeAccessToFormGroup',
7070
'reg009' => 'Rooms\Eg009AssignFormToFormGroup',
7171
'meg001' => 'Monitor\Eg001GetMonitoringData',
72+
'meg002' => 'Monitor\Eg002WebQueryEndpoint',
7273
'aeg001' => 'Admin\EG001CreateNewUser',
7374
'aeg002' => 'Admin\EG002CreateActiveCLMESignUser',
7475
'aeg003' => 'Admin\EG003BulkExportUserData',
@@ -141,6 +142,7 @@ class RouterService
141142
"reg008" => "rooms/eg008_grant_office_access_to_form_group.html",
142143
"reg009" => "rooms/eg009_assign_form_to_form_group.html",
143144
"meg001" => "monitor/eg001_get_monitoring_data.html",
145+
"meg002" => "monitor/eg002_web_query_endpoint.html",
144146
"aeg001" => "admin/eg001_create_active_user.html",
145147
"aeg002" => "admin/eg002_create_new_esignature_clm_user.html",
146148
"aeg003" => "admin/eg003_bulk_export_user_data.html",
@@ -210,6 +212,7 @@ class RouterService
210212
"reg008" => "Grant office access to a form group",
211213
"reg009" => "Assign a form to a form group",
212214
"meg001" => "Get monitoring data",
215+
"meg002" => "Post web query",
213216
"aeg001" => "Create a new user with active status",
214217
"aeg002" => "Create a new active user for CLM and eSignature",
215218
"aeg003" => "How to bulk-export user data",

templates/home_monitor.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ <h4 id="example-c001">
3131
<p>API method used:
3232
<a target='_blank' href="https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/getstreamfordataset/">DataSet:GetStream</a>.
3333
</p>
34+
35+
<h4 id="example-c002">2. <a href="{{ app_url ~ 'index.php?page=meg002'}}">Query monitoring data with filters</a></h4>
36+
<p>
37+
Demonstrates how to query an organization's data based on specified filters and aggregations.
38+
</p>
39+
<p>
40+
API method used:
41+
<a target='_blank' href="https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/postwebquery/">DataSet:postWebQuery</a>.
42+
</p>
3443
</div>
3544

3645
<!-- anchor-js is only for the index page -->

0 commit comments

Comments
 (0)