Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Rdo 6 1 liner sdk examples #185

Closed
wants to merge 12 commits into from
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ using Memphis.Client;

// Connecting to the broker
var options = MemphisClientFactory.GetDefaultOptions();
options.Host = "aws-us-east-1.cloud.memphis.dev";
options.AccountId = int.Parse(Environment.GetEnvironmentVariable("memphis_account_id"));
options.Username = "test_user";
options.Password = Environment.GetEnvironmentVariable("memphis_pass");
options.Host = "<memphis-host>";
options.AccountId = <memphis-accountid>;
options.Username = "<memphis-username>";
options.Password = "<memphis-password>";

var memphisClient = await MemphisClientFactory.CreateClient(options);
```
Expand All @@ -67,8 +67,8 @@ var headers = new NameValueCollection();
var msgBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message));
await memphisClient.ProduceAsync(new Memphis.Client.Producer.MemphisProducerOptions
{
StationName = "test_station",
ProducerName = "producer"
StationName = "<station-name>",
ProducerName = "<producer-name>"
},
msgBytes,
headers);
Expand All @@ -77,15 +77,17 @@ public class Message
{
public string Hello { get; set; }
}

memphisClient.Dispose();
```

Lastly, to consume this message, call the `memphisClient.FetchMessages` function or create a consumer and call its `consumer.Fetch` function:

```C#
var messages = await memphisClient.FetchMessages(new Memphis.Client.Consumer.FetchMessageOptions
{
StationName = "test_station",
ConsumerName = "consumer",
StationName = "<station-name>",
ConsumerName = "<consumer-name>",
Prefetch = false
});

Expand All @@ -98,6 +100,8 @@ foreach (MemphisMessage message in messages)

message.Ack();
}

memphisClient.Dispose();
```

> Remember to call `memphisClient.Dispose()` to close the connection!
Expand Down
45 changes: 26 additions & 19 deletions examples/Consumer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,52 @@
using Memphis.Client.Core;
using System.Text.Json;

MemphisClient? memphisClient = null;

try
{
var options = MemphisClientFactory.GetDefaultOptions();
options.Host = "aws-us-east-1.cloud.memphis.dev";
options.AccountId = int.Parse(Environment.GetEnvironmentVariable("memphis_account_id"));
options.Username = "test_user";
options.Password = Environment.GetEnvironmentVariable("memphis_pass");
options.Host = "<memphis-host>";
// options.AccountId = "<memphis-accountId>";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it in a comment? maybe needs another comment - for cloud users

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shay23b because it errors if I don't put it in a comment. I don't want to put it in a string because it's not a string (it's an int), but I also can't leave it uncommented because it will error.

I looked at the old examples and this was how this was treated to get past the linter complaining about the quotes

options.Username = "<memphis-username>";
options.Password = "<memphis-password>";

var memphisClient = await MemphisClientFactory.CreateClient(options);
memphisClient = await MemphisClientFactory.CreateClient(options);

var consumer = await memphisClient.CreateConsumer(
new Memphis.Client.Consumer.MemphisConsumerOptions
{
StationName = "test_station",
ConsumerName = "consumer"
StationName = "<station-name>",
ConsumerName = "<consumer-name>"
});

var messages = consumer.Fetch(3, false);

foreach (MemphisMessage message in messages)
{
var messageData = message.GetData();
var messageOBJ = JsonSerializer.Deserialize<Message>(messageData);
while (true) {
var messages = consumer.Fetch(3, false);

// Do something with the message here
Console.WriteLine(JsonSerializer.Serialize(messageOBJ));
if (!messages.Any())
{
continue;
}

message.Ack();
}
foreach (MemphisMessage message in messages)
{
var messageData = message.GetData();
var messageOBJ = JsonSerializer.Deserialize<Message>(messageData);

memphisClient.Dispose();
// Do something with the message here
Console.WriteLine(JsonSerializer.Serialize(messageOBJ));

message.Ack();
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
memphisClient?.Dispose();
}

public class Message
{
public string Hello { get; set; }
public string? Hello { get; set; }
}
20 changes: 12 additions & 8 deletions examples/Producer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
using System.Text;
using System.Text.Json;

MemphisClient? memphisClient = null;

try
{
var options = MemphisClientFactory.GetDefaultOptions();
options.Host = "aws-us-east-1.cloud.memphis.dev";
options.AccountId = int.Parse(Environment.GetEnvironmentVariable("memphis_account_id"));
options.Username = "test_user";
options.Password = Environment.GetEnvironmentVariable("memphis_pass");

options.Host = "<memphis-host>";
// options.AccountId = <memphis-accountId>;
John-Memphis marked this conversation as resolved.
Show resolved Hide resolved
options.Username = "<memphis-username>";
options.Password = "<memphis-password>";

var memphisClient = await MemphisClientFactory.CreateClient(options);
memphisClient = await MemphisClientFactory.CreateClient(options);

var producer = await memphisClient.CreateProducer(
new Memphis.Client.Producer.MemphisProducerOptions
{
StationName = "test_station",
ProducerName = "producer"
StationName = "<station-name>",
ProducerName = "<producer-name>"
});

Message message = new()
Expand All @@ -41,9 +44,10 @@ await producer.ProduceAsync(
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
memphisClient?.Dispose();
}

public class Message
{
public string Hello { get; set; }
public string? Hello { get; set; }
}
2 changes: 1 addition & 1 deletion src/Memphis.Client/Exception/MemphisExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class MemphisExceptions{

public static readonly MemphisException InvalidSchemaNameException = new("Only alphanumeric and the '_', '-', '.' characters are allowed in schema name");

public static readonly MemphisException InvalidSchemaStartEndCharsException = new("Schema name can not start or end with non alphanumeric character");
public static readonly MemphisException InvalidSchemaStartEndCharsException = new("Schema name can not start or end with non-alphanumeric character");

public static readonly MemphisException EmptySchemaTypeException = new("Schema type can not be empty");

Expand Down