-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathhttpRequestUtils.ts
95 lines (81 loc) · 3.07 KB
/
httpRequestUtils.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
/**
* @module botbuilder-ai
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import * as os from 'os';
import { KnowledgeBaseAnswers } from '../qnamaker-interfaces/knowledgeBaseAnswers';
import { QnAMakerEndpoint } from '../qnamaker-interfaces/qnamakerEndpoint';
import { QnAMakerResults } from '../qnamaker-interfaces/qnamakerResults';
import { getFetch } from '../globals';
const fetch = getFetch();
// eslint-disable-next-line @typescript-eslint/no-require-imports
const pjson: Record<'name' | 'version', string> = require('../../package.json');
/**
* Http request utils class.
*
* @summary
* This class is helper class for all the http request operations.
*/
export class HttpRequestUtils {
/**
* Execute Http request.
*
* @param {string} requestUrl Http request url.
* @param {string} payloadBody Http request body.
* @param {QnAMakerEndpoint} endpoint QnA Maker endpoint details.
* @param {number} timeout (Optional)Timeout for http call
* @returns {QnAMakerResults} a promise that resolves to the QnAMakerResults
*/
async executeHttpRequest(
requestUrl: string,
payloadBody: string,
endpoint: QnAMakerEndpoint,
timeout?: number,
): Promise<QnAMakerResults | KnowledgeBaseAnswers | undefined> {
if (!requestUrl) {
throw new TypeError('Request url cannot be null.');
}
if (!payloadBody) {
throw new TypeError('Payload body cannot be null.');
}
if (!endpoint) {
throw new TypeError('Payload body cannot be null.');
}
const headers = this.getHeaders(endpoint);
const qnaResult = await fetch(requestUrl, {
method: 'POST',
headers: headers,
timeout: timeout,
body: payloadBody,
});
return qnaResult.status !== 204 ? await qnaResult.json() : undefined;
}
/**
* Sets headers for request to QnAMaker service.
*
* The [QnAMakerEndpointKey](#QnAMakerEndpoint.QnAMakerEndpointKey) is set as the value of
* `Authorization` header for v4.0 and later of QnAMaker service.
*
* Legacy QnAMaker services use the `Ocp-Apim-Subscription-Key` header for the QnAMakerEndpoint value instead.
*
* [QnAMaker.getHeaders()](#QnAMaker.getHeaders) also gets the User-Agent header value.
*
* @private
*/
private getHeaders(endpoint: QnAMakerEndpoint): Record<string, string> {
const headers = {};
headers['Ocp-Apim-Subscription-Key'] = endpoint.endpointKey;
headers['Authorization'] = `EndpointKey ${endpoint.endpointKey}`;
headers['User-Agent'] = this.getUserAgent();
headers['Content-Type'] = 'application/json';
return headers;
}
private getUserAgent(): string {
const packageUserAgent = `${pjson.name}/${pjson.version}`;
const platformUserAgent = `(${os.arch()}-${os.type()}-${os.release()}; Node.js,Version=${process.version})`;
return `${packageUserAgent} ${platformUserAgent}`;
}
}