Google Cloud Pub/Sub C++ Client  1.35.0
A C++ Client Library for Google Cloud Pub/Sub
Public Member Functions | List of all members
google::cloud::pubsub::SchemaAdminClient Class Reference

Performs schema admin operations in Cloud Pub/Sub. More...

#include <google/cloud/pubsub/schema_admin_client.h>

Public Member Functions

 SchemaAdminClient (std::shared_ptr< SchemaAdminConnection > connection)
 
 SchemaAdminClient ()=delete
 The default constructor is deleted. More...
 
StatusOr< google::pubsub::v1::Schema > CreateAvroSchema (Schema const &schema, std::string schema_definition)
 Creates a new Cloud Pub/Sub schema. More...
 
StatusOr< google::pubsub::v1::Schema > CreateProtobufSchema (Schema const &schema, std::string schema_definition)
 Creates a new Cloud Pub/Sub schema. More...
 
StatusOr< google::pubsub::v1::Schema > CreateSchema (google::pubsub::v1::CreateSchemaRequest const &request)
 Creates a new Cloud Pub/Sub schema. More...
 
StatusOr< google::pubsub::v1::Schema > GetSchema (Schema const &schema, google::pubsub::v1::SchemaView view=google::pubsub::v1::BASIC)
 Gets information about an existing Cloud Pub/Sub schema. More...
 
ListSchemasRange ListSchemas (std::string const &project_id, google::pubsub::v1::SchemaView view=google::pubsub::v1::BASIC)
 Lists all the schemas for a given project id. More...
 
Status DeleteSchema (Schema const &schema)
 Deletes an existing schema in Cloud Pub/Sub. More...
 
StatusOr< google::pubsub::v1::ValidateSchemaResponse > ValidateAvroSchema (std::string const &project_id, std::string schema_definition)
 Validates a schema definition. More...
 
StatusOr< google::pubsub::v1::ValidateSchemaResponse > ValidateProtobufSchema (std::string const &project_id, std::string schema_definition)
 Validates a schema definition. More...
 
StatusOr< google::pubsub::v1::ValidateSchemaResponse > ValidateSchema (std::string const &project_id, google::pubsub::v1::Schema schema)
 Validates a schema definition. More...
 
StatusOr< google::pubsub::v1::ValidateMessageResponse > ValidateMessageWithNamedSchema (google::pubsub::v1::Encoding encoding, std::string message, Schema const &named_schema)
 Validates a message against a schema. More...
 
StatusOr< google::pubsub::v1::ValidateMessageResponse > ValidateMessageWithAvro (google::pubsub::v1::Encoding encoding, std::string message, std::string project_id, std::string schema_definition)
 Validates a message against a schema. More...
 
StatusOr< google::pubsub::v1::ValidateMessageResponse > ValidateMessageWithProtobuf (google::pubsub::v1::Encoding encoding, std::string message, std::string project_id, std::string schema_definition)
 Validates a message against a schema. More...
 
StatusOr< google::pubsub::v1::ValidateMessageResponse > ValidateMessage (google::pubsub::v1::ValidateMessageRequest const &request)
 Validates a message against a schema. More...
 

Detailed Description

Performs schema admin operations in Cloud Pub/Sub.

Applications use this class to perform operations on Cloud Pub/Sub.

Warning
The Cloud Pub/Sub schema API and the C++ client library for the Cloud Pub/Sub schema APIs are experimental. They are subject to change, including complete removal, without notice.
Performance
SchemaAdminClient objects are cheap to create, copy, and move. However, each SchemaAdminClient object must be created with a std::shared_ptr<SchemaAdminConnection>, which itself is relatively expensive to create. Therefore, connection instances should be shared when possible. See the MakeSchemaAdminConnection() function and the SchemaAdminConnection interface for more details.
Thread Safety
Instances of this class created via copy-construction or copy-assignment share the underlying pool of connections. Access to these copies via multiple threads is guaranteed to work. Two threads operating on the same instance of this class is not guaranteed to work.
Error Handling
This class uses StatusOr<T> to report errors. When an operation fails to perform its work the returned StatusOr<T> contains the error details. If the ok() member function in the StatusOr<T> returns true then it contains the expected result. Please consult the StatusOr<T> documentation for more details.

Definition at line 62 of file schema_admin_client.h.

Constructor & Destructor Documentation

◆ SchemaAdminClient() [1/2]

google::cloud::pubsub::SchemaAdminClient::SchemaAdminClient ( std::shared_ptr< SchemaAdminConnection connection)
explicit

◆ SchemaAdminClient() [2/2]

google::cloud::pubsub::SchemaAdminClient::SchemaAdminClient ( )
delete

The default constructor is deleted.

Use PublisherClient(std::shared_ptr<PublisherConnection>)

Member Function Documentation

◆ CreateAvroSchema()

StatusOr<google::pubsub::v1::Schema> google::cloud::pubsub::SchemaAdminClient::CreateAvroSchema ( Schema const &  schema,
std::string  schema_definition 
)

Creates a new Cloud Pub/Sub schema.

Idempotency
This operation is idempotent, as it succeeds only once, therefore the library retries the call. It might return a status code ofkAlreadyExists as a consequence of retrying a successful (but reported as failed) request.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id,
std::string const& schema_id) {
auto constexpr kDefinition = R"js({
"type": "record",
"name": "State",
"namespace": "utilities",
"doc": "A list of states in the United States of America.",
"fields": [
{
"name": "name",
"type": "string",
"doc": "The common name of the state."
},
{
"name": "post_abbr",
"type": "string",
"doc": "The postal code abbreviation of the state."
}
]
})js";
auto schema = client.CreateAvroSchema(pubsub::Schema(project_id, schema_id),
kDefinition);
if (schema.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The schema already exists\n";
return;
}
if (!schema) throw std::runtime_error(schema.status().message());
std::cout << "Schema successfully created: " << schema->DebugString()
<< "\n";
}

◆ CreateProtobufSchema()

StatusOr<google::pubsub::v1::Schema> google::cloud::pubsub::SchemaAdminClient::CreateProtobufSchema ( Schema const &  schema,
std::string  schema_definition 
)

Creates a new Cloud Pub/Sub schema.

Idempotency
This operation is idempotent, as it succeeds only once, therefore the library retries the call. It might return a status code ofkAlreadyExists as a consequence of retrying a successful (but reported as failed) request.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id,
std::string const& schema_id) {
auto constexpr kDefinition = R"pfile(
syntax = "proto3";
package google.cloud.pubsub.samples;
message State {
string name = 1;
string post_abbr = 2;
}
)pfile";
auto schema = client.CreateProtobufSchema(
pubsub::Schema(project_id, schema_id), kDefinition);
if (schema.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The schema already exists\n";
return;
}
if (!schema) return; // TODO(#4792) - protobuf schema support in emulator
std::cout << "Schema successfully created: " << schema->DebugString()
<< "\n";
}

◆ CreateSchema()

StatusOr<google::pubsub::v1::Schema> google::cloud::pubsub::SchemaAdminClient::CreateSchema ( google::pubsub::v1::CreateSchemaRequest const &  request)

Creates a new Cloud Pub/Sub schema.

Idempotency
This operation is idempotent, as it succeeds only once, therefore the library retries the call. It might return a status code ofkAlreadyExists as a consequence of retrying a successful (but reported as failed) request.

◆ DeleteSchema()

Status google::cloud::pubsub::SchemaAdminClient::DeleteSchema ( Schema const &  schema)

Deletes an existing schema in Cloud Pub/Sub.

Idempotency
This operation is idempotent, the state of the system is the same after one or several calls, and therefore it is always retried. It might return a status code of kNotFound as a consequence of retrying a successful (but reported as failed) request.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id,
std::string const& schema_id) {
auto status = client.DeleteSchema(pubsub::Schema(project_id, schema_id));
// Note that kNotFound is a possible result when the library retries.
if (status.code() == google::cloud::StatusCode::kNotFound) {
std::cout << "The schema was not found\n";
return;
}
if (!status.ok()) throw std::runtime_error(status.message());
std::cout << "Schema successfully deleted\n";
}
Parameters
schemathe name of the schema to be deleted.

◆ GetSchema()

StatusOr<google::pubsub::v1::Schema> google::cloud::pubsub::SchemaAdminClient::GetSchema ( Schema const &  schema,
google::pubsub::v1::SchemaView  view = google::pubsub::v1::BASIC 
)

Gets information about an existing Cloud Pub/Sub schema.

Idempotency
This is a read-only operation and therefore always idempotent and retried.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id,
std::string const& schema_id) {
auto schema = client.GetSchema(pubsub::Schema(project_id, schema_id),
google::pubsub::v1::FULL);
if (!schema) throw std::runtime_error(schema.status().message());
std::cout << "The schema exists and its metadata is: "
<< schema->DebugString() << "\n";
}
Parameters
schemathe full name of the schema
viewUse BASIC to include the name and type of the schema, but not the definition. Use FULL to include the definition.

◆ ListSchemas()

ListSchemasRange google::cloud::pubsub::SchemaAdminClient::ListSchemas ( std::string const &  project_id,
google::pubsub::v1::SchemaView  view = google::pubsub::v1::BASIC 
)

Lists all the schemas for a given project id.

Idempotency
This is a read-only operation and therefore always idempotent and retried.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id) {
for (auto const& schema :
client.ListSchemas(project_id, google::pubsub::v1::FULL)) {
if (!schema) throw std::runtime_error(schema.status().message());
std::cout << "Schema: " << schema->DebugString() << "\n";
}
}
Parameters
project_idlists the schemas in this project
viewUse BASIC to include the name and type of each schema, but not the definition. Use FULL to include the definition.

◆ ValidateAvroSchema()

StatusOr<google::pubsub::v1::ValidateSchemaResponse> google::cloud::pubsub::SchemaAdminClient::ValidateAvroSchema ( std::string const &  project_id,
std::string  schema_definition 
)

Validates a schema definition.

Idempotency
This is a read-only operation and therefore always idempotent and retried.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id) {
auto constexpr kDefinition = R"js({
"type": "record",
"name": "State",
"namespace": "utilities",
"doc": "A list of states in the United States of America.",
"fields": [
{
"name": "name",
"type": "string",
"doc": "The common name of the state."
},
{
"name": "post_abbr",
"type": "string",
"doc": "The postal code abbreviation of the state."
}
]
})js";
auto schema = client.ValidateAvroSchema(project_id, kDefinition);
if (!schema) throw std::runtime_error(schema.status().message());
std::cout << "Schema is valid\n";
}

◆ ValidateMessage()

StatusOr<google::pubsub::v1::ValidateMessageResponse> google::cloud::pubsub::SchemaAdminClient::ValidateMessage ( google::pubsub::v1::ValidateMessageRequest const &  request)

Validates a message against a schema.

Idempotency
This is a read-only operation and therefore always idempotent and retried.

◆ ValidateMessageWithAvro()

StatusOr<google::pubsub::v1::ValidateMessageResponse> google::cloud::pubsub::SchemaAdminClient::ValidateMessageWithAvro ( google::pubsub::v1::Encoding  encoding,
std::string  message,
std::string  project_id,
std::string  schema_definition 
)

Validates a message against a schema.

Idempotency
This is a read-only operation and therefore always idempotent and retried.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id) {
auto constexpr kDefinition = R"js({
"type": "record",
"name": "State",
"namespace": "google.cloud.pubsub.samples",
"doc": "A list of states in the United States of America.",
"fields": [
{
"name": "name",
"type": "string",
"doc": "The common name of the state."
},
{
"name": "post_abbr",
"type": "string",
"doc": "The postal code abbreviation of the state."
}
]
})js";
auto constexpr kMessage = R"js({
"name": "New York",
"post_abbr": "NY"
})js";
auto schema = client.ValidateMessageWithAvro(
google::pubsub::v1::JSON, kMessage, project_id, kDefinition);
if (!schema) throw std::runtime_error(schema.status().message());
std::cout << "Message is valid\n";
}
Parameters
encodingthe message encoding, note that some schemas may not support some encodings.
messagethe message to validate
project_idthe project used to perform the validation
schema_definitionthe schema definition, in AVRO format

◆ ValidateMessageWithNamedSchema()

StatusOr<google::pubsub::v1::ValidateMessageResponse> google::cloud::pubsub::SchemaAdminClient::ValidateMessageWithNamedSchema ( google::pubsub::v1::Encoding  encoding,
std::string  message,
Schema const &  named_schema 
)

Validates a message against a schema.

Idempotency
This is a read-only operation and therefore always idempotent and retried.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id,
std::string const& schema_id) {
google::cloud::pubsub::samples::State data;
data.set_name("New York");
data.set_post_abbr("NY");
auto const message = data.SerializeAsString();
auto schema = client.ValidateMessageWithNamedSchema(
google::pubsub::v1::BINARY, message,
pubsub::Schema(project_id, schema_id));
if (!schema) return; // TODO(#4792) - protobuf schema support in emulator
std::cout << "Schema is valid\n";
}
Parameters
encodingthe message encoding, note that some schemas may not support some encodings.
messagethe message to validate
named_schemathe name of an existing schema to validate against

◆ ValidateMessageWithProtobuf()

StatusOr<google::pubsub::v1::ValidateMessageResponse> google::cloud::pubsub::SchemaAdminClient::ValidateMessageWithProtobuf ( google::pubsub::v1::Encoding  encoding,
std::string  message,
std::string  project_id,
std::string  schema_definition 
)

Validates a message against a schema.

Idempotency
This is a read-only operation and therefore always idempotent and retried.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id) {
google::cloud::pubsub::samples::State data;
data.set_name("New York");
data.set_post_abbr("NY");
auto const message = data.SerializeAsString();
auto constexpr kDefinition = R"pfile(
syntax = "proto3";
package google.cloud.pubsub.samples;
message State {
string name = 1;
string post_abbr = 2;
}
)pfile";
auto schema = client.ValidateMessageWithProtobuf(
google::pubsub::v1::BINARY, message, project_id, kDefinition);
if (!schema) return; // TODO(#4792) - protobuf schema support in emulator
std::cout << "Schema is valid\n";
}
Parameters
encodingthe message encoding, note that some schemas may not support some encodings.
messagethe message to validate
project_idthe project used to perform the validation
schema_definitionthe schema definition, in protocol buffers format

◆ ValidateProtobufSchema()

StatusOr<google::pubsub::v1::ValidateSchemaResponse> google::cloud::pubsub::SchemaAdminClient::ValidateProtobufSchema ( std::string const &  project_id,
std::string  schema_definition 
)

Validates a schema definition.

Idempotency
This is a read-only operation and therefore always idempotent and retried.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id) {
auto constexpr kDefinition = R"pfile(
syntax = "proto3";
package google.cloud.pubsub.samples;
message State {
string name = 1;
string post_abbr = 2;
}
)pfile";
auto schema = client.ValidateProtobufSchema(project_id, kDefinition);
if (!schema) return; // TODO(#4792) - protobuf schema support in emulator
std::cout << "Schema is valid\n";
}

◆ ValidateSchema()

StatusOr<google::pubsub::v1::ValidateSchemaResponse> google::cloud::pubsub::SchemaAdminClient::ValidateSchema ( std::string const &  project_id,
google::pubsub::v1::Schema  schema 
)

Validates a schema definition.

Idempotency
This is a read-only operation and therefore always idempotent and retried.