-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
azureKeyCredentialPolicy.ts
55 lines (48 loc) · 1.52 KB
/
azureKeyCredentialPolicy.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { KeyCredential } from "@azure/core-auth";
import {
RequestPolicyFactory,
RequestPolicy,
RequestPolicyOptions,
BaseRequestPolicy,
HttpOperationResponse,
RequestPolicyOptionsLike,
WebResourceLike
} from "@azure/core-http";
const API_KEY_HEADER_NAME = "Ocp-Apim-Subscription-Key";
/**
* Create an HTTP pipeline policy to authenticate a request
* using an `AzureKeyCredential` for Azure Form Recognizer
*/
export function createFormRecognizerAzureKeyCredentialPolicy(
credential: KeyCredential
): RequestPolicyFactory {
return {
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new FormRecognizerAzureKeyCredentialPolicy(nextPolicy, options, credential);
}
};
}
/**
* A concrete implementation of an AzureKeyCredential policy
* using the appropriate header for Azure Form Recognizer
*/
class FormRecognizerAzureKeyCredentialPolicy extends BaseRequestPolicy {
private credential: KeyCredential;
constructor(
nextPolicy: RequestPolicy,
options: RequestPolicyOptionsLike,
credential: KeyCredential
) {
super(nextPolicy, options);
this.credential = credential;
}
public async sendRequest(webResource: WebResourceLike): Promise<HttpOperationResponse> {
if (!webResource) {
throw new Error("webResource cannot be null or undefined");
}
webResource.headers.set(API_KEY_HEADER_NAME, this.credential.key);
return this._nextPolicy.sendRequest(webResource);
}
}