1 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
2 | // Licensed under the MIT License. See License.txt in the project root for license information.
|
3 |
|
4 | import { HttpOperationResponse } from "../httpOperationResponse";
|
5 | import { HttpPipelineLogger } from "../httpPipelineLogger";
|
6 | import { HttpPipelineLogLevel } from "../httpPipelineLogLevel";
|
7 | import { WebResourceLike } from "../webResource";
|
8 |
|
9 | /**
|
10 | * Creates a new RequestPolicy per-request that uses the provided nextPolicy.
|
11 | */
|
12 | export type RequestPolicyFactory = {
|
13 | create(nextPolicy: RequestPolicy, options: RequestPolicyOptionsLike): RequestPolicy;
|
14 | };
|
15 |
|
16 | export interface RequestPolicy {
|
17 | sendRequest(httpRequest: WebResourceLike): Promise<HttpOperationResponse>;
|
18 | }
|
19 |
|
20 | export abstract class BaseRequestPolicy implements RequestPolicy {
|
21 | protected constructor(
|
22 | readonly _nextPolicy: RequestPolicy,
|
23 | readonly _options: RequestPolicyOptionsLike
|
24 | ) {}
|
25 |
|
26 | public abstract sendRequest(webResource: WebResourceLike): Promise<HttpOperationResponse>;
|
27 |
|
28 | /**
|
29 | * Get whether or not a log with the provided log level should be logged.
|
30 | * @param logLevel The log level of the log that will be logged.
|
31 | * @returns Whether or not a log with the provided log level should be logged.
|
32 | */
|
33 | public shouldLog(logLevel: HttpPipelineLogLevel): boolean {
|
34 | return this._options.shouldLog(logLevel);
|
35 | }
|
36 |
|
37 | /**
|
38 | * Attempt to log the provided message to the provided logger. If no logger was provided or if
|
39 | * the log level does not meat the logger's threshold, then nothing will be logged.
|
40 | * @param logLevel The log level of this log.
|
41 | * @param message The message of this log.
|
42 | */
|
43 | public log(logLevel: HttpPipelineLogLevel, message: string): void {
|
44 | this._options.log(logLevel, message);
|
45 | }
|
46 | }
|
47 |
|
48 | /**
|
49 | * Optional properties that can be used when creating a RequestPolicy.
|
50 | */
|
51 | export interface RequestPolicyOptionsLike {
|
52 | /**
|
53 | * Get whether or not a log with the provided log level should be logged.
|
54 | * @param logLevel The log level of the log that will be logged.
|
55 | * @returns Whether or not a log with the provided log level should be logged.
|
56 | */
|
57 | shouldLog(logLevel: HttpPipelineLogLevel): boolean;
|
58 |
|
59 | /**
|
60 | * Attempt to log the provided message to the provided logger. If no logger was provided or if
|
61 | * the log level does not meet the logger's threshold, then nothing will be logged.
|
62 | * @param logLevel The log level of this log.
|
63 | * @param message The message of this log.
|
64 | */
|
65 | log(logLevel: HttpPipelineLogLevel, message: string): void;
|
66 | }
|
67 |
|
68 | /**
|
69 | * Optional properties that can be used when creating a RequestPolicy.
|
70 | */
|
71 | export class RequestPolicyOptions implements RequestPolicyOptionsLike {
|
72 | constructor(private _logger?: HttpPipelineLogger) {}
|
73 |
|
74 | /**
|
75 | * Get whether or not a log with the provided log level should be logged.
|
76 | * @param logLevel The log level of the log that will be logged.
|
77 | * @returns Whether or not a log with the provided log level should be logged.
|
78 | */
|
79 | public shouldLog(logLevel: HttpPipelineLogLevel): boolean {
|
80 | return (
|
81 | !!this._logger &&
|
82 | logLevel !== HttpPipelineLogLevel.OFF &&
|
83 | logLevel <= this._logger.minimumLogLevel
|
84 | );
|
85 | }
|
86 |
|
87 | /**
|
88 | * Attempt to log the provided message to the provided logger. If no logger was provided or if
|
89 | * the log level does not meat the logger's threshold, then nothing will be logged.
|
90 | * @param logLevel The log level of this log.
|
91 | * @param message The message of this log.
|
92 | */
|
93 | public log(logLevel: HttpPipelineLogLevel, message: string): void {
|
94 | if (this._logger && this.shouldLog(logLevel)) {
|
95 | this._logger.log(logLevel, message);
|
96 | }
|
97 | }
|
98 | }
|