createTopic method

Future<Topic> createTopic({
  1. Map<String, String>? labels,
  2. MessageStoragePolicy? messageStoragePolicy,
  3. String? kmsKeyName,
  4. int retries = 5,
  5. SchemaSettings? schemaSettings,
  6. Duration? messageRetentionDuration,
  7. required String topic,
})

Creates a topic with the given topic. The topic can either be the simple name of the topic or it may be the fully quantified name in the projects/{project}/topics/{topic} format.

If set, the kmsKeyName represents The resource name of the Cloud KMS CryptoKey to be used to protect access to messages published on this topic. The expected format is projects/*/locations/*/keyRings/*/cryptoKeys/*.

Implementation

Future<Topic> createTopic({
  Map<String, String>? labels,
  MessageStoragePolicy? messageStoragePolicy,
  String? kmsKeyName,
  int retries = 5,
  SchemaSettings? schemaSettings,
  Duration? messageRetentionDuration,
  required String topic,
}) async {
  assert(_initialized);
  _logger.fine('[createTopic]: start -- [$topic]');
  try {
    return await _execute(
      executor: () async {
        final request = Topic(
          labels: labels,
          messageStoragePolicy: messageStoragePolicy,
          kmsKeyName: kmsKeyName,
          schemaSettings: schemaSettings,
          messageRetentionDuration: messageRetentionDuration == null
              ? null
              : '${messageRetentionDuration.inSeconds}s',
        );

        final result = await _pubsubApi.projects.topics.create(
          request,
          topic.startsWith('projects/')
              ? topic
              : 'projects/$_projectId/topics/$topic',
        );

        return result;
      },
      retries: retries,
    );
  } finally {
    _logger.fine('[createTopic]: complete -- [$topic]');
  }
}