Tutorial: Using SDK in backend to handle endpoints

On this page you will learn how to use the SDK with your back-end application. The tutorial uses Uniscale's Message Threads demo solution as its base. For more detailed information on the SDK, see Library implementation basics.

You can find similar complete code samples with running instructions in our GitHub for every language:

SDK Installation

You can find guide with correct SDK information for your generated languages in your solution or service's SDK Portal in Uniscale.

Implementing the endpoint logic

Your SDK is built with the endpoints that you have defined. In order to get started with your backend you will need to implement the logic of endpoint and create an interceptor for it that you register inside PlatformSession.

In this sample we will implement basic logic for GetOrRegister endpoint. This endpoint will simply return the pre-generated sample data for return type. We then register that interceptor into instance of session, which can be injected/passed to our endpoint/controller.

For example if we create ASP.NET Core WebAPI. We will need to add PlatformSession into services when building the WebApplication.

Program.cs
using Uniscale.Core;
using Uniscale.Designtime;
using UniscaleDemo.Account_1_0.Functionality.ServiceToModule.Account.Registration.ContinueToApplication;
using UniscaleDemo.Account.Account;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddSingleton<PlatformSession>(_ =>
{
    return Platform.Builder()
        // Implement what happens on endpoints when GatewayRequests come in.
        .WithInterceptors(i => i
            .InterceptRequest(
                GetOrRegister.AllFeatureUsages,
                GetOrRegister.Handle((input, ctx) =>
                    Result<UserFull>.Ok(UserFull.Samples().DefaultSample()))))
        .Build()
        .Result;
});

// ... Rest of your basic .NET scaffold / Initialize code

Now there is server running with PlatformSession ready to be injected/passed.

Create network endpoint

With server running and SDK endpoint logic implemented into your session, you're still missing the network endpoint. You will only need to be able to inject/pass instance of PlatformSession into your controller/ network endpoint definition. Then inside your network endpoint you need to pass the GatewayRequest json data into the session's acceptGatewayRequest function and return the output of that function in json.

Use the .toJson function of Result to avoid any serialization issues.

These endpoints can be created using any library or protocol fits your needs. You'll just need to be able to pass the content. We will select a library for each language and showcase an example, but this does not mean it would only work with these showcased techniques.

For basic ASP.NET WebAPI this means injecting the PlatformSession into your controller and creating single HTTP endpoint that serves the app based on the created instance of PlatformSession.

GatewayController.cs
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Uniscale.Core;

// Sample implementation for demo. Please note that this is just one showcase
// You are free to use your own protocols/libraries for this task.
namespace DemoApi.Controllers;

[ApiController]
[Route("")]
public class GatewayController : ControllerBase
{
    // Inject the PlatformSession into your controller
    private readonly PlatformSession _session;
    
    public GatewayController(PlatformSession session)
    {
        _session = session;
    }
    
    [HttpPost]
    public async Task<string> Handle([FromBody]JsonElement request)
    {
        // All you need to do is pass json to session and parse result to json.
        var result = await _session.AcceptGatewayRequest(request.GetRawText());
        return result.ToJson();
    }
}

And with just couple of steps like that we have created backend that can be easily called with a front end client that was created based on Quick Start: Front-end with Uniscale SDK.

Last updated