Skip to content

Latest commit

 

History

History
121 lines (89 loc) · 3.97 KB

README.md

File metadata and controls

121 lines (89 loc) · 3.97 KB

YaCloudKit

A set of tools for working with Yandex.Cloud services.

Main Build Nuget Nuget Nuget Nuget

Description

YaCloudKit is a set of tools that enables your application to interact with Yandex.Cloud platform services.

Supports .Net 8.0 and higher.

Tools

This section lists the tools that are currently implemented.

Implemented:

  • YaCloudKit.MQ - a client for working with Yandex.Cloud Message Queue.
  • YaCloudKit.MQ.Transport - an extension for the message queue client, responsible for message serialization/deserialization and processing.
  • YaCloudKit.TTS - a SpeechKit client for text-to-speech synthesis.

YaCloudKit.MQ

YaCloudKit.MQ is a client for working with Yandex.Cloud Message Queue. It is fully tailored for the Yandex Message Queue service without unnecessary dependencies or code.

Usage

The client can be easily connected and used in three simple steps:

// Step 1. create client instance
var client = new YandexMqClient("[Access Key Id]", "[Secret Access Key]");

// Step 2. Create message
var message = new SendMessageRequest()
    .SetQueueUrl("[Queue url]")
    .SetMessageBody("Message test example");

// Step 3. Send message
await client.SendMessageAsync(message);

More examples can be found here

YaCloudKit.MQ.Transport

YaCloudKit.MQ.Transport is a set of services and components designed to simplify message processing from the message queue.

Usage

Create a handler for your message:

public class CustomerRegistrationHandler : IMessageHandler<CustomerRegistrationDto>
{
    private readonly ILogger<CustomerRegistrationHandler> _logger;
    
    public CustomerRegistrationHandler(ILogger<CustomerRegistrationHandler> logger)
    {
        // You can use dependency injection
        _logger = logger;
    }

    public async Task HandleAsync(CustomerRegistrationDto message, CancellationToken cancellationToken)
    {
        // do work
    }
}

Configure dependencies:

services.AddYandexMqClient("[Access Key Id]", "[Secret Access Key]");

services.AddMqTransport(configure =>
    {
        configure
            .WithMessageConverter(new JsonMessageConverter())
            .WithMessageType("customer_registration", typeof(CustomerRegistrationDto));
    },
    Assembly.GetExecutingAssembly());

Receive updates from the queue and process them:

IYandexMq client = ... // Get IYandexMq
IMqTransportService transport = ... // Get IMqTransportService

var request = new ReceiveMessageRequest()
    .SetQueueUrl("[Queue url]")
    .SetAllMessageAttribute();

var response = await client.ReceiveMessageAsync(request);

if(response.HttpStatusCode == HttpStatusCode.OK && response.Messages.Count > 0)
{
    foreach (var message in response.Messages)
    {
        await transport.HandleAsync(message, CancellationToken.None);
        await client.DeleteMessageAsync(
            new DeleteMessageRequest()
                .SetQueueUrl(YandexMqClientOptions.QueueUrl)
                .SetReceiptHandle(message.ReceiptHandle), CancellationToken.None);
    }
}

More examples can be found here

License

YaCloudKit is provided "as is" under the MIT License.

For more information, see LICENSE.