requestPolicy.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. import { HttpOperationResponse } from "../httpOperationResponse";
  4. import { HttpPipelineLogger } from "../httpPipelineLogger";
  5. import { HttpPipelineLogLevel } from "../httpPipelineLogLevel";
  6. import { WebResource } from "../webResource";
  7. /**
  8. * Creates a new RequestPolicy per-request that uses the provided nextPolicy.
  9. */
  10. export type RequestPolicyFactory = {
  11. create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): RequestPolicy
  12. };
  13. export interface RequestPolicy {
  14. sendRequest(httpRequest: WebResource): Promise<HttpOperationResponse>;
  15. }
  16. export abstract class BaseRequestPolicy implements RequestPolicy {
  17. protected constructor(readonly _nextPolicy: RequestPolicy, readonly _options: RequestPolicyOptions) {
  18. }
  19. public abstract sendRequest(webResource: WebResource): Promise<HttpOperationResponse>;
  20. /**
  21. * Get whether or not a log with the provided log level should be logged.
  22. * @param logLevel The log level of the log that will be logged.
  23. * @returns Whether or not a log with the provided log level should be logged.
  24. */
  25. public shouldLog(logLevel: HttpPipelineLogLevel): boolean {
  26. return this._options.shouldLog(logLevel);
  27. }
  28. /**
  29. * Attempt to log the provided message to the provided logger. If no logger was provided or if
  30. * the log level does not meat the logger's threshold, then nothing will be logged.
  31. * @param logLevel The log level of this log.
  32. * @param message The message of this log.
  33. */
  34. public log(logLevel: HttpPipelineLogLevel, message: string): void {
  35. this._options.log(logLevel, message);
  36. }
  37. }
  38. /**
  39. * Optional properties that can be used when creating a RequestPolicy.
  40. */
  41. export class RequestPolicyOptions {
  42. constructor(private _logger?: HttpPipelineLogger) {
  43. }
  44. /**
  45. * Get whether or not a log with the provided log level should be logged.
  46. * @param logLevel The log level of the log that will be logged.
  47. * @returns Whether or not a log with the provided log level should be logged.
  48. */
  49. public shouldLog(logLevel: HttpPipelineLogLevel): boolean {
  50. return !!this._logger &&
  51. logLevel !== HttpPipelineLogLevel.OFF &&
  52. logLevel <= this._logger.minimumLogLevel;
  53. }
  54. /**
  55. * Attempt to log the provided message to the provided logger. If no logger was provided or if
  56. * the log level does not meat the logger's threshold, then nothing will be logged.
  57. * @param logLevel The log level of this log.
  58. * @param message The message of this log.
  59. */
  60. public log(logLevel: HttpPipelineLogLevel, message: string): void {
  61. if (this._logger && this.shouldLog(logLevel)) {
  62. this._logger.log(logLevel, message);
  63. }
  64. }
  65. }