Library implementation
Learn about the library generated via Uniscale.
Prerequisites
You understand the architecture of the SDK.
You have generated an SDK and downloaded it with one of the supported package managers.
You can locate endpoints and contracts of the SDK for imports.
Introduction
You now have the SDK at hand and you're wondering how to get started. To utilize your endpoints you can use the generated Platform
. You will need to start by initializing a PlatformSession
that will work as a base for your actions. Depending on the needs of your specific service, you will then use the session to initialize a dispatcher, create forwarding sessions, accept gateway requests, etc.
Our sessions come bundled with support for client-server usage and request forwarding tools for when you're working with multiple services at once. Interceptors make life easy for you defining exactly what needs to be implemented and what data you have in use. On top of that with pattern interceptors, you can implement a bunch at a time. This allows you to implement your authentication and transport mechanisms in a way that works for you.
You can use the library to test out and prototype your service interfaces ahead of building your service by using the generated sample data. When you have finished your implementation, you move from samples into proper implementations without having to change the interactions.
IDE plugin
We offer a variety of IDE plugins to seamlessly integrate Uniscale with your code editors, enhancing and streamlining your interaction with the Uniscale SDK.
To learn more: IDE plugins
Initialize session
To get started you will first have to create your PlatformSession
. It will allow you to easily write handlers for your endpoints and interact with them as a user of your defined functionality. Platform session is something you initialize once and keep for the lifetime of your application.
Starter for session initialisation. Create from the builder and define interceptors.
using Uniscale.Designtime;
var session = await Platform.Builder()
// Set up interceptors for the endpoints
.WithInterceptors(i =>
{
// Implementation in later samples
})
// And build the session
.Build();
While you can get started with just interceptors, the session initialization allows you to define more functionality on a general level.
using Uniscale.Designtime;
var session = await Platform.Builder()
// Set up interceptors for the endpoints
.WithInterceptors(i =>
{
// Implementation in later samples
})
// Define functionality for logging throughout
.OnLogMessage(message => Console.WriteLine(message))
// Inspect requested and returned objects
.InspectRequests((o, ctx) => Console.WriteLine(o.ToString()))
.InspectResponses((result, o, ctx) => Console.WriteLine(result.Value))
// And build the session
.Build();
Starter for session initialisation. Create from the builder and define interceptors.
from uniscale.core.platform.platform import Platform
session = (
await Platform.builder()
# Set up an interceptors for the endpoints
.with_interceptors(
lambda i: i
# Implementation in later samples
).build()
)
While you can get started with just interceptors, the session initialization allows you to define more functionality on a general level.
from uniscale.core.platform.platform import Platform
session = (
await Platform.builder()
# Set up an interceptors for the endpoints
.with_interceptors(
lambda i: i
# Implementation in later samples
)
# Define functionality for logging throughout
.on_log_message(lambda message: print(message))
# Inspect requested and returned objects
.inspect_requests(lambda o, ctx: print(o))
.inspect_responses(lambda result, o, ctx: print(result.Value))
# And build the session
.build()
)
Starter for session initialisation. Create from the builder and define interceptors.
import com.uniscale.sdk.Platform;
var session =
Platform.builder()
// Set up interceptors for the endpoints
.withInterceptors(
i -> {
// Implementation in later samples
})
// And build the session
.build()
.join();
While you can get started with just interceptors, the session initialization allows you to define more functionality on general level.
using Uniscale.Designtime;
var session =
Platform.builder()
// Set up interceptors for the endpoints
.withInterceptors(
i -> {
// Implementation in later samples
})
// Define functionality for logging throughout
.onLogMessage(System.out::println)
// Inspect requested and returned objects
.inspectRequests((o, ctx) -> System.out.println(o.toString()))
.inspectResponses((result, o, ctx) ->
System.out.println(result.getValue()))
// And build the session
.build()
.join();
Starter for session initialisation. Create from the builder and define interceptors.
import { Platform } from "@uniscale-sdk/ActorCharacter-Messagethreads"
const session = await Platform.builder()
// Set up interceptors for the endpoints
.withInterceptors(i => {
// Implementation in later samples
})
// And build the session
.build()
While you can get started with just interceptors, the session initialization allows you to define more functionality on a general level.
import { Platform } from "@uniscale-sdk/ActorCharacter-Messagethreads"
const session = await Platform.builder()
// Set up interceptors for the endpoints
.withInterceptors(i => {
// Implementation in later samples
})
// Define functionality for logging throughout
.onLogMessage({ logLevel: 2, write: (message) => console.log(message) })
// Inspect requested and returned objects
.inspectRequests((input, _ctx) => {
console.log(input.toString())
})
.inspectResponses((result, _input, _ctx) => {
console.log(result.value)
})
// And build the session
.build()
Starter for session initialisation. Create from the builder and define interceptors.
<?php
use Uniscale\Platform\Platform;
$platformSession = Platform::builder()
// Set up interceptors for the endpoints
->withInterceptors(function ($i) {
// Implementation in later samples
})
// And build the session
->build();
While you can get started with just interceptors, the session initialization allows you to define more functionality on a general level.
<?php
use Uniscale\Logging\ILogger;
use Uniscale\Platform\Platform;
use Uniscale\Platform\Types\RequestInspector;
use Uniscale\Platform\Types\ResponseInspector;
class MyLogger implements ILogger { ... }
class MyRequestInspector implements RequestInspector { ... }
class MyResponseInspector implements ResponseInspector { ... }
$this->platformSession = Platform::builder()
// Set up interceptors for the endpoints
->withInterceptors(function ($i) {
// Implementation in later samples
})
// Define functionality for logging throughout
->onLogMessage(new MyLogger())
// Inspect requested and returned objects
->inspectRequests(new MyRequestInspector())
->inspectResponses(new MyResponseInspector())
// And build the session
->build();
Defining interceptors
Define the functionality of your endpoints inside the interceptors. You can intercept each function separately and have the data typed, or intercept whole namespaces and deal with untyped data.
Your interceptor's handle
function has two parameters:
The input model defined for your endpoint
FeatureContext
This context will follow any request you make and contains the information about which solution, characters, language, data tenant, and more this request was made with.
In a typical situation, you can write interceptors for each endpoint
using Uniscale.Designtime;
using UniscaleDemo.Account_1_0.Functionality.ServiceToModule.
Account.Registration.ContinueToApplication;
using UniscaleDemo.Account.Account;
using UniscaleDemo.Messages;
var session = await Platform.Builder()
// Set up interceptors for endpoints
.WithInterceptors(i =>
{
i.InterceptRequest(
GetOrRegister.AllFeatureUsages,
GetOrRegister.Handle((input, ctx) =>
Result<UserFull>.Ok(UserFull.Samples().DefaultSample())));
// You can also intercept with patterns
i.InterceptMessage(
Patterns.Messages.SendMessage.AllMessageUsages,
Patterns.Messages.SendMessage.Handle((input, ctx) =>
{
// You can validate and use defined error codes on return
if (string.IsNullOrEmpty(input.Message))
return Result.BadRequest(
ErrorCodes.Messages.InvalidMessageLength);
Console.WriteLine(
ctx.Characters.Performer?.Terminology == "Term.Admin"
? $"Admin sent message: {input.Message}"
: $"{input.By} sent message: {input.Message}");
return Result.Ok();
}));
})
.Build();
However, there are some cases where it makes more sense to have one implementation for a group of endpoints, like for example in your frontend when calling a specific backend service for all endpoints under the messages namespace. We stay out of your way and allow you to choose your preferred authentication and transport mechanisms.
using Uniscale.Core;
using Uniscale.Designtime;
using UniscaleDemo.Messages;
var session = await Platform.Builder()
.WithInterceptors(i =>
{
// We can intercept the whole messages namespace with a pattern
i.InterceptPattern(Patterns.Messages.Pattern, async (input, ctx) =>
{
// By creating a GatewayRequest JSON, the receiving end can simply
// handle incoming calls with session.AcceptGatewayRequest()
var json = GatewayRequest.From(input, ctx).ToJson();
var responseJson = await SendToMessageService(json);
// Convert JSON return from AcceptGatewayRequest
return Result<object>.FromJson(responseJson);
});
// You can also intercept all (remaining) calls to a general endpoint
i.InterceptPattern("*", async (input, ctx) =>
{
var request = GatewayRequest.From(input, ctx);
var responseJson = await CallGeneralServiceEndpoint(request);
return Result<object>.FromJson(responseJson);
});
})
.Build();
In typical situation you can write interceptors for each endpoint
from uniscale.core.platform.platform import Platform
from uniscale.core import Result
from uniscale.core.utilisation.utilisation_session_base import FeatureContext
from uniscale.uniscaledemo.account_1_0.functionality.servicetomodule.account.registration.continuetoapplication.get_or_register import GetOrRegister
from uniscale.uniscaledemo.account.account.user_full import UserFull
from uniscale.uniscaledemo.messages.messages.send_message_input import SendMessageInput
from uniscale.uniscaledemo.messages.patterns import Patterns
from uniscale.uniscaledemo.messages_1_0.error_codes import ErrorCodes
# Example for under message intercept lambda
def handle_send_message(
cinput: SendMessageInput, ctx: FeatureContext
) -> Result:
# You can validate and use defined error codes on return
if not cinput.message:
return Result.bad_request(ErrorCodes.messages.invalid_message_length)
if ctx.characters.performer.terminology == "Term.Admin":
print("Admin sent message: " + cinput.message)
else:
print(cinput.by + " sent message: " + cinput.message)
return Result.ok(None)
session = (
await Platform.builder()
# Set up an interceptors for the endpoints
.with_interceptors(
lambda i: i.intercept_request(
GetOrRegister.all_feature_usages,
GetOrRegister.handle(
lambda cinput, ctx: Result.ok(
UserFull.samples().default_sample()
)
),
# You can also intercept with patterns
).intercept_message(
Patterns.messages.send_message.all_message_usages,
Patterns.messages.send_message.handle(
lambda cinput, ctx: handle_send_message
),
)
).build()
)
However there are some cases where it makes more sense to have one implementation for group of endpoints, like for example in your frontend when calling specific backend service for all endpoints under the messages namespace. We stay out of your way and allow you to choose your preferred authentication and transport mechanisms.
from uniscale.core.platform.platform import Platform
from uniscale.core import Result, GatewayRequest
from uniscale.uniscaledemo.messages.patterns import Patterns
# Example for under message intercept lambda
async def gateway_message_interceptor(cinput, ctx) -> Result:
# By creating GatewayRequest JSON, receiving end can simply
# handle incoming call with session.accept_gateway_request()
json = GatewayRequest.from_(cinput, ctx).to_json()
response_json = await send_to_message_service(json)
# Convert JSON return from accept_gateway_request
result = Result.from_json(response_json)
return result
# Example for under general intercept lambda
async def gateway_general_interceptor(cinput, ctx) -> Result:
request = GatewayRequest.from_(cinput, ctx)
response_json = await call_general_service_endpoint(request)
result = Result.from_json(response_json)
return result
session = (
await Platform.builder()
.with_interceptors(
# We can intercept whole messages namespace with pattern
lambda i: i.intercept_pattern(
Patterns.messages.pattern,
gateway_message_interceptor,
# You can intercept all (remaining) calls to general endpoint
).intercept_pattern("*", gateway_general_interceptor)
)
.build()
)
In typical situation you can write interceptors for each endpoint
import com.uniscale.sdk.Platform;
import com.uniscale.sdk.core.Result;
import java.util.Objects;
import uniscaledemo.account.account.UserFull;
import uniscaledemo.account_1_0.functionality.servicetomodule.account.registration.continuetoapplication.GetOrRegister;
import uniscaledemo.messages.Patterns;
import uniscaledemo.messages_1_0.ErrorCodes;
var session =
Platform.builder()
// Set up an interceptors for features
.withInterceptors(
i -> {
i.interceptRequest(
GetOrRegister.allFeatureUsages,
GetOrRegister.handle(
(input, ctx) -> Result.ok(UserFull.samples().defaultSample())));
// You can also intercept with patterns
i.interceptMessage(
Patterns.messages.sendMessage.allMessageUsages,
Patterns.messages.sendMessage.handle(
(input, ctx) -> {
// You can validate and use defined error codes on return
if (input.getMessage() == null || input.getMessage().isEmpty())
return Result.badRequest(ErrorCodes.messages.invalidMessageLength);
System.out.println(
Objects.equals(
ctx.getCharacters().getPerformer().getTerminology(),
"Term.Admin")
? String.format("Admin sent message: %s", input.getMessage())
: String.format(
"%s sent message: %s", input.getBy(), input.getMessage()));
return Result.ok();
}));
})
.build()
.join();
However there are some cases where it makes more sense to have one implementation for group of endpoints, like for example in your frontend when calling specific backend service for all endpoints under the messages namespace. We stay out of your way and allow you to choose your preferred authentication and transport mechanisms.
import com.uniscale.sdk.Platform;
import com.uniscale.sdk.core.GatewayRequest;
import com.uniscale.sdk.core.Result;
import java.util.concurrent.CompletableFuture;
import uniscaledemo.messages.Patterns;
var session =
Platform.builder()
.withInterceptors(
i -> {
// We can intercept whole messages namespace with pattern
i.interceptPattern(
Patterns.messages.getPattern(),
(input, ctx) -> {
// By creating GatewayRequest JSON, receiving end can simply
// handle incoming call with session.acceptGatewayRequest()
var json = GatewayRequest.from(input, ctx).toJson();
var responseJson = sendToMessageService(json);
// Convert JSON return from acceptGatewayRequest
return CompletableFuture.completedFuture(Result.fromJson(responseJson));
});
// You can also intercept all (remaining) calls to general endpoint
i.interceptPattern(
"*",
(input, ctx) -> {
var request = GatewayRequest.from(input, ctx);
var responseJson = callGeneralServiceEndpoint(request);
return CompletableFuture.completedFuture(Result.fromJson(responseJson));
});
})
.build()
.join();
In typical situation you can write interceptors for each endpoint
import { Platform, Result } from "@uniscale-sdk/ActorCharacter-Messagethreads"
import { Patterns as MessagePatterns } from "@uniscale-sdk/ActorCharacter-Messagethreads/sdk/UniscaleDemo/Messages"
import { GetOrRegister } from "@uniscale-sdk/ActorCharacter-Messagethreads/sdk/UniscaleDemo/Account_1_0/Functionality/ServiceToModule/Account/Registration/ContinueToApplication"
import { UserFull } from "@uniscale-sdk/ActorCharacter-Messagethreads/sdk/UniscaleDemo/Account/Account/UserFull"
import { ErrorCodes } from "@uniscale-sdk/ActorCharacter-Messagethreads/sdk/UniscaleDemo/Messages_1_0"
const session = await Platform.builder()
// Set up interceptors for the endpoints
.withInterceptors((i) => {
i.interceptRequest(
GetOrRegister.allFeatureUsages,
GetOrRegister.handle((_input, _ctx) => {
return Result.ok(UserFull.samples().defaultSample())
}),
)
// You can also intercept with patterns
i.interceptMessage(
MessagePatterns.messages.sendMessage.allMessageUsages,
MessagePatterns.messages.sendMessage.handle((input, ctx) => {
// You can validate and use defined error codes on return
if (!input.message) {
return Result.badRequest(ErrorCodes.messages.invalidMessageLength)
}
console.log(
ctx.characters.performer?.terminology === "Term.Admin"
? `Admin sent a message: ${input.message}`
: `${input.by} sent a message: ${input.message}`,
);
return Result.ok(undefined)
}),
);
})
.build()
However there are some cases where it makes more sense to have one implementation for group of endpoints, like for example in your frontend when calling specific backend service for all endpoints under the messages namespace. We stay out of your way and allow you to choose your preferred authentication and transport mechanisms.
import { GatewayRequest, Platform, Result } from "@uniscale-sdk/ActorCharacter-Messagethreads"
import { Patterns as MessagePatterns } from "@uniscale-sdk/ActorCharacter-Messagethreads/sdk/UniscaleDemo/Messages"
const session = await Platform.builder()
.withInterceptors((i) => {
i.interceptPattern(MessagePatterns.messages.pattern, async (input, ctx) => {
// By creating GatewayRequest JSON, the receiving end can simply
// handle incoming call with session.acceptGatewayRequest()
const json = GatewayRequest.from(input, ctx).toJson()
const responseJson = await sendToMessageService(json)
// The response needs to be serialized into the Result class
return Result.fromJson(responseJson)
})
// You can also intercept all (remaining) calls to a general endpoint
i.interceptPattern("*", async (input, ctx) => {
const request = GatewayRequest.from(input, ctx)
const responseJson = await callGeneralServiceEndpoint(request)
return Result.fromJson(responseJson)
})
})
.build()
In typical situation you can write interceptors for each endpoint
<?php
use Uniscale\Http\Result;
use Uniscale\Platform\Platform;
use Uniscale\Uniscaledemo\Account\Account\UserFull;
use Uniscale\Uniscaledemo\Account\Functionality\ServiceToModule\Account\Registration\GetOrRegister;
use Uniscale\Uniscaledemo\Messages\Patterns as MessagePatterns;
use Uniscale\Uniscaledemo\Messages_1_0\ErrorCodes;
use Uniscale\Utilisation\Types\FeatureContext;
$builder = Platform::builder();
// Set up interceptors for the endpoints
$builder->withInterceptors(function ($interceptor) {
$interceptor->interceptRequest(
GetOrRegister::ALL_FEATURE_USAGES,
GetOrRegister::handle(function (string $input, FeatureContext $ctx) {
return Result::ok(UserFull::samples()->defaultSample());
})
);
// You can also intercept with patterns
$interceptor->interceptMessage(
MessagePatterns::$messages->sendMessage->allMessageUsages,
MessagePatterns::$messages->sendMessage->handle(function ($input, FeatureContext $ctx) {
// You can validate and use defined error codes on return
if (empty($input['message'])) {
return Result::badRequest(ErrorCodes::$messages->invalidMessageLength);
}
if ($ctx->characters->performer->terminology === "Term.Admin") {
echo "Admin sent a message: " . $input['message'];
} else {
echo $input['by'] . " sent a message: " . $input['message'];
}
return Result::ok();
})
);
});
$session = $builder->build();
However there are some cases where it makes more sense to have one implementation for group of endpoints, like for example in your frontend when calling specific backend service for all endpoints under the messages namespace. We stay out of your way and allow you to choose your preferred authentication and transport mechanisms.
<?php
use Uniscale\Http\Result;
use Uniscale\Models\BackendActions\GatewayRequest;
use Uniscale\Platform\Platform;
use Uniscale\Uniscaledemo\Messages\Patterns as MessagePatterns;
use Uniscale\Utilisation\Types\FeatureContext;
$builder = Platform::builder();
// Set up interceptors for the patterns
$builder->withInterceptors(function ($interceptor) {
$interceptor->interceptPattern(
MessagePatterns::$messages->pattern,
function ($input, FeatureContext $ctx) {
// By creating GatewayRequest JSON, the receiving end can simply
// handle incoming call with session.acceptGatewayRequest()
$json = GatewayRequest::from($input, $ctx)->toJson();
$responseJson = sendToMessageService($json);
// The response needs to be serialized into the Result class
return Result::fromJson($responseJson);
}
);
// You can also intercept all (remaining) calls to a general endpoint
$interceptor->interceptPattern(
"*",
function ($input, $ctx) {
$request = GatewayRequest::from($input, $ctx);
$responseJson = callGeneralServiceEndpoint($request);
return Result::fromJson($responseJson);
}
);
});
$session = $builder->build();
Handle endpoints and make requests
With the platform session, you have a setup that knows how to handle your endpoints. To utilize that and call your endpoints you need to get a DispatcherSession
through which you can interact with your endpoints. Dispatchers are designed to be set up in a way where one dispatcher can handle all your solution/service's endpoints.
Making a request: PlatformSession -> DispatcherSession -> Request()
Platform sessions can also be used to receive requests. If in your endpoint you can turn your data into a GatewayRequest (or you sent the data with our helpers), you use AcceptGatewayRequest in your session and let the platform handle the request based on your interceptor implementations.
Receiving a request: PlatformSession -> AcceptGatewayRequest()
The typical case for making a request and creating a GatewayRequest from it.
using Uniscale.Core;
using Uniscale.Designtime;
using UniscaleDemo.Account_1_0.Functionality.ServiceToModule.
Account.Registration.ContinueToApplication;
var session = await Platform.Builder()
// Set up your PlatformSession as in above samples
// For example sending all to GeneralServiceEndpoint
.WithInterceptors(i =>
{
i.InterceptPattern("*", async (input, ctx) =>
{
var request = GatewayRequest.From(input, ctx);
var responseJson = await CallGeneralServiceEndpoint(request);
return Result<object>.FromJson(responseJson);
});
})
.Build();
// Initialize a DispatcherSession with a solution id of your choice
var dispatcher = session
.AsSolution(Guid.Parse("a18b2b3e-4010-4c0f-a01f-565fda8c466e"));
// Create a new user and check if we got a successful response
var result = await dispatcher
.Request(GetOrRegister.With("myAwesomeUserHandle"));
if (result.Success)
Console.WriteLine(
"User: id=" + result.Value.UserIdentifier +
" Handle=" + result.Value.Handle);
else
Console.WriteLine("Failed with: " + result.Error.ToLongString());
And a quick sample of how one would receive such a request.
using Uniscale.Core;
using UniscaleDemo.Account_1_0.Functionality.ServiceToModule.
Account.Registration.ContinueToApplication;
using UniscaleDemo.Account.Account;
var session = await 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();
// In your endpoint handler (Http endpoint or similar)
var result = await session.AcceptGatewayRequest(requestJson);
return result.ToJson();
The typical case for creating a request and creating a GatewayRequest from it.
from uuid import UUID
from uniscale.core.platform.platform import Platform
from uniscale.core import Result, GatewayRequest
from uniscale.uniscaledemo.account_1_0.functionality.servicetomodule.account.registration.continuetoapplication.get_or_register import GetOrRegister
# Example for under general intercept lambda
async def gateway_general_interceptor(cinput, ctx) -> Result:
request = GatewayRequest.from_(cinput, ctx)
response_json = await call_general_service_endpoint(request)
result = Result.from_json(response_json)
return result
session = (
await Platform.builder()
# Set up your PlatformSession as in above samples
# For example sending all to GeneralServiceEndpoint
.with_interceptors(
lambda i: i.intercept_pattern("*", gateway_general_interceptor)
).build()
)
# Initialize a DispatcherSession with a solution id of your choice
dispatcher = session.as_solution(UUID("a18b2b3e-4010-4c0f-a01f-565fda8c466e"))
# Create a new user and check if we got a successful response
result = await dispatcher.request(GetOrRegister.with_("myAwesomeUserHandle"))
if result.is_success():
print(
"User: id="
+ str(result.value.user_identifier)
+ " Handle="
+ result.value.handle
)
else:
print("Failed with: " + result.error.to_long_string())
And a quick sample on how one would receive such request.
from uniscale.core.platform.platform import Platform
from uniscale.core import Result, GatewayRequest
from uniscale.uniscaledemo.account_1_0.functionality.servicetomodule.account.registration.continuetoapplication.get_or_register import GetOrRegister
from uniscale.uniscaledemo.account.account.user_full import UserFull
session = (
await Platform.builder()
# Implement what happens on endpoints when GatewayRequests come in.
.with_interceptors(
lambda i: i.intercept_request(
GetOrRegister.all_feature_usages,
GetOrRegister.handle(
lambda cinput, ctx: Result.ok(
UserFull.samples().default_sample()
)
)
)
).build()
)
# In your endpoint handler (Http endpoint or similar)
result = await session.accept_gateway_request(request_json)
return result.to_json()
The typical case for creating a request and creating a GatewayRequest from it.
import com.uniscale.sdk.Platform;
import com.uniscale.sdk.core.GatewayRequest;
import com.uniscale.sdk.core.Result;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import uniscaledemo.account_1_0.functionality.servicetomodule.
account.registration.continuetoapplication.GetOrRegister;
var session =
Platform.builder()
// Set up your PlatformSession as in above samples
// For example sending all to GeneralServiceEndpoint
.withInterceptors(
i -> {
i.interceptPattern(
"*",
(input, ctx) -> {
var request = GatewayRequest.from(input, ctx);
var responseJson = callGeneralServiceEndpoint(request);
return CompletableFuture.completedFuture(
Result.fromJson(responseJson));
});
})
.build()
.join();
// Initialize a DispatcherSession with a solution id of your choice
var dispatcher = session.asSolution(
UUID.fromString("a18b2b3e-4010-4c0f-a01f-565fda8c466e"));
// Create a new user and check if we got a successful response
var result = dispatcher.request(GetOrRegister.with("myAwesomeUserHandle"))
.join();
if (result.isSuccess())
System.out.printf(
"User: id= %s Handle= %s%n",
result.getValue().getUserIdentifier(), result.getValue().getHandle());
else System.out.printf("Failed with: %s%n", result.getError().toLongString());
And a quick sample on how one would receive such request.
import com.uniscale.sdk.Platform;
import com.uniscale.sdk.core.Result;
import uniscaledemo.account.account.UserFull;
import uniscaledemo.account_1_0.functionality.servicetomodule.
account.registration.continuetoapplication.GetOrRegister;
var session =
Platform.builder()
// Implement what happens on endpoints when GatewayRequests come in.
.withInterceptors(
i ->
i.interceptRequest(
GetOrRegister.allFeatureUsages,
GetOrRegister.handle(
(input, ctx) -> Result.ok(UserFull.samples().defaultSample()))))
.build()
.join();
// In your endpoint handler (Http endpoint or similar)
var result = session.acceptGatewayRequest(requestJson).join();
return result.toJson();
The typical case for creating a request and creating a GatewayRequest from it.
import { GatewayRequest, Platform, Result } from "@uniscale-sdk/ActorCharacter-Messagethreads"
import { GetOrRegister } from "@uniscale-sdk/ActorCharacter-Messagethreads/sdk/UniscaleDemo/Account_1_0/Functionality/ServiceToModule/Account/Registration/ContinueToApplication"
const session = await Platform.builder()
.withInterceptors((i) => {
// Set up your PlatformSession as in above samples
// For example sending all to GeneralServiceEndpoint
i.interceptPattern("*", async (input, ctx) => {
const request = GatewayRequest.from(input, ctx)
const responseJson = callGeneralServiceEndpoint(request)
return Result.fromJson(responseJson)
})
})
.build()
// Initialize a DispatcherSession with a solution id of your choice
const dispatcher = session.asSolution('fb344616-794e-4bd7-b81a-fb1e3361701f')
// Create a new user and check if we got a successful response
const result = await dispatcher.request(GetOrRegister.with('myAwesomeUserHandle'))
if (result.success) {
console.log(`User: id=${result.value?.identifier}`)
return
}
console.log(`Failed with: ${result.error?.toLongString()}`)
And a quick sample on how one would receive such request.
import { Platform, Result, BackendAction } from "@uniscale-sdk/ActorCharacter-Messagethreads"
import { GetOrRegister } from "@uniscale-sdk/ActorCharacter-Messagethreads/sdk/UniscaleDemo/Account_1_0/Functionality/ServiceToModule/Account/Registration/ContinueToApplication"
import { UserFull } from "@uniscale-sdk/ActorCharacter-Messagethreads/sdk/UniscaleDemo/Account/Account/UserFull"
const session = await Platform.builder()
// Implement what happens on endpoints when GatewayRequests come in
.withInterceptors((i) => {
i.interceptRequest(
GetOrRegister.allFeatureUsages,
GetOrRegister.handle((_input, _ctx) => {
return Result.ok(UserFull.samples().defaultSample())
}),
)
})
.build()
// In your endpoint handler (Http endpoint or similar)
const result = await session.acceptGatewayRequest(requestJson)
return result.toJson()
The typical case for creating a request and creating a GatewayRequest from it.
<?php
use Ramsey\Uuid\Uuid;
use Uniscale\Http\Result;
use Uniscale\Models\BackendActions\GatewayRequest;
use Uniscale\Platform\Platform;
use Uniscale\Uniscaledemo\Account\Functionality\ServiceToModule\Account\Registration\GetOrRegister;
$builder = Platform::builder();
// Set up interceptors for the patterns
$builder->withInterceptors(function ($interceptor) {
// Set up your PlatformSession as in above samples
// For example sending all to GeneralServiceEndpoint
$interceptor->interceptPattern(
"*",
function ($input, $ctx) {
$request = GatewayRequest::from($input, $ctx);
$responseJson = callGeneralServiceEndpoint($request);
return Result::fromJson($responseJson);
}
);
});
$session = $builder->build();
// Initialize a DispatcherSession with a solution id of your choice
$dispatcher = $session->asSolution(Uuid::fromString('fb344616-794e-4bd7-b81a-fb1e3361701f'));
// Create a new user and check if we got a successful response
$result = $dispatcher->request(GetOrRegister::with('myAwesomeUserHandle'));
if ($result->isSuccess()) {
echo "User: id=" . $result->getValue()->identifier;
return;
}
echo "Failed with: " . $result->getError()->toLongString();
And a quick sample on how one would receive such request.
<?php
use Uniscale\Uniscaledemo\Account\Functionality\ServiceToModule\Account\Registration\GetOrRegister;
use Uniscale\Http\Result;
use Uniscale\Platform\Platform;
use Uniscale\Uniscaledemo\Account\Account\UserFull;
$builder = Platform::builder();
// Set up interceptors for the requests
$builder->withInterceptors(function ($interceptor) {
$interceptor->interceptRequest(
GetOrRegister::ALL_FEATURE_USAGES,
GetOrRegister::handle(function ($input, $ctx) {
return Result::ok(UserFull::samples()->defaultSample());
})
);
});
$session = $builder->build();
// In your endpoint handler (Http endpoint or similar)
$requestJson = file_get_contents('php://input'); // Assuming the request JSON is sent via POST
$result = $session->acceptGatewayRequest($requestJson);
echo $result->toJson();
You can use the created dispatcher to make multiple requests and fetch Terminologies that you have defined into your model. You can find a more complex sample of how forwarding works in Forwarding session.
Conclusion
This tutorial has information on everything you need to consider when getting started with your minimalistic implementation. If you want to see all of it come together, you can find samples based on our Demo solution at: https://github.com/uniscale?q=demo-. You can test it out, and mix and match frontend and backend demos as you want.
Last updated