-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
hooks.ts
177 lines (160 loc) · 6.03 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* @summary This sample demonstrates Metrics Advisor Hooks CRUD operations.
*/
// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();
import {
MetricsAdvisorKeyCredential,
MetricsAdvisorAdministrationClient,
EmailNotificationHook,
WebNotificationHook,
EmailNotificationHookPatch
} from "@azure/ai-metrics-advisor";
export async function main() {
// You will need to set these environment variables or edit the following values
const endpoint = process.env["METRICS_ADVISOR_ENDPOINT"] || "<service endpoint>";
const subscriptionKey = process.env["METRICS_ADVISOR_SUBSCRIPTION_KEY"] || "<subscription key>";
const apiKey = process.env["METRICS_ADVISOR_API_KEY"] || "<api key>";
const existingHookId = process.env["METRICS_ADVISOR_HOOK_ID"] || "<hook id>";
const credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);
const adminClient = new MetricsAdvisorAdministrationClient(endpoint, credential);
await getHook(adminClient, existingHookId);
const createdEmailHook = await createEmailHook(adminClient);
await updateEmailHook(adminClient, createdEmailHook.id!);
const createdWebHook = await createWebHook(adminClient);
await listHooks(adminClient);
await deleteHook(adminClient, createdEmailHook.id!);
await deleteHook(adminClient, createdWebHook.id!);
}
async function createWebHook(client: MetricsAdvisorAdministrationClient) {
console.log("Creating a new web hook...");
const hook: WebNotificationHook = {
hookType: "Webhook",
name: "js web hook example" + new Date().getTime().toString(),
description: "description",
hookParameter: {
endpoint: "https://httpbin.org/post",
username: "user",
password: "pass",
headers: {
name1: "value1",
name2: "value2"
}
// certificateKey: "k",
// certificatePassword: "kp"
}
};
const created = await client.createHook(hook);
console.log(` hook created: ${created.id}`);
return created;
}
async function createEmailHook(client: MetricsAdvisorAdministrationClient) {
console.log("Creating a new email hook...");
const hook: EmailNotificationHook = {
hookType: "Email",
name: "js email hook example" + new Date().getTime().toString(),
description: "description",
hookParameter: { toList: ["test@example.com"] }
};
const created = await client.createHook(hook);
console.log(` hook created: ${created.id}`);
return created;
}
async function getHook(client: MetricsAdvisorAdministrationClient, hookId: string) {
console.log(`Retrieving an existing hook for id ${hookId}...`);
const result = await client.getHook(hookId);
console.log(result.name);
console.log(result.description);
console.log(result.admins);
}
async function updateEmailHook(client: MetricsAdvisorAdministrationClient, hookId: string) {
console.log(`Updating hook ${hookId}`);
const emailPatch: EmailNotificationHookPatch = {
hookType: "Email",
hookParameter: {
toList: ["test2@example.com", "test3@example.com"]
}
};
const response = await client.updateHook(hookId, emailPatch);
console.log(response);
return response;
}
async function listHooks(client: MetricsAdvisorAdministrationClient) {
console.log("Listing existing hooks");
console.log(" using for-await-of syntax");
let i = 1;
const iterator = client.listHooks({
hookName: "js "
});
for await (const hook of iterator) {
console.log(`hook ${i++} - type ${hook.hookType}`);
console.log(` description: ${hook.description}`);
if (hook.hookType === "Email") {
console.log(` TO: list ${hook.hookParameter.toList}`);
} else {
console.log(` endpoint: ${hook.hookParameter.endpoint}`);
console.log(` username: ${hook.hookParameter.username}`);
if (hook.hookParameter.headers) {
console.log(` headers:`);
for (const key of Object.keys(hook.hookParameter.headers)) {
console.log(` ${key}: ${hook.hookParameter.headers[key]}`);
}
}
console.log(` certificate key: ${hook.hookParameter.certificateKey}`);
}
}
console.log(" by pages");
i = 1;
const pages = client.listHooks({ hookName: "js " }).byPage({ maxPageSize: 5 });
let page = await pages.next();
while (!(page.done === true)) {
for (const hook of page.value) {
console.log(` hook ${i++} - type ${hook.hookType}`);
console.log(` id: ${hook.id}`);
console.log(` description: ${hook.description}`);
if (hook.hookType === "Email") {
console.log(` TO: list ${hook.hookParameter.toList}`);
} else {
console.log(` endpoint: ${hook.hookParameter.endpoint}`);
console.log(` username: ${hook.hookParameter.username}`);
if (hook.hookParameter.headers) {
console.log(` headers:`);
for (const key of Object.keys(hook.hookParameter.headers)) {
console.log(` ${key}: ${hook.hookParameter.headers[key]}`);
}
}
console.log(` certificate key: ${hook.hookParameter.certificateKey}`);
}
}
console.log(` next: ${page.value.continuationToken}`);
page = await pages.next();
}
console.log(" resume paging using continuation token");
const pageIterator = client.listHooks({ hookName: "js " }).byPage({ maxPageSize: 5 });
const firstPage = await pageIterator.next();
if (firstPage.done !== true) {
const newIterator = client
.listHooks({ hookName: "js " })
.byPage({ continuationToken: firstPage.value.continuationToken });
const secondPage = await newIterator.next();
console.log(" Second page:");
for (const hook of secondPage.value) {
console.log(` id: ${hook.id}`);
}
}
}
async function deleteHook(client: MetricsAdvisorAdministrationClient, hookId: string) {
console.log(`Deleting hook ${hookId}`);
await client.deleteHook(hookId);
}
main()
.then((_) => {
console.log("Succeeded");
})
.catch((err) => {
console.log("Error occurred:");
console.log(err);
});