-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
faceClient.ts
70 lines (62 loc) · 2.43 KB
/
faceClient.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger.js";
import { TokenCredential, KeyCredential } from "@azure/core-auth";
import { FaceClient } from "./clientDefinitions.js";
import { Versions } from "./models.js";
export interface FaceClientOptions extends ClientOptions {
apiVersion?: Versions;
}
/**
* Initialize a new instance of `FaceClient`
* @param endpointParam - Supported Cognitive Services endpoints (protocol and hostname, for example:
* https://{resource-name}.cognitiveservices.azure.com).
* @param credentials - uniquely identify client credential
* @param options - the parameter for all optional parameters
*/
export default function createClient(
endpointParam: string,
credentials: TokenCredential | KeyCredential,
options: FaceClientOptions = {},
): FaceClient {
const apiVersion = options.apiVersion ?? "v1.1-preview.1";
const endpointUrl = options.endpoint ?? options.baseUrl ?? `${endpointParam}/face/${apiVersion}`;
const userAgentInfo = `azsdk-js-ai-vision-face-rest/1.0.0-beta.1`;
const userAgentPrefix =
options.userAgentOptions && options.userAgentOptions.userAgentPrefix
? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}`
: `${userAgentInfo}`;
options = {
...options,
userAgentOptions: {
userAgentPrefix,
},
loggingOptions: {
logger: options.loggingOptions?.logger ?? logger.info,
},
credentials: {
scopes: options.credentials?.scopes ?? ["https://cognitiveservices.azure.com/.default"],
apiKeyHeaderName: options.credentials?.apiKeyHeaderName ?? "Ocp-Apim-Subscription-Key",
},
};
const client = getClient(endpointUrl, credentials, options) as FaceClient;
client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
client.pipeline.addPolicy({
name: "VerifyImageFilenamePolicy",
sendRequest: (request, next) => {
for (const part of request.multipartBody?.parts ?? []) {
const contentDisposition = part.headers.get("content-disposition");
if (
contentDisposition &&
contentDisposition.includes(`name="VerifyImage"`) &&
!contentDisposition.includes("filename=")
) {
part.headers.set("content-disposition", `form-data; name="VerifyImage"; filename="blob"`);
}
}
return next(request);
},
});
return client;
}