Skip to content

Commit

Permalink
chore: add dev docs examples for topics client, publish, subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua committed Sep 12, 2023
1 parent 231d750 commit 2fb5b10
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions examples/DocExampleApis/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public static async Task Main(string[] args)
AuthConfigurations.Default.Latest(),
new EnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN")
);
ITopicClient topicClient = new TopicClient(
TopicConfigurations.Laptop.latest(),
new EnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN")
);

await Example_API_CreateCache(client);
await Example_API_FlushCache(client);
Expand All @@ -29,6 +33,10 @@ public static async Task Main(string[] args)
await Example_API_Delete(client);

await Example_API_GenerateDisposableToken(authClient);

await Example_API_InstantiateTopicClient();
await Example_API_TopicSubscribe(topicClient);
await Example_API_TopicPublish(topicClient);
}

public static async Task Example_API_CreateCache(CacheClient cacheClient)
Expand Down Expand Up @@ -165,4 +173,65 @@ public static async Task Example_API_GenerateDisposableToken(IAuthClient authCli
Console.WriteLine("Error generating disposable token: " + err.Message);
}
}

public static async Task Example_API_InstantiateTopicClient()

Check warning on line 177 in examples/DocExampleApis/Program.cs

View workflow job for this annotation

GitHub Actions / build_examples

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
new TopicClient(
TopicConfigurations.Laptop.latest(),
new EnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN")
);
}
public static async Task Example_API_TopicPublish(ITopicClient topicClient)
{
var publishResponse =
await topicClient.PublishAsync("test-cache", "test-topic", "test-topic-value");
switch (publishResponse)
{
case TopicPublishResponse.Success:
Console.WriteLine("Successfully published message to 'test-topic'");
break;
case TopicPublishResponse.Error error:
throw new Exception($"An error occurred while publishing topic message: {error.ErrorCode}: {error}");
break;

Check warning on line 195 in examples/DocExampleApis/Program.cs

View workflow job for this annotation

GitHub Actions / build_examples

Unreachable code detected
}
}
public static async Task Example_API_TopicSubscribe(ITopicClient topicClient)
{
var produceCancellation = new CancellationTokenSource();
produceCancellation.CancelAfter(2000);

var subscribeResponse = await topicClient.SubscribeAsync("test-cache", "test-topic");
switch (subscribeResponse)
{
case TopicSubscribeResponse.Subscription subscription:
var cancellableSubscription = subscription.WithCancellation(produceCancellation.Token);

await Task.Delay(1_000);
await topicClient.PublishAsync("test-cache", "test-topic", "test-topic-value");
await Task.Delay(1_000);

await foreach (var message in cancellableSubscription)
{
switch (message)
{
case TopicMessage.Binary:
Console.WriteLine("Received unexpected binary message from topic.");
break;
case TopicMessage.Text text:
Console.WriteLine($"Received string message from topic: {text.Value}");
break;
case TopicMessage.Error error:
throw new Exception($"An error occurred while receiving topic message: {error.ErrorCode}: {error}");
break;

Check warning on line 225 in examples/DocExampleApis/Program.cs

View workflow job for this annotation

GitHub Actions / build_examples

Unreachable code detected
default:
throw new Exception("Bad message received");
}
}
subscription.Dispose();
break;
case TopicSubscribeResponse.Error error:
throw new Exception($"An error occurred subscribing to a topic: {error.ErrorCode}: {error}");
break;

Check warning on line 234 in examples/DocExampleApis/Program.cs

View workflow job for this annotation

GitHub Actions / build_examples

Unreachable code detected
}
}
}

0 comments on commit 2fb5b10

Please sign in to comment.