Skip to content

Commit

Permalink
Generate examples
Browse files Browse the repository at this point in the history
  • Loading branch information
coryan committed Oct 30, 2022
1 parent d8d1c3b commit b12ad79
Show file tree
Hide file tree
Showing 199 changed files with 6,816 additions and 593 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include "generator/integration_tests/golden/golden_kitchen_sink_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/example_driver.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// main-dox-marker: GoldenKitchenSinkClient
namespace {

void SetClientEndpoint(std::vector<std::string> const& argv) {
Expand All @@ -36,23 +40,51 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
"private.googleapis.com");
auto client = google::cloud::golden::GoldenKitchenSinkClient(
google::cloud::golden::MakeGoldenKitchenSinkConnection(options));
// Use the `client` object as usual
//! [set-client-endpoint]
}

// main-dox-marker: GoldenKitchenSinkClient
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
}
//! [with-service-account]
[](std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit); // Minimal error handling in examples
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return google::cloud::golden::GoldenKitchenSinkClient(
google::cloud::golden::MakeGoldenKitchenSinkConnection(options));
}
//! [with-service-account]
(argv.at(0));
}

void AutoRun(std::vector<std::string> const& argv) {
if (!argv.empty()) throw google::cloud::testing_util::Usage{"auto"};
namespace examples = ::google::cloud::testing_util;
using ::google::cloud::internal::GetEnv;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet({
"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE"
});
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();

std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
return example.Run(argc, argv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include "generator/integration_tests/golden/golden_thing_admin_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/example_driver.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// main-dox-marker: GoldenThingAdminClient
namespace {

void SetClientEndpoint(std::vector<std::string> const& argv) {
Expand All @@ -36,23 +40,51 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
"private.googleapis.com");
auto client = google::cloud::golden::GoldenThingAdminClient(
google::cloud::golden::MakeGoldenThingAdminConnection(options));
// Use the `client` object as usual
//! [set-client-endpoint]
}

// main-dox-marker: GoldenThingAdminClient
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
}
//! [with-service-account]
[](std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit); // Minimal error handling in examples
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return google::cloud::golden::GoldenThingAdminClient(
google::cloud::golden::MakeGoldenThingAdminConnection(options));
}
//! [with-service-account]
(argv.at(0));
}

void AutoRun(std::vector<std::string> const& argv) {
if (!argv.empty()) throw google::cloud::testing_util::Usage{"auto"};
namespace examples = ::google::cloud::testing_util;
using ::google::cloud::internal::GetEnv;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet({
"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE"
});
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();

std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
return example.Run(argc, argv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include "google/cloud/accessapproval/access_approval_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/example_driver.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// main-dox-marker: AccessApprovalClient
namespace {

void SetClientEndpoint(std::vector<std::string> const& argv) {
Expand All @@ -36,23 +40,50 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
"private.googleapis.com");
auto client = google::cloud::accessapproval::AccessApprovalClient(
google::cloud::accessapproval::MakeAccessApprovalConnection(options));
// Use the `client` object as usual
//! [set-client-endpoint]
}

// main-dox-marker: AccessApprovalClient
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
}
//! [with-service-account]
[](std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit); // Minimal error handling in examples
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return google::cloud::accessapproval::AccessApprovalClient(
google::cloud::accessapproval::MakeAccessApprovalConnection(options));
}
//! [with-service-account]
(argv.at(0));
}

void AutoRun(std::vector<std::string> const& argv) {
if (!argv.empty()) throw google::cloud::testing_util::Usage{"auto"};
namespace examples = ::google::cloud::testing_util;
using ::google::cloud::internal::GetEnv;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet(
{"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE"});
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();

std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
return example.Run(argc, argv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include "google/cloud/accesscontextmanager/access_context_manager_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/example_driver.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// main-dox-marker: AccessContextManagerClient
namespace {

void SetClientEndpoint(std::vector<std::string> const& argv) {
Expand All @@ -37,23 +41,51 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
auto client = google::cloud::accesscontextmanager::AccessContextManagerClient(
google::cloud::accesscontextmanager::MakeAccessContextManagerConnection(
options));
// Use the `client` object as usual
//! [set-client-endpoint]
}

// main-dox-marker: AccessContextManagerClient
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
}
//! [with-service-account]
[](std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit); // Minimal error handling in examples
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return google::cloud::accesscontextmanager::AccessContextManagerClient(
google::cloud::accesscontextmanager::MakeAccessContextManagerConnection(
options));
}
//! [with-service-account]
(argv.at(0));
}

void AutoRun(std::vector<std::string> const& argv) {
if (!argv.empty()) throw google::cloud::testing_util::Usage{"auto"};
namespace examples = ::google::cloud::testing_util;
using ::google::cloud::internal::GetEnv;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet(
{"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE"});
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();

std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
return example.Run(argc, argv);
Expand Down
37 changes: 34 additions & 3 deletions google/cloud/apigateway/samples/api_gateway_client_samples.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include "google/cloud/apigateway/api_gateway_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/example_driver.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// main-dox-marker: ApiGatewayServiceClient
namespace {

void SetClientEndpoint(std::vector<std::string> const& argv) {
Expand All @@ -36,23 +40,50 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
"private.googleapis.com");
auto client = google::cloud::apigateway::ApiGatewayServiceClient(
google::cloud::apigateway::MakeApiGatewayServiceConnection(options));
// Use the `client` object as usual
//! [set-client-endpoint]
}

// main-dox-marker: ApiGatewayServiceClient
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
}
//! [with-service-account]
[](std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit); // Minimal error handling in examples
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return google::cloud::apigateway::ApiGatewayServiceClient(
google::cloud::apigateway::MakeApiGatewayServiceConnection(options));
}
//! [with-service-account]
(argv.at(0));
}

void AutoRun(std::vector<std::string> const& argv) {
if (!argv.empty()) throw google::cloud::testing_util::Usage{"auto"};
namespace examples = ::google::cloud::testing_util;
using ::google::cloud::internal::GetEnv;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet(
{"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE"});
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();

std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
return example.Run(argc, argv);
Expand Down
Loading

0 comments on commit b12ad79

Please sign in to comment.