-
Notifications
You must be signed in to change notification settings - Fork 13
Define the APIs
| Main > Using Liquid for building your application > Define the APIs |
|---|
A microservice must expose its functionalities as public accessible endpoints.
Currently Liquid makes it easier for a microservice to receive synchronous calls via REST endpoints as well as asynchronous ones via messages sent to topics and queues of a message bus.
| Take a look at related key concepts of Liquid |
|---|
| Encapsulated Domain Logic |
| Microservice Sizing |
| Business Error Handling |
#Synchronous calls (via REST)
Liquid provides a LightController base class with many high level primitives so as to respond to REST calls in a much easier and standardized way.
[ApiVersion("1.0")]
public class MyController : LightController
{
[HttpGet("/All")]
public IActionResult GetAll()
{
var response = Factory<MyService>().GetAsync();
return Result(response);
}
}
| See how this is done in Liquid Hotel360 sample application. |
|---|
##Activate Swagger/Open API Specification Liquid also abstracts the basic and common Swagger decoration of a Controller (with Attributes), thus freeing developers of doing repetitive work.
[HttpGet("/All")] //This will be automatically documented with Swagger
public IActionResult GetAll()
{
...
}
Additionally, Liquid encapsulates the activation of Swagger based on configuration files.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddWorkBench(Configuration); //This activates Swagger automatically
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseWorkBench(Configuration); //This activates Swagger automatically
}
| See how this is done in Liquid Hotel360 sample application. |
|---|
#Asynchronous messages (via Message Bus)
Messages a microservice respond to are also considered one of its public exposed APIs - an asynchronous one.
Therefore, Liquid allows receiving and processing messages from a specific Message Bus cartridge that was previously configured:
[MessageBus("FOO")]
public class FooWorker: LightWorker
{
[Queue("my_queue", 1)]
public void ProcessMessage(QueuedMessage1 msg)
{
ValidateInput(msg);
//Calls domain (business) logic
var response = Factory<FooService>().AddAsync(msg.MapTo<fooVM>());
//Terminates the message according to domain response
Terminate(response.Result);
}
}
[Topic("my_topic", "AllNotification")]
public void ProcessNotification(SubscribedMessage1 msg)
{
...
}
| See how this is done in Liquid Hotel360 sample application. |
|---|