Skip to content

Commit 39ef319

Browse files
authored
fix(documentai): make quickstart functional (#9108)
1 parent a75c8d8 commit 39ef319

File tree

7 files changed

+33
-25
lines changed

7 files changed

+33
-25
lines changed

ci/etc/integration-tests-config.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ $env:GOOGLE_CLOUD_CPP_PUBSUB_TEST_QUICKSTART_TOPIC="quickstart"
6969
# Cloud BigQuery configuration parameters
7070
$env:GOOGLE_CLOUD_CPP_BIGQUERY_TEST_QUICKSTART_TABLE="projects/bigquery-public-data/datasets/usa_names/tables/usa_1910_current"
7171

72+
# Document AI
73+
$env:GOOGLE_CLOUD_CPP_DOCUMENTAI_TEST_LOCATION_ID="us"
74+
$env:GOOGLE_CLOUD_CPP_DOCUMENTAI_TEST_PROCESSOR_ID="3cb572567f9df97f"
75+
$env:GOOGLE_CLOUD_CPP_DOCUMENTAI_TEST_FILENAME="${env:PROJECT_ROOT}/google/cloud/documentai/quickstart/resources/invoice.pdf"
76+
7277
# Cloud IAM configuration parameters
7378
$env:GOOGLE_CLOUD_CPP_IAM_CREDENTIALS_TEST_SERVICE_ACCOUNT="iam-credentials-test-sa@${env:GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com"
7479
$env:GOOGLE_CLOUD_CPP_IAM_TEST_SERVICE_ACCOUNT="iam-test-sa@${env:GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com"

ci/etc/integration-tests-config.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ export GOOGLE_CLOUD_CPP_PUBSUB_TEST_QUICKSTART_TOPIC="quickstart"
8585
# Cloud BigQuery configuration parameters
8686
export GOOGLE_CLOUD_CPP_BIGQUERY_TEST_QUICKSTART_TABLE="projects/bigquery-public-data/datasets/usa_names/tables/usa_1910_current"
8787

88+
# Document AI
89+
export GOOGLE_CLOUD_CPP_DOCUMENTAI_TEST_LOCATION_ID="us"
90+
export GOOGLE_CLOUD_CPP_DOCUMENTAI_TEST_PROCESSOR_ID="3cb572567f9df97f"
91+
export GOOGLE_CLOUD_CPP_DOCUMENTAI_TEST_FILENAME="${PROJECT_ROOT}/google/cloud/documentai/quickstart/resources/invoice.pdf"
92+
8893
# Cloud IAM configuration parameters
8994
export GOOGLE_CLOUD_CPP_IAM_CREDENTIALS_TEST_SERVICE_ACCOUNT="iam-credentials-test-sa@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com"
9095
export GOOGLE_CLOUD_CPP_IAM_TEST_SERVICE_ACCOUNT="iam-test-sa@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com"

google/cloud/documentai/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ if (BUILD_TESTING)
114114
COMMAND
115115
cmake -P "${PROJECT_SOURCE_DIR}/cmake/quickstart-runner.cmake"
116116
$<TARGET_FILE:documentai_quickstart> GOOGLE_CLOUD_PROJECT
117-
# TODO(#7976): Enable this quickstart and make it run.
118-
)
119-
set_tests_properties(
120-
documentai_quickstart PROPERTIES DISABLED true LABELS
121-
"integration-test;quickstart")
117+
GOOGLE_CLOUD_CPP_DOCUMENTAI_TEST_LOCATION_ID
118+
GOOGLE_CLOUD_CPP_DOCUMENTAI_TEST_PROCESSOR_ID
119+
GOOGLE_CLOUD_CPP_DOCUMENTAI_TEST_FILENAME)
120+
set_tests_properties(documentai_quickstart
121+
PROPERTIES LABELS "integration-test;quickstart")
122122
endif ()
123123

124124
# Get the destination directories based on the GNU recommendations.

google/cloud/documentai/README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
# Cloud Document AI API C++ Client Library
22

3-
:warning: This library will not be declared GA until the quickstart is operable.
4-
53
This directory contains an idiomatic C++ client library for the
64
[Cloud Document AI API][cloud-service], a service that uses machine
75
learning on a scalable cloud-based platform to help your organization
86
efficiently scan, analyze, and understand documents.
97

10-
This library is **experimental**. Its APIs are subject to change without notice.
11-
128
While this library is **GA**, please note that the Google Cloud C++ client libraries do **not** follow
139
[Semantic Versioning](https://semver.org/).
1410

@@ -43,13 +39,14 @@ this library.
4339
```cc
4440
#include "google/cloud/documentai/document_processor_client.h"
4541
#include "google/cloud/common_options.h"
42+
#include <fstream>
4643
#include <iostream>
4744
#include <stdexcept>
4845

4946
int main(int argc, char* argv[]) try {
50-
if (argc != 4) {
47+
if (argc != 5) {
5148
std::cerr << "Usage: " << argv[0]
52-
<< " project-id location-id processor-id\n";
49+
<< " project-id location-id processor-id filename (PDF only)\n";
5350
return 1;
5451
}
5552
std::string const location = argv[2];
@@ -59,11 +56,11 @@ int main(int argc, char* argv[]) try {
5956
}
6057

6158
namespace gc = ::google::cloud;
59+
namespace documentai = ::google::cloud::documentai;
60+
// The Document AI service requires using an endpoint matching the processor's
61+
// location.
6262
auto options = gc::Options{}.set<gc::EndpointOption>(
6363
location + "-documentai.googleapis.com");
64-
options.set<google::cloud::TracingComponentsOption>({"rpc"});
65-
66-
namespace documentai = ::google::cloud::documentai;
6764
auto client = documentai::DocumentProcessorServiceClient(
6865
documentai::MakeDocumentProcessorServiceConnection(options));
6966

@@ -73,9 +70,10 @@ int main(int argc, char* argv[]) try {
7370
google::cloud::documentai::v1::ProcessRequest req;
7471
req.set_name(resource);
7572
req.set_skip_human_review(true);
76-
auto& doc = *req.mutable_inline_document();
73+
auto& doc = *req.mutable_raw_document();
7774
doc.set_mime_type("application/pdf");
78-
doc.set_uri("gs://cloud-samples-data/documentai/invoice.pdf");
75+
std::ifstream is(argv[4]);
76+
doc.set_content(std::string{std::istreambuf_iterator<char>(is), {}});
7977

8078
auto resp = client.ProcessDocument(std::move(req));
8179
if (!resp) throw std::runtime_error(resp.status().message());

google/cloud/documentai/quickstart/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# HOWTO: using the Cloud Document AI API C++ client in your project
22

3-
:warning: This quickstart is not yet operable.
4-
53
This directory contains small examples showing how to use the Cloud Document AI API C++
64
client library in your own project. These instructions assume that you have
75
some experience as a C++ developer and that you have a working C++ toolchain

google/cloud/documentai/quickstart/quickstart.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414

1515
#include "google/cloud/documentai/document_processor_client.h"
1616
#include "google/cloud/common_options.h"
17+
#include <fstream>
1718
#include <iostream>
1819
#include <stdexcept>
1920

2021
int main(int argc, char* argv[]) try {
21-
if (argc != 4) {
22+
if (argc != 5) {
2223
std::cerr << "Usage: " << argv[0]
23-
<< " project-id location-id processor-id\n";
24+
<< " project-id location-id processor-id filename (PDF only)\n";
2425
return 1;
2526
}
2627
std::string const location = argv[2];
@@ -30,11 +31,11 @@ int main(int argc, char* argv[]) try {
3031
}
3132

3233
namespace gc = ::google::cloud;
34+
namespace documentai = ::google::cloud::documentai;
35+
// The Document AI service requires using an endpoint matching the processor's
36+
// location.
3337
auto options = gc::Options{}.set<gc::EndpointOption>(
3438
location + "-documentai.googleapis.com");
35-
options.set<google::cloud::TracingComponentsOption>({"rpc"});
36-
37-
namespace documentai = ::google::cloud::documentai;
3839
auto client = documentai::DocumentProcessorServiceClient(
3940
documentai::MakeDocumentProcessorServiceConnection(options));
4041

@@ -44,9 +45,10 @@ int main(int argc, char* argv[]) try {
4445
google::cloud::documentai::v1::ProcessRequest req;
4546
req.set_name(resource);
4647
req.set_skip_human_review(true);
47-
auto& doc = *req.mutable_inline_document();
48+
auto& doc = *req.mutable_raw_document();
4849
doc.set_mime_type("application/pdf");
49-
doc.set_uri("gs://cloud-samples-data/documentai/invoice.pdf");
50+
std::ifstream is(argv[4]);
51+
doc.set_content(std::string{std::istreambuf_iterator<char>(is), {}});
5052

5153
auto resp = client.ProcessDocument(std::move(req));
5254
if (!resp) throw std::runtime_error(resp.status().message());
57.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)