/
userAgentPolicy.ts
101 lines (87 loc) · 2.85 KB
/
userAgentPolicy.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { HttpHeaders } from "../httpHeaders";
import { HttpOperationResponse } from "../httpOperationResponse";
import { Constants } from "../util/constants";
import { WebResourceLike } from "../webResource";
import { getDefaultUserAgentKey, getPlatformSpecificData } from "./msRestUserAgentPolicy";
import {
BaseRequestPolicy,
RequestPolicy,
RequestPolicyFactory,
RequestPolicyOptions
} from "./requestPolicy";
export type TelemetryInfo = { key?: string; value?: string };
/**
* Options for adding user agent details to outgoing requests.
*/
export interface UserAgentOptions {
/*
* String prefix to add to the user agent for outgoing requests.
* Defaults to an empty string.
*/
userAgentPrefix?: string;
}
function getRuntimeInfo(): TelemetryInfo[] {
const msRestRuntime = {
key: "core-http",
value: Constants.coreHttpVersion
};
return [msRestRuntime];
}
function getUserAgentString(
telemetryInfo: TelemetryInfo[],
keySeparator = " ",
valueSeparator = "/"
): string {
return telemetryInfo
.map((info) => {
const value = info.value ? `${valueSeparator}${info.value}` : "";
return `${info.key}${value}`;
})
.join(keySeparator);
}
export const getDefaultUserAgentHeaderName = getDefaultUserAgentKey;
export function getDefaultUserAgentValue(): string {
const runtimeInfo = getRuntimeInfo();
const platformSpecificData = getPlatformSpecificData();
const userAgent = getUserAgentString(runtimeInfo.concat(platformSpecificData));
return userAgent;
}
export function userAgentPolicy(userAgentData?: TelemetryInfo): RequestPolicyFactory {
const key: string =
!userAgentData || userAgentData.key === undefined || userAgentData.key === null
? getDefaultUserAgentKey()
: userAgentData.key;
const value: string =
!userAgentData || userAgentData.value === undefined || userAgentData.value === null
? getDefaultUserAgentValue()
: userAgentData.value;
return {
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new UserAgentPolicy(nextPolicy, options, key, value);
}
};
}
export class UserAgentPolicy extends BaseRequestPolicy {
constructor(
readonly _nextPolicy: RequestPolicy,
readonly _options: RequestPolicyOptions,
protected headerKey: string,
protected headerValue: string
) {
super(_nextPolicy, _options);
}
sendRequest(request: WebResourceLike): Promise<HttpOperationResponse> {
this.addUserAgentHeader(request);
return this._nextPolicy.sendRequest(request);
}
addUserAgentHeader(request: WebResourceLike): void {
if (!request.headers) {
request.headers = new HttpHeaders();
}
if (!request.headers.get(this.headerKey) && this.headerValue) {
request.headers.set(this.headerKey, this.headerValue);
}
}
}