The API controller is essential for communication between the server and Flexmonster Pivot. This guide describes how to implement the API controller for Flexmonster.DataServer.Core.dll
.
Note This tutorial relates to the guide Referencing 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
that contains methods for handling custom data source API requests. These methods enable getting fields, members, and aggregated data.
To use these methods, add the field of the IApiService
type to the controller class:
private readonly IApiService _flexmonsterApiService;
public FlexmonsterAPIController(IApiService apiService)
{
_flexmonsterApiService = apiService;
}
From here, you need to handle four POST requests: /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;
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 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); }
Note Handling the /handshake request is optional. Flexmonster Pivot will proceed to the next request even if the server does not handle it.
The first type of request that must be handled 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. It should be handled with the GetMembers() method of the IApiService
class. This can 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.
The last request that must 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 use. Now you can return to the Referencing Flexmonster Data Server as a DLL guide and complete it.
You may be interested in the following articles: