From 9c7049c0b28ed9ea1d860c3cc90cd47977a02006 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Tue, 11 Jun 2024 10:48:41 -0400 Subject: [PATCH] add Kotlin lintered files --- kotlin/services/textract/build.gradle.kts | 4 +- .../com/kotlin/textract/AnalyzeDocument.kt | 138 ++++++------ .../com/kotlin/textract/DetectDocumentText.kt | 136 ++++++------ .../kotlin/textract/DetectDocumentTextS3.kt | 144 ++++++------ .../kotlin/textract/StartDocumentAnalysis.kt | 206 +++++++++--------- .../textract/src/test/kotlin/TextractTest.kt | 69 +++--- 6 files changed, 359 insertions(+), 338 deletions(-) diff --git a/kotlin/services/textract/build.gradle.kts b/kotlin/services/textract/build.gradle.kts index 514d15f882a..1ab42e8f618 100644 --- a/kotlin/services/textract/build.gradle.kts +++ b/kotlin/services/textract/build.gradle.kts @@ -27,8 +27,8 @@ repositories { } apply(plugin = "org.jlleitschuh.gradle.ktlint") dependencies { - implementation("aws.sdk.kotlin:textract:1.0.30") - implementation("aws.sdk.kotlin:secretsmanager:1.0.30") + implementation("aws.sdk.kotlin:textract:1.2.28") + implementation("aws.sdk.kotlin:secretsmanager:1.2.28") implementation("aws.smithy.kotlin:http-client-engine-okhttp:0.30.0") implementation("aws.smithy.kotlin:http-client-engine-crt:0.30.0") implementation("com.google.code.gson:gson:2.10") diff --git a/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/AnalyzeDocument.kt b/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/AnalyzeDocument.kt index e6fd05a6d97..1ca67a042e4 100644 --- a/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/AnalyzeDocument.kt +++ b/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/AnalyzeDocument.kt @@ -1,69 +1,69 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.textract - -// snippet-start:[textract.kotlin._analyze_doc.import] -import aws.sdk.kotlin.services.textract.TextractClient -import aws.sdk.kotlin.services.textract.model.AnalyzeDocumentRequest -import aws.sdk.kotlin.services.textract.model.Document -import aws.sdk.kotlin.services.textract.model.FeatureType -import java.io.File -import java.io.FileInputStream -import kotlin.system.exitProcess -// snippet-end:[textract.kotlin._analyze_doc.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ -suspend fun main(args: Array) { - - val usage = """ - Usage: - - - Where: - sourceDoc - The path where the document is located (must be an image, for example, C:/AWS/book.png). - """ - - if (args.size != 1) { - println(usage) - exitProcess(0) - } - - val sourceDoc = args[0] - analyzeDoc(sourceDoc) -} - -// snippet-start:[textract.kotlin._analyze_doc.main] -suspend fun analyzeDoc(sourceDoc: String?) { - - val sourceStream = FileInputStream(File(sourceDoc)) - val sourceBytes = sourceStream.readBytes() - - // Get the input Document object as bytes. - val myDoc = Document { - bytes = sourceBytes - } - - val featureTypesOb = mutableListOf() - featureTypesOb.add(FeatureType.Forms) - featureTypesOb.add(FeatureType.Tables) - - val analyzeDocumentRequest = AnalyzeDocumentRequest { - featureTypes = featureTypesOb - document = myDoc - } - - TextractClient { region = "us-east-1" }.use { textractClient -> - val response = textractClient.analyzeDocument(analyzeDocumentRequest) - response.blocks?.forEach { block -> - println("The block type is ${block.blockType}") - } - } -} -// snippet-end:[textract.kotlin._analyze_doc.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.textract + +// snippet-start:[textract.kotlin._analyze_doc.import] +import aws.sdk.kotlin.services.textract.TextractClient +import aws.sdk.kotlin.services.textract.model.AnalyzeDocumentRequest +import aws.sdk.kotlin.services.textract.model.Document +import aws.sdk.kotlin.services.textract.model.FeatureType +import java.io.File +import java.io.FileInputStream +import kotlin.system.exitProcess +// snippet-end:[textract.kotlin._analyze_doc.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ +suspend fun main(args: Array) { + val usage = """ + Usage: + + + Where: + sourceDoc - The path where the document is located (must be an image, for example, C:/AWS/book.png). + """ + + if (args.size != 1) { + println(usage) + exitProcess(0) + } + + val sourceDoc = args[0] + analyzeDoc(sourceDoc) +} + +// snippet-start:[textract.kotlin._analyze_doc.main] +suspend fun analyzeDoc(sourceDoc: String?) { + val sourceStream = FileInputStream(File(sourceDoc)) + val sourceBytes = sourceStream.readBytes() + + // Get the input Document object as bytes. + val myDoc = + Document { + bytes = sourceBytes + } + + val featureTypesOb = mutableListOf() + featureTypesOb.add(FeatureType.Forms) + featureTypesOb.add(FeatureType.Tables) + + val analyzeDocumentRequest = + AnalyzeDocumentRequest { + featureTypes = featureTypesOb + document = myDoc + } + + TextractClient { region = "us-east-1" }.use { textractClient -> + val response = textractClient.analyzeDocument(analyzeDocumentRequest) + response.blocks?.forEach { block -> + println("The block type is ${block.blockType}") + } + } +} +// snippet-end:[textract.kotlin._analyze_doc.main] diff --git a/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/DetectDocumentText.kt b/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/DetectDocumentText.kt index 2d7f8eb85a6..c02f8f3074c 100644 --- a/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/DetectDocumentText.kt +++ b/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/DetectDocumentText.kt @@ -1,68 +1,68 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.textract - -// snippet-start:[textract.kotlin._detect_doc_text.import] -import aws.sdk.kotlin.services.textract.TextractClient -import aws.sdk.kotlin.services.textract.model.DetectDocumentTextRequest -import aws.sdk.kotlin.services.textract.model.Document -import java.io.File -import java.io.FileInputStream -import kotlin.system.exitProcess -// snippet-end:[textract.kotlin._detect_doc_text.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ -suspend fun main(args: Array) { - - val usage = """ - Usage: - - - Where: - sourceDoc - The path where the document is located (must be an image, for example, C:/AWS/book.png). - """ - - if (args.size != 1) { - println(usage) - exitProcess(0) - } - - val sourceDoc = args[0] - detectDocText(sourceDoc) -} - -// snippet-start:[textract.kotlin._detect_doc_text.main] -suspend fun detectDocText(sourceDoc: String) { - - val sourceStream = FileInputStream(File(sourceDoc)) - val sourceBytes = sourceStream.readBytes() - - // Get the input Document object as bytes. - val myDoc = Document { - bytes = sourceBytes - } - - val detectDocumentTextRequest = DetectDocumentTextRequest { - document = myDoc - } - - TextractClient { region = "us-east-1" }.use { textractClient -> - val response = textractClient.detectDocumentText(detectDocumentTextRequest) - response.blocks?.forEach { block -> - println("The block type is ${block.blockType}") - } - - val documentMetadata = response.documentMetadata - if (documentMetadata != null) { - println("The number of pages in the document is ${documentMetadata.pages}") - } - } -} -// snippet-end:[textract.kotlin._detect_doc_text.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.textract + +// snippet-start:[textract.kotlin._detect_doc_text.import] +import aws.sdk.kotlin.services.textract.TextractClient +import aws.sdk.kotlin.services.textract.model.DetectDocumentTextRequest +import aws.sdk.kotlin.services.textract.model.Document +import java.io.File +import java.io.FileInputStream +import kotlin.system.exitProcess +// snippet-end:[textract.kotlin._detect_doc_text.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ +suspend fun main(args: Array) { + val usage = """ + Usage: + + + Where: + sourceDoc - The path where the document is located (must be an image, for example, C:/AWS/book.png). + """ + + if (args.size != 1) { + println(usage) + exitProcess(0) + } + + val sourceDoc = args[0] + detectDocText(sourceDoc) +} + +// snippet-start:[textract.kotlin._detect_doc_text.main] +suspend fun detectDocText(sourceDoc: String) { + val sourceStream = FileInputStream(File(sourceDoc)) + val sourceBytes = sourceStream.readBytes() + + // Get the input Document object as bytes. + val myDoc = + Document { + bytes = sourceBytes + } + + val detectDocumentTextRequest = + DetectDocumentTextRequest { + document = myDoc + } + + TextractClient { region = "us-east-1" }.use { textractClient -> + val response = textractClient.detectDocumentText(detectDocumentTextRequest) + response.blocks?.forEach { block -> + println("The block type is ${block.blockType}") + } + + val documentMetadata = response.documentMetadata + if (documentMetadata != null) { + println("The number of pages in the document is ${documentMetadata.pages}") + } + } +} +// snippet-end:[textract.kotlin._detect_doc_text.main] diff --git a/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/DetectDocumentTextS3.kt b/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/DetectDocumentTextS3.kt index c301dbd51b5..0da3b629d9e 100644 --- a/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/DetectDocumentTextS3.kt +++ b/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/DetectDocumentTextS3.kt @@ -1,70 +1,74 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.textract - -// snippet-start:[textract.kotlin._detect_s3_text.import] -import aws.sdk.kotlin.services.textract.TextractClient -import aws.sdk.kotlin.services.textract.model.DetectDocumentTextRequest -import aws.sdk.kotlin.services.textract.model.Document -import aws.sdk.kotlin.services.textract.model.S3Object -import kotlin.system.exitProcess -// snippet-end:[textract.kotlin._detect_s3_text.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ -suspend fun main(args: Array) { - - val usage = """ - Usage: - - - Where: - bucketName - The name of the Amazon S3 bucket that contains the document. - docName - The document name (must be an image, i.e., book.png). - """ - - if (args.size != 2) { - println(usage) - exitProcess(0) - } - - val bucketName = args[0] - val docName = args[1] - detectDocTextS3(bucketName, docName) -} - -// snippet-start:[textract.kotlin._detect_s3_text.main] -suspend fun detectDocTextS3(bucketName: String?, docName: String?) { - - val s3ObjectOb = S3Object { - bucket = bucketName - name = docName - } - - val myDoc = Document { - s3Object = s3ObjectOb - } - - val detectDocumentTextRequest = DetectDocumentTextRequest { - document = myDoc - } - - TextractClient { region = "us-west-2" }.use { textractClient -> - val response = textractClient.detectDocumentText(detectDocumentTextRequest) - response.blocks?.forEach { block -> - println("The block type is ${block.blockType}") - } - - val documentMetadata = response.documentMetadata - if (documentMetadata != null) { - println("The number of pages in the document is ${documentMetadata.pages}") - } - } -} -// snippet-end:[textract.kotlin._detect_s3_text.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.textract + +// snippet-start:[textract.kotlin._detect_s3_text.import] +import aws.sdk.kotlin.services.textract.TextractClient +import aws.sdk.kotlin.services.textract.model.DetectDocumentTextRequest +import aws.sdk.kotlin.services.textract.model.Document +import aws.sdk.kotlin.services.textract.model.S3Object +import kotlin.system.exitProcess +// snippet-end:[textract.kotlin._detect_s3_text.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ +suspend fun main(args: Array) { + val usage = """ + Usage: + + + Where: + bucketName - The name of the Amazon S3 bucket that contains the document. + docName - The document name (must be an image, i.e., book.png). + """ + + if (args.size != 2) { + println(usage) + exitProcess(0) + } + + val bucketName = args[0] + val docName = args[1] + detectDocTextS3(bucketName, docName) +} + +// snippet-start:[textract.kotlin._detect_s3_text.main] +suspend fun detectDocTextS3( + bucketName: String?, + docName: String? +) { + val s3ObjectOb = + S3Object { + bucket = bucketName + name = docName + } + + val myDoc = + Document { + s3Object = s3ObjectOb + } + + val detectDocumentTextRequest = + DetectDocumentTextRequest { + document = myDoc + } + + TextractClient { region = "us-west-2" }.use { textractClient -> + val response = textractClient.detectDocumentText(detectDocumentTextRequest) + response.blocks?.forEach { block -> + println("The block type is ${block.blockType}") + } + + val documentMetadata = response.documentMetadata + if (documentMetadata != null) { + println("The number of pages in the document is ${documentMetadata.pages}") + } + } +} +// snippet-end:[textract.kotlin._detect_s3_text.main] diff --git a/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/StartDocumentAnalysis.kt b/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/StartDocumentAnalysis.kt index fefca73dc0c..a8a378cfd4f 100644 --- a/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/StartDocumentAnalysis.kt +++ b/kotlin/services/textract/src/main/kotlin/com/kotlin/textract/StartDocumentAnalysis.kt @@ -1,99 +1,107 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.textract - -// snippet-start:[textract.kotlin._start_doc_analysis.import] -import aws.sdk.kotlin.services.textract.TextractClient -import aws.sdk.kotlin.services.textract.model.DocumentLocation -import aws.sdk.kotlin.services.textract.model.FeatureType -import aws.sdk.kotlin.services.textract.model.GetDocumentAnalysisRequest -import aws.sdk.kotlin.services.textract.model.S3Object -import aws.sdk.kotlin.services.textract.model.StartDocumentAnalysisRequest -import kotlinx.coroutines.delay -import kotlin.system.exitProcess -// snippet-end:[textract.kotlin._start_doc_analysis.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ -suspend fun main(args: Array) { - - val usage = """ - Usage: - - - Where: - bucketName - The name of the Amazon S3 bucket that contains the document. - docName - The document name (must be an image, i.e., book.png). - """ - - if (args.size != 2) { - println(usage) - exitProcess(1) - } - - val bucketName = args[0] - val docName = args[1] - startDocAnalysisS3(bucketName, docName) -} - -// snippet-start:[textract.kotlin._start_doc_analysis.main] -suspend fun startDocAnalysisS3(bucketName: String?, docName: String?) { - - val myList = mutableListOf() - myList.add(FeatureType.Tables) - myList.add(FeatureType.Forms) - - val s3ObjectOb = S3Object { - bucket = bucketName - name = docName - } - - val location = DocumentLocation { - s3Object = s3ObjectOb - } - - val documentAnalysisRequest = StartDocumentAnalysisRequest { - documentLocation = location - featureTypes = myList - } - - TextractClient { region = "us-west-2" }.use { textractClient -> - val response = textractClient.startDocumentAnalysis(documentAnalysisRequest) - - // Get the job ID. - val jobId = response.jobId - val result = getJobResults(textractClient, jobId) - println("The status of the job is: $result") - } -} - -private suspend fun getJobResults(textractClient: TextractClient, jobIdVal: String?): String { - - var finished = false - var index = 0 - var status = "" - - while (!finished) { - - val analysisRequest = GetDocumentAnalysisRequest { - jobId = jobIdVal - maxResults = 1000 - } - val response = textractClient.getDocumentAnalysis(analysisRequest) - status = response.jobStatus.toString() - - if (status.compareTo("SUCCEEDED") == 0) finished = true else { - println("$index status is: $status") - delay(1000) - } - index ++ - } - return status -} -// snippet-end:[textract.kotlin._start_doc_analysis.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.textract + +// snippet-start:[textract.kotlin._start_doc_analysis.import] +import aws.sdk.kotlin.services.textract.TextractClient +import aws.sdk.kotlin.services.textract.model.DocumentLocation +import aws.sdk.kotlin.services.textract.model.FeatureType +import aws.sdk.kotlin.services.textract.model.GetDocumentAnalysisRequest +import aws.sdk.kotlin.services.textract.model.S3Object +import aws.sdk.kotlin.services.textract.model.StartDocumentAnalysisRequest +import kotlinx.coroutines.delay +import kotlin.system.exitProcess +// snippet-end:[textract.kotlin._start_doc_analysis.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ +suspend fun main(args: Array) { + val usage = """ + Usage: + + + Where: + bucketName - The name of the Amazon S3 bucket that contains the document. + docName - The document name (must be an image, i.e., book.png). + """ + + if (args.size != 2) { + println(usage) + exitProcess(1) + } + + val bucketName = args[0] + val docName = args[1] + startDocAnalysisS3(bucketName, docName) +} + +// snippet-start:[textract.kotlin._start_doc_analysis.main] +suspend fun startDocAnalysisS3( + bucketName: String?, + docName: String? +) { + val myList = mutableListOf() + myList.add(FeatureType.Tables) + myList.add(FeatureType.Forms) + + val s3ObjectOb = + S3Object { + bucket = bucketName + name = docName + } + + val location = + DocumentLocation { + s3Object = s3ObjectOb + } + + val documentAnalysisRequest = + StartDocumentAnalysisRequest { + documentLocation = location + featureTypes = myList + } + + TextractClient { region = "us-west-2" }.use { textractClient -> + val response = textractClient.startDocumentAnalysis(documentAnalysisRequest) + + // Get the job ID. + val jobId = response.jobId + val result = getJobResults(textractClient, jobId) + println("The status of the job is: $result") + } +} + +private suspend fun getJobResults( + textractClient: TextractClient, + jobIdVal: String?, +): String { + var finished = false + var index = 0 + var status = "" + + while (!finished) { + val analysisRequest = + GetDocumentAnalysisRequest { + jobId = jobIdVal + maxResults = 1000 + } + val response = textractClient.getDocumentAnalysis(analysisRequest) + status = response.jobStatus.toString() + + if (status.compareTo("SUCCEEDED") == 0) { + finished = true + } else { + println("$index status is: $status") + delay(1000) + } + index++ + } + return status +} +// snippet-end:[textract.kotlin._start_doc_analysis.main] diff --git a/kotlin/services/textract/src/test/kotlin/TextractTest.kt b/kotlin/services/textract/src/test/kotlin/TextractTest.kt index 1687672cfbe..6aea7cc73f9 100644 --- a/kotlin/services/textract/src/test/kotlin/TextractTest.kt +++ b/kotlin/services/textract/src/test/kotlin/TextractTest.kt @@ -26,14 +26,15 @@ class TextractTest { private var docName = "" @BeforeAll - fun setup() = runBlocking { - // Get the values to run these tests from AWS Secrets Manager. - val gson = Gson() - val json: String = getSecretValues() - val values = gson.fromJson(json, SecretValues::class.java) - sourceDoc = values.sourceDoc.toString() - bucketName = values.bucketName.toString() - docName = values.docName.toString() + fun setup() = + runBlocking { + // Get the values to run these tests from AWS Secrets Manager. + val gson = Gson() + val json: String = getSecretValues() + val values = gson.fromJson(json, SecretValues::class.java) + sourceDoc = values.sourceDoc.toString() + bucketName = values.bucketName.toString() + docName = values.docName.toString() /* val input: InputStream = this.javaClass.getClassLoader().getResourceAsStream("config.properties") @@ -46,44 +47,52 @@ class TextractTest { sourceDoc = prop.getProperty("sourceDoc") bucketName = prop.getProperty("bucketName") docName = prop.getProperty("docName") - */ - } + */ + } @Test @Order(1) - fun analyzeDocumentTest() = runBlocking { - analyzeDoc(sourceDoc) - println("Test 1 passed") - } + fun analyzeDocumentTest() = + runBlocking { + analyzeDoc(sourceDoc) + println("Test 1 passed") + } @Test @Order(2) - fun detectDocumentTextTest() = runBlocking { - detectDocText(sourceDoc) - println("Test 2 passed") - } + fun detectDocumentTextTest() = + runBlocking { + detectDocText(sourceDoc) + println("Test 2 passed") + } @Test @Order(3) - fun detectDocumentTextS3Test() = runBlocking { - detectDocTextS3(bucketName, docName) - println("Test 3 passed") - } + fun detectDocumentTextS3Test() = + runBlocking { + detectDocTextS3(bucketName, docName) + println("Test 3 passed") + } @Test @Order(4) - fun startDocumentAnalysisTest() = runBlocking { - startDocAnalysisS3(bucketName, docName) - println("Test 4 passed") - } + fun startDocumentAnalysisTest() = + runBlocking { + startDocAnalysisS3(bucketName, docName) + println("Test 4 passed") + } private suspend fun getSecretValues(): String { val secretName = "test/textract" - val valueRequest = GetSecretValueRequest { - secretId = secretName - } + val valueRequest = + GetSecretValueRequest { + secretId = secretName + } - SecretsManagerClient { region = "us-east-1"; credentialsProvider = EnvironmentCredentialsProvider() }.use { secretClient -> + SecretsManagerClient { + region = "us-east-1" + credentialsProvider = EnvironmentCredentialsProvider() + }.use { secretClient -> val valueResponse = secretClient.getSecretValue(valueRequest) return valueResponse.secretString.toString() }