|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +title: Google Pub/Sub bindings |
| 4 | +description: Read and write Google Pub/Sub channel and message bindings. |
| 5 | +--- |
| 6 | + |
| 7 | +# Google Pub/Sub |
| 8 | + |
| 9 | +`BindingsCollection.GooglePubSub` registers channel and message levels; it is also part of `All`. |
| 10 | + |
| 11 | +## Write |
| 12 | + |
| 13 | +Attach channel-level settings to an `AsyncApiChannel`: |
| 14 | + |
| 15 | +```csharp |
| 16 | +using System.Collections.Generic; |
| 17 | +using ByteBard.AsyncAPI.Bindings.GooglePubSub; |
| 18 | +using ByteBard.AsyncAPI.Models; |
| 19 | + |
| 20 | +var channel = new AsyncApiChannel(); |
| 21 | +channel.Bindings.Add(new GooglePubSubChannelBinding |
| 22 | +{ |
| 23 | + Labels = new Dictionary<string, string> { ["team"] = "fulfillment" }, |
| 24 | + MessageRetentionDuration = "86400s", |
| 25 | + MessageStoragePolicy = new MessageStoragePolicy |
| 26 | + { |
| 27 | + AllowedPersistenceRegions = new[] { "us-central1", "us-east1" }, |
| 28 | + }, |
| 29 | + SchemaSettings = new SchemaSettings |
| 30 | + { |
| 31 | + Encoding = "JSON", |
| 32 | + FirstRevisionId = "1", |
| 33 | + LastRevisionId = "3", |
| 34 | + Name = "projects/example/schemas/orders", |
| 35 | + }, |
| 36 | +}); |
| 37 | + |
| 38 | +var message = new AsyncApiMessage(); |
| 39 | +message.Bindings.Add(new GooglePubSubMessageBinding |
| 40 | +{ |
| 41 | + Attributes = new Dictionary<string, string> { ["source"] = "billing" }, |
| 42 | + OrderingKey = "customer-7", |
| 43 | + Schema = new MessageSchema { Name = "projects/example/schemas/orders" }, |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +## Read and use |
| 48 | + |
| 49 | +Register the channel and message parsers, parse a channel fragment, and access a typed field from the channel's `Bindings` dictionary: |
| 50 | + |
| 51 | +```csharp |
| 52 | +using ByteBard.AsyncAPI; |
| 53 | +using ByteBard.AsyncAPI.Bindings; |
| 54 | +using ByteBard.AsyncAPI.Bindings.GooglePubSub; |
| 55 | +using ByteBard.AsyncAPI.Models; |
| 56 | +using ByteBard.AsyncAPI.Readers; |
| 57 | + |
| 58 | +var settings = new AsyncApiReaderSettings { Bindings = BindingsCollection.GooglePubSub }; |
| 59 | +var parsed = new AsyncApiStringReader(settings).ReadFragment<AsyncApiChannel>(""" |
| 60 | + bindings: |
| 61 | + googlepubsub: |
| 62 | + labels: |
| 63 | + team: fulfillment |
| 64 | + messageRetentionDuration: 86400s |
| 65 | + messageStoragePolicy: |
| 66 | + allowedPersistenceRegions: |
| 67 | + - us-central1 |
| 68 | + - us-east1 |
| 69 | + schemaSettings: |
| 70 | + encoding: JSON |
| 71 | + firstRevisionId: '1' |
| 72 | + lastRevisionId: '3' |
| 73 | + name: projects/example/schemas/orders |
| 74 | + """, AsyncApiVersion.AsyncApi3_0, out var diagnostics); |
| 75 | + |
| 76 | +if (parsed.Bindings.TryGetValue<GooglePubSubChannelBinding>(out var pubSub)) |
| 77 | +{ |
| 78 | + Console.WriteLine(pubSub.Labels["team"]); |
| 79 | + foreach (var region in pubSub.MessageStoragePolicy.AllowedPersistenceRegions) |
| 80 | + { |
| 81 | + Console.WriteLine(region); |
| 82 | + } |
| 83 | + Console.WriteLine(pubSub.SchemaSettings.Name); |
| 84 | +} |
| 85 | + |
| 86 | +var parsedMessage = new AsyncApiStringReader(settings).ReadFragment<AsyncApiMessage>(""" |
| 87 | + bindings: |
| 88 | + googlepubsub: |
| 89 | + schema: |
| 90 | + name: projects/example/schemas/orders |
| 91 | + orderingKey: customer-7 |
| 92 | + """, AsyncApiVersion.AsyncApi3_0, out diagnostics); |
| 93 | +if (parsedMessage.Bindings.TryGetValue<GooglePubSubMessageBinding>(out var pubSubMessage)) |
| 94 | +{ |
| 95 | + Console.WriteLine(pubSubMessage.Schema.Name); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +Channel fields: `Labels` (`Dictionary<string,string>`), `MessageRetentionDuration` (`string`), `MessageStoragePolicy` (`AllowedPersistenceRegions`, a sequence of strings), and `SchemaSettings` (`Encoding`, `FirstRevisionId`, `LastRevisionId`, and `Name`, all strings). Message fields: `Attributes` (`Dictionary<string,string>`), `OrderingKey`, and `Schema` (`MessageSchema`, whose `Name` is a string). This message `Schema` is Pub/Sub schema metadata, not an `AsyncApiJsonSchema` payload schema. |
| 100 | + |
| 101 | +For message-level writes, attach a `GooglePubSubMessageBinding` with `message.Bindings.Add(new GooglePubSubMessageBinding { ... });`; it provides `Attributes`, `OrderingKey`, and `Schema`. Duration text and nested schema/storage settings are retained as model data; the library does not verify them against a live topic. |
0 commit comments