Files
Deyaaeldeen AlmahallawiandGitHub 07131a27f7 [document intelligence] Use safe secrets (#33551)
Overhauls tests and samples for Azure Cognitive Services for Language
SDKs by implementing safe secret standards:
- Updates `test-resources.bicep` to not output API keys nor SAS tokens
- Updates the tests to retrieve API keys using the arm client
- Updates the tests to create SAS tokens for Storage container access
- Updates the samples and snippets to use Entra ID authentication
- Fix live tests by enabling SAS access to the persistent storage
account

Live tests run:
https://dev.azure.com/azure-sdk/internal/_build/results?buildId=4694655&view=results
2025-03-27 17:02:52 -07:00

78 lines
3.0 KiB
TypeScript

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* This sample shows how to extract elements of an identity document (such as a driver license or passport) from a URL
* to a file using the prebuilt identity document model.
*
* The prebuilt identity document model can return several fields. For a detailed list of the fields supported by the
* identity document model, see the `IdentityDocument` type in the documentation, or refer to the following link:
*
* https://aka.ms/azsdk/documentitelligence/iddocumentfieldschema
*
* @summary extract data from an identity document
* @azsdk-skip-javascript
*/
import type { AnalyzeOperationOutput } from "@azure-rest/ai-document-intelligence";
import DocumentIntelligence, {
getLongRunningPoller,
isUnexpected,
} from "@azure-rest/ai-document-intelligence";
import { DefaultAzureCredential } from "@azure/identity";
import "dotenv/config";
async function main(): Promise<void> {
const client = DocumentIntelligence(
process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"] || "<cognitive services endpoint>",
new DefaultAzureCredential(),
);
const initialResponse = await client
.path("/documentModels/{modelId}:analyze", "prebuilt-idDocument")
.post({
contentType: "application/json",
body: {
// The Document Intelligence service will access the following URL to a driver license image and extract data from it
urlSource:
"https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/identityDocument/license.png",
},
queryParameters: { locale: "en-IN" },
});
if (isUnexpected(initialResponse)) {
throw initialResponse.body.error;
}
const poller = getLongRunningPoller(client, initialResponse);
const analyzeResult = ((await poller.pollUntilDone()).body as AnalyzeOperationOutput)
.analyzeResult;
const documents = analyzeResult?.documents;
const document = documents && documents[0];
// Use of PrebuiltModels.Receipt above (rather than the raw model ID), adds strong typing of the model's output
if (document) {
// The identity document model has multiple document types, so we need to know which document type was actually
// extracted.
if (document.docType === "idDocument.driverLicense") {
// For the sake of the example, we'll only show a few of the fields that are produced.
console.log("Extracted a Driver License:");
console.log(document.fields);
} else if (document.docType === "idDocument.passport") {
console.log("Extracted a Passport:");
console.log(document.fields);
} else {
// The only reason this would happen is if the client library's schema for the prebuilt identity document model is
// out of date, and a new document type has been introduced.
console.error("Unknown document type in result:", document);
}
} else {
throw new Error("Expected at least one receipt in the result.");
}
}
main().catch((error) => {
console.error("An error occurred:", error);
process.exit(1);
});