Skip to content
Snippets Groups Projects
SenderDemo.cs 3.99 KiB
using System.Net.Mime;
using System.Text;
using FitConnect;
using FitConnect.Encryption;
using FitConnect.Models;
using FitConnect.Models.Api.Metadata;
using FitConnect.Models.Api.Set;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace ConsoleAppExample;

public static class SenderDemo {
    public static async Task Run(IConfigurationRoot config, ILogger logger) {
        if (config["FitConnect:Sender:DestinationId"] == null)
            throw new ArgumentException("Destination ID is not set in config file");

        var clientId = config["FitConnect:Sender:ClientId"];
        var destinationId = new Guid(config["FitConnect:Sender:DestinationId"]!);
        var clientSecret = config["FitConnect:Sender:ClientSecret"];
        var serviceIdentifier = config["FitConnect:Sender:ServiceIdentifier"];

        if (clientId == null || clientSecret == null || serviceIdentifier == null) {
            logger.LogError("Missing configuration values");
            return;
        }

        OutputHelper.PrintSender();

        var sendableSubmission = SendableSubmission.Builder()
            .SetDestination(destinationId)
            .SetServiceType(serviceIdentifier, "FIT Connect Demo")
            .SetJsonData("{\"message\":\"Hello World\"}")
            .AddAttachments(
                Attachment.FromPath("./Attachments/Test.pdf", "application/pdf",
                    description: "Test Attachment"),
                Attachment.FromPath("./Attachments/Test.pdf", "application/pdf",
                    description: "Test Attachment #2"),
                Attachment.FromString("{\"message\":\"Hello World\"}",
                    MediaTypeNames.Application.Json, "data.json")
            )
            .SetAuthenticationInformation(new AuthenticationInformation("12345",
                AuthenticationInformationType.IdentificationReport, "1.3.5"))
            .SetPaymentInformation(new PaymentInformation(PaymentMethod.Creditcard,
                PaymentStatus.Booked, "12345445", "2345566"))
            .SetReplyChannel(ReplyChannel.FromEMail("a@example.com"))
            .Build();

        var sender =
            ClientFactory.GetSenderClient(FitConnectEnvironment.Testing, clientId, clientSecret,
                logger);
        await sender.SendAsync(sendableSubmission);
    }

    public static async Task RunEncrypted(IConfigurationRoot config, ILogger logger) {
        var configDestinationIdValue = config["FitConnect:Sender:DestinationId"];
        if (configDestinationIdValue == null)
            throw new ArgumentException("Value for DestinationId is not set in the config");

        var clientId = config["FitConnect:Sender:ClientId"];
        var clientSecret = config["FitConnect:Sender:ClientSecret"];
        var destinationId = Guid.Parse(configDestinationIdValue);
        var serviceIdentifier = config["FitConnect:Sender:ServiceIdentifier"];

        if (clientId == null || clientSecret == null || serviceIdentifier == null) {
            logger.LogError("Missing configuration values");
            return;
        }

        var encryption = new FitEncryption(null, null, null);
        Dictionary<Guid, string> encryptedAttachments = null!;
        string? encryptedMetadata = null;
        string? encryptedData = null;


        var publicKey = await ClientFactory
            .GetSenderClient(FitConnectEnvironment.Testing, clientId, clientSecret, logger)
            .GetPublicKeyForDestinationAsync(destinationId);


        var sendableEncryptedSubmission = SendableEncryptedSubmission.Builder()
            .SetDestination(destinationId)
            .SetServiceType(serviceIdentifier, "FIT Connect Demo")
            .SetEncryptedMetadata(encryptedMetadata!)
            .SetEncryptedData(encryptedData!)
            .AddEncryptedAttachments(encryptedAttachments)
            .Build();
        var sentSubmission = await ClientFactory
            .GetSenderClient(FitConnectEnvironment.Testing, clientId, clientSecret, logger)
            .SendAsync(sendableEncryptedSubmission);
    }
}