Tutorial: Using SDK in backend to handle endpoints

Learn how to use the SDK in your 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 a 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 the endpoint and create an interceptor for it that you register inside PlatformSession.

In this sample, we will implement basic logic for GetOrRegister the endpoint. This endpoint will simply return the pre-generated sample data for return type. We then register that interceptor into an instance of a 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 a server running with PlatformSession ready to be injected/passed.

Create network endpoint

With the 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 an 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 that 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 a 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();
    }
}

With just a couple of steps like that, we have created a 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