Get Free Trial
  1. API reference
Connecting to Data Source
  1. Supported data sources
  2. Connecting to other data sources
Configuring the component
  • Documentation for older versions
  • Table of contents

    Implementing the API controller

    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.

    Step 1. Create a new API controller

    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
    {
    }

    Step 2. Include Flexmonster.DataServer.Core in the controller

    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;

    Step 3. Add the IApiService field to the controller

    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.

    Step 4. Include Flexmonster.DataServer.Core.Models to the controller

    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;

    Step 5. Handle the /handshake 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 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.

    Step 6. Handle the /fields request

    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.

    Step 7. Handle the /members request

    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.

    Step 8. Handle the /select request

    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.

    What’s next?

    You may be interested in the following articles: