Skip to main content

Boards

Boards

This operation is designed for the comprehensive retrieval of full Boards (also known as MealPlans) static data.

Models

Step 1: Define the Models of your response (Request and Response models)

These models are crucial because they specify the structure of the objects contained within supplier responses. They'll also play a vital role in serializing and deserializing requests and responses during development.

Template Structure Models Boards

Boards usually do not require any request model, as they are HTTP GET methods.

Response example:

public class BoardsSupplierResponse
{
public List<Boards> Boards { get; set; }
}

public class Boards
{
public string Code { get; set; }
public string Name { get; set; }
}

Develop

Step 1: Register the serializers and operations

To specify which serializer and operations the developer will be using (based on the seller's API) we can specify it in our "Extensions":

File location: "Boards\BoardsExtensions.cs"

If the seller works with JSON format, we can specify the integration to work with JSON with the following:

using System.Text.Json;
using Connectors.Content.Pull.Api.Extensions.Boards;
using Connectors.Core.App.Extensions;
using ConnectorsTemplateContent.Common.Constants;
using ConnectorsTemplateContent.Boards.Models.Request;
using ConnectorsTemplateContent.Boards.Models.Response;
using BoardsOperation = ConnectorsTemplateContent.Boards.Operations.BoardsOperation;

namespace ConnectorsTemplateContent.Boards;

public static class BoardsExtensions
{
public static void AddBoardsServices(this IServiceCollection services,
IConfiguration configuration)
{
services.AddJsonSerializer<BoardsRequest, BoardsSupplierResponse>(ConfigureJSONOptions);
services.AddBoardsOperation<BoardsOperation, BoardsRequest, BoardsSupplierResponse, AccessModel>(
TgxPlatform.Name, configuration);
}
private static void ConfigureJSONOptions(JsonSerializerOptions options) { }
}

For details about others serializers, check Extensions.

Step 2: BoardsOperation validators

There are two previous validations that serve as a filter so the buildrequest and the parseresponse are as safe as possible. They can be found in the SearchOperation.cs class:

File location: "Boards\Operations\BoardsOperation.cs"

TryValidateBoards

TryValidateModelRequest

This step validates the incoming request from the client. While most validation is defined in the metadata, this step is used for specific edge cases that cannot be generalized.

public bool TryValidateModelRequest(ContentRequest connectorsRequest,
ContentOperationParameters<AccessModel> connectorParameters,
out IEnumerable<AdviseMessage> adviseMessages)
{
adviseMessages = null;
return true;
}

TryValidateSupplierResponses

Once the supplier's response is received, this step validates it for errors or anomalies. Suppliers may return incomplete or erroneous data, so this step ensures only valid responses are processed further.

Details:

  • Check for supplier-specific error fields.
  • Ensure required fields (e.g., boards) are present.
  • Example Use Case: A supplier might return a response with an error code or an empty boards list. This step would detect and handle such cases.
public bool TryValidateSupplierResponses(ContentOperationParameters<AccessModel> connectorParameters,
IEnumerable<SupplierResponseWrapper<BoardsSupplierResponse>> supplierResponses,
out IEnumerable<AdviseMessage> adviseMessages)
{
var supplierResponseWrappers = supplierResponses as SupplierResponseWrapper<BoardsSupplierResponse>[] ?? supplierResponses.ToArray();

var success = ResponseValidator.TryValidateSupplierResponses(supplierResponseWrappers, out adviseMessages);

if (!success) return false;

if (supplierResponseWrappers.ElementAt(0).Response.Boards is null)
{
adviseMessages =
[
AdviseMessage.BuildSupplierNoResults() // Indicates no results from the supplier.
];

return false;
}

return true; // Validation passes if no issues are found.
}

Step 3: Build the Seller's request

This class will contain a "BuildRequests" method that will have the following args:

  • Object of the requests from the models previously created (BoardsRequest).
  • The request that the buyer sends (connectorsRequest).
  • Parameters (connectorParameters) which will have some helpers.

File location: "Boards\Operations\BoardsOperation.BuildRequest.cs"

Build Request Boards

Example of Build Request:

using Connectors.Content.Pull.Application.Operations;
using Connectors.Core.Application.Connection;
using ConnectorsTemplateContent.Boards.Models.Request;
namespace ConnectorsTemplateContent.Boards.Operations;

internal partial class BoardsOperation
{
public IEnumerable<SupplierRequestWrapper<BoardsRequest>> BuildRequests(ContentRequest connectorsRequest,
ContentOperationParameters<AccessModel> connectorParameters)
{
string genericUrl = connectorParameters.ParametersModel.GenericUrl;

var request = new SupplierRequestWrapper<BoardsRequest>(
null,
new Uri(genericUrl),
HttpMethod.Post);

return new List<SupplierRequestWrapper<BoardsRequest>>()
{
request
};
}
}

Step 4: Parse the Seller's response

Once the request has been sent, we will have to control and parse the response returned by the seller.

We will be implementing the "ParseResponse" step inside BoardsOperation:

File location: "Boards\Operations\BoardsOperation.ParseResponse.cs"

Build Buyer&#39;s Response

Example of Parse Response:

using Connectors.Content.Pull.Application.Operations;
using Connectors.Content.Pull.Application.Operations.Boards;
using Connectors.Content.Pull.Domain.Contracts.Boards;
using Connectors.Core.Application.Connection;
using ConnectorsTemplateContent.Boards.Models.Response;
using Content.Common.Domain.AccumulativeContracts.Board;

namespace ConnectorsTemplateContent.Boards.Operations;

internal partial class BoardsOperation
{
public BoardsResponse ParseResponses(ContentRequest connectorsRequest,
ContentOperationParameters<AccessModel> connectorParameters,
IEnumerable<SupplierResponseWrapper<BoardsSupplierResponse>> supplierResponses,
CancellationToken cancellationToken)
{
ParseSupplierBoards(supplierResponses.First().Response);
return new BoardsResponse(new BoardsRs());
}

private void ParseSupplierBoards(BoardsSupplierResponse response)
{
foreach (var board in response.Boards)
{
//Accumulator is used instead of returning an object with the response.
_accumulator.AddValue(new AccumulativeBoard()
{
Code = board.Code,
Names = new Dictionary<Iso2LanguageEnum, string>
{
{ Iso2LanguageEnum.EN, board.Name },
}
});
}
}
}

Code Review

Step 1: Create Pull Request

  • Commit your Search changes and push them to a new branch called "BoardsDevelopment" into the original repository.
  • Separate the Pull Request into minimum these 4 commits:
    • Request and Response models
    • BuildRequest
    • ParseResponse

Step 2: Wait for Travelgate review

  • This step involves waiting for the Travelgate team to review and approve the submitted pull request, for more details, check Code Review Details