Skip to content

Commit

Permalink
feat(DocumentAI): Adds Quickstart
Browse files Browse the repository at this point in the history
  • Loading branch information
holtskinner authored Feb 1, 2023
1 parent f0aed2c commit fb8a67e
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ applications/liftAndShiftPetshop/PrecompiledWeb/
applications/liftAndShiftPetshop/Web/
*.leu
.cr/
.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JUnitTestLogger" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DocumentAI.Samples\DocumentAI.Samples.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\Invoice.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
58 changes: 58 additions & 0 deletions documentai/api/DocumentAI.Samples.Tests/DocumentAIFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


using Google.Api.Gax.ResourceNames;
using Google.Cloud.DocumentAI.V1;
using Google.Protobuf.WellKnownTypes;
using System;
using System.Threading;
using Xunit;

[CollectionDefinition(nameof(DocumentAIFixture))]
public class DocumentAIFixture : ICollectionFixture<DocumentAIFixture>
{
public string ProjectId { get; }
public string LocationId { get; }

public string ProcessorId { get; }
public ProcessorName ProcessorName { get; }

public string LocalPath { get; }

public string MimeType { get; }

public DocumentAIFixture()
{
ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
if (string.IsNullOrEmpty(ProjectId))
{
throw new Exception("Missing GOOGLE_PROJECT_ID environment variable.");
}

LocationId = "us";

ProcessorId = Environment.GetEnvironmentVariable("DOCUMENT_AI_PROCESSOR_ID");
if (string.IsNullOrEmpty(ProcessorId))
{
throw new Exception("Missing DOCUMENT_AI_PROCESSOR_ID environment variable.");
}
ProcessorName = ProcessorName.FromProjectLocationProcessor(ProjectId, LocationId, ProcessorId);

LocalPath = "Resources/Invoice.pdf";
MimeType = "application/pdf";
}
}
38 changes: 38 additions & 0 deletions documentai/api/DocumentAI.Samples.Tests/QuickstartTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Xunit;

[Collection(nameof(DocumentAIFixture))]
public class QuickstartTest
{
private readonly DocumentAIFixture _fixture;
private readonly QuickstartSample _sample;

public QuickstartTest(DocumentAIFixture fixture)
{
_fixture = fixture;
_sample = new QuickstartSample();
}

[Fact]
public void Runs()
{
// Run the sample code.
var document = _sample.Quickstart(projectId: _fixture.ProjectId, locationId: _fixture.LocationId, processorId: _fixture.ProcessorId, localPath: _fixture.LocalPath, mimeType: _fixture.MimeType);
Assert.Contains("Invoice", document.Text);
}
}
Binary file not shown.
9 changes: 9 additions & 0 deletions documentai/api/DocumentAI.Samples/DocumentAI.Samples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Cloud.DocumentAI.V1" Version="3.3.0" />
</ItemGroup>
</Project>
66 changes: 66 additions & 0 deletions documentai/api/DocumentAI.Samples/Quickstart.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START documentai_process_document]
// [START documentai_quickstart]

using Google.Cloud.DocumentAI.V1;
using Google.Protobuf;
using System;
using System.IO;

public class QuickstartSample
{
public Document Quickstart(
string projectId = "your-project-id",
string locationId = "your-processor-location",
string processorId = "your-processor-id",
string localPath = "my-local-path/my-file-name",
string mimeType = "application/pdf"
)
{
// Create client
var client = new DocumentProcessorServiceClientBuilder
{
Endpoint = $"{locationId}-documentai.googleapis.com"
}.Build();

// Read in local file
using var fileStream = File.OpenRead(localPath);
var rawDocument = new RawDocument
{
Content = ByteString.FromStream(fileStream),
MimeType = mimeType
};

// Initialize request argument(s)
var request = new ProcessRequest
{
Name = ProcessorName.FromProjectLocationProcessor(projectId, locationId, processorId).ToString(),
RawDocument = rawDocument
};

// Make the request
var response = client.ProcessDocument(request);

var document = response.Document;
Console.WriteLine(document.Text);
return document;
}
}

// [END documentai_quickstart]
// [END documentai_process_document]
64 changes: 64 additions & 0 deletions documentai/api/DocumentAI.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DocumentAI.Samples", "DocumentAI.Samples\DocumentAI.Samples.csproj", "{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DocumentAI.Samples.Tests", "DocumentAI.Samples.Tests\DocumentAI.Samples.Tests.csproj", "{D9264292-F54B-4EA8-B393-DDD49A20FE5A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "testutil", "..\..\testutil\testutil.csproj", "{785E4F97-D1CD-41EB-8E48-D329C628516F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Debug|x64.ActiveCfg = Debug|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Debug|x64.Build.0 = Debug|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Debug|x86.ActiveCfg = Debug|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Debug|x86.Build.0 = Debug|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Release|Any CPU.Build.0 = Release|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Release|x64.ActiveCfg = Release|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Release|x64.Build.0 = Release|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Release|x86.ActiveCfg = Release|Any CPU
{3CBCFC9F-AB47-46A3-AF5F-B92DC5E939F0}.Release|x86.Build.0 = Release|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Debug|x64.ActiveCfg = Debug|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Debug|x64.Build.0 = Debug|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Debug|x86.ActiveCfg = Debug|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Debug|x86.Build.0 = Debug|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Release|Any CPU.Build.0 = Release|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Release|x64.ActiveCfg = Release|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Release|x64.Build.0 = Release|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Release|x86.ActiveCfg = Release|Any CPU
{D9264292-F54B-4EA8-B393-DDD49A20FE5A}.Release|x86.Build.0 = Release|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Debug|x64.ActiveCfg = Debug|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Debug|x64.Build.0 = Debug|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Debug|x86.ActiveCfg = Debug|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Debug|x86.Build.0 = Debug|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Release|Any CPU.Build.0 = Release|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Release|x64.ActiveCfg = Release|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Release|x64.Build.0 = Release|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Release|x86.ActiveCfg = Release|Any CPU
{785E4F97-D1CD-41EB-8E48-D329C628516F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3539D717-814A-4D4A-957E-4FE214B97B36}
EndGlobalSection
EndGlobal
35 changes: 35 additions & 0 deletions documentai/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Google Cloud Document AI Sample

These samples demonstrate how to interact with the [Google Cloud Document AI API][Documentai] using C# and
the .NET client libraries to call the Cloud Document AI API.

The samples require [.NET 6][net-6] or later. That means using

[Visual Studio 2022](https://www.visualstudio.com/), or the command line.


## Setup

1. Set up a [.NET development environment](https://cloud.google.com/dotnet/docs/setup).

4. Enable APIs for your project.
[Click here][enable-api] to visit Cloud Platform Console and enable the Google Cloud Document AI API.


## Contributing changes

* See [CONTRIBUTING.md](../../CONTRIBUTING.md)

## Licensing

* See [LICENSE](../../LICENSE)

## Testing

* See [TESTING.md](../../TESTING.md)


[Documentai]: https://cloud.google.com/documentai/docs
[enable-api]: https://console.cloud.google.com/flows/enableapi?apiid=documentai.googleapis.com&showconfirmation=true
[net-6]: https://dotnet.microsoft.com/download/dotnet/6.0

16 changes: 16 additions & 0 deletions documentai/api/runTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

dotnet restore --force
dotnet test --no-restore --test-adapter-path:. --logger:junit 2>&1 | %{ "$_" }

0 comments on commit fb8a67e

Please sign in to comment.