The API controller is essential for the communication of the server and Flexmonster Pivot. This guide describes how to implement the API controller for Flexmonster.DataServer.Core.dll
.
This tutorial relates to the guide Referring the Data Server as a DLL and should be completed as a part of it.
If needed, create the Controllers
folder inside the project:
Then create a new API controller class FlexmonsterAPIController
inside this folder:
The code should be the following:
[Route("api")]
[ApiController]
public class FlexmonsterAPIController : ControllerBase
{
}
To use the DLL in the controller, add the following line of code to the beginning of the FlexmonsterAPIController.cs
file:
using Flexmonster.DataServer.Core;
IApiService
is a class from Flexmonster.DataServer.Core.dll
containing methods for handling the custom data source API requests. These methods allow getting fields, members, and aggregated data.
To use the methods, add the field of the IApiService
type to the controller class:
private readonly IApiService _flexmonsterApiService;
public FlexmonsterAPIController(IApiService apiService)
{
_flexmonsterApiService = apiService;
}
Then, four POST requests should be handled: /handshake, /fields, /members, and /select.
The Flexmonster.DataServer.Core.Models
class contains type definitions for requests and responses. To include it to the controller, add the following line of code to the beginning of the FlexmonsterAPIController.cs
file:
using Flexmonster.DataServer.Core.Models;
This step is optional as it describes the handling of a /handshake request, which is also optional. Even if the server does not handle it, Flexmonster Pivot will proceed to the next request.
The /handshake request establishes a connection between the client and server sides. If the server sends the implemented version of the custom data source API in response to the /handshake request, the client can check whether the server and the client implement the same version of the custom data source API.
To receive notifications about the version compatibility, respond to the /handshake request with the implemented version of the custom data source API:
const string API_VERSION = "2.8.5";
[Route("handshake")]
[HttpPost]
public IActionResult Handshake()
{
object response = new { version = API_VERSION };
return new JsonResult(response);
}
The first required request to handle is the /fields request. To handle it, use the GetFields() method of the IApiService
class:
[Route("fields")]
[HttpPost]
public IActionResult PostFields([FromBody]FieldsRequest request)
{
var response = _flexmonsterApiService.GetFields(request);
return new JsonResult(response);
}
FieldsRequest
is a predefined class from Flexmonster.DataServer.Core.Models
. It describes the /fields request’s structure.
The next required request to handle is the /members request. Handle it with the GetMembers() method of the IApiService
class. It should be done as follows:
[Route("members")]
[HttpPost]
public IActionResult PostMembers([FromBody]MembersRequest request)
{
var response = _flexmonsterApiService.GetMembers(request);
return new JsonResult(response);
}
MembersRequest
is a predefined class from Flexmonster.DataServer.Core.Models
. It describes the /members request’s structure.
One more request that should be handled is the /select request. To handle it, use the GetAggregatedData() method of the IApiService
class:
[Route("select")]
[HttpPost]
public IActionResult PostSelect([FromBody]SelectRequest request)
{
var response = _flexmonsterApiService.GetAggregatedData(request);
return new JsonResult(response);
}
SelectRequest
is a predefined class from Flexmonster.DataServer.Core.Models
. It describes the /select request’s structure.
After handling all the requests, the controller is ready for usage. Now you can return to the Referring Flexmonster Data Server as a DLL guide and complete it.
You may be interested in the following articles: