Preoperation Overview
Preoperations are preparatory steps required by the supplier before executing a main operation (see Operation Overview).
These preoperations follow the same structure as regular operations and implement the IPre<Operation>Operations<SupplierRequest, SupplierResponse, Binder>
interface. Preoperations can be applied to Search
, Quote
, Book
, CheckBookingsByReference
, CheckBookingsByDates
, or Cancel
.
Adding a Preoperation
To register a preoperation, use the appropriate service extensions based on the operation type. For example, for a Quote
preoperation:
services.AddPreQuoteOperation<PriceOperation, PriceRq, PriceRs, CvcAccessModel>();
Specify the serializer to use for the request and response format (e.g., JSON, XML, or a custom serializer):
services.AddJsonSerializer<PriceRq, PriceRs>(_ => { });
Key Notes
- Purpose: Preoperations are crucial when suppliers require additional preparatory steps, such as fetching tokens or rules, before the main operation.
- Flexibility: Use
IContextCache
to streamline data sharing across operations, reducing redundant calls. - Modular Integration: Register preoperations and serializers in the service collection, ensuring seamless integration with the Connector's architecture.
Example Preoperation: Quote
The following example demonstrates a Quote
preoperation:
public partial class PriceOperation(IContextCache contextCache, ICancelPenaltyManager cancelPenaltyManager)
: IPreQuoteOperations<PriceRq, PriceRs, CvcAccessModel>
{
public bool TryValidateModelRequest(
QuoteConnectorRequest connectorsRequest, QuoteParameters<CvcAccessModel> connectorParameters,
out IEnumerable<AdviseMessage> adviseMessages)
{
adviseMessages = default;
return true;
}
public bool TryValidateSupplierResponses(
QuoteParameters<CvcAccessModel> connectorParameters, IEnumerable<SupplierResponseWrapper<PriceRs>> supplierResponses,
out IEnumerable<AdviseMessage> adviseMessages)
{
return supplierResponses.TryValidateSupplierResponses(out adviseMessages);
}
public IEnumerable<SupplierRequestWrapper<PriceRq>> BuildRequests(
QuoteConnectorRequest connectorsRequest, QuoteParameters<CvcAccessModel> connectorParameters)
{
// Implement request-building logic here
}
}
Sharing Data Between Preoperations and Operations
To pass data between a preoperation and the main operation (or another preoperation), the IContextCache
class is used. This class allows you to store and retrieve custom data objects.
Injecting IContextCache
The IContextCache
instance is injected into the constructor via Dependency Injection:
public partial class PriceOperation : IPreQuoteOperations<PriceRq, PriceRs, CvcAccessModel>
{
private readonly IContextCache _contextCache;
private readonly ICancelPenaltyManager _cancelPenaltyManager;
public PriceOperation(IContextCache contextCache, ICancelPenaltyManager cancelPenaltyManager)
{
_contextCache = contextCache;
_cancelPenaltyManager = cancelPenaltyManager;
}
}
Adding Data to IContextCache
Data can be added to the context cache in the ParseResponses
method:
public QuoteConnectorResponse ParseResponses(
QuoteConnectorRequest connectorsRequest, QuoteParameters<CvcAccessModel> connectorParameters,
IEnumerable<SupplierResponseWrapper<PriceRs>> supplierResponses, CancellationToken cancellationToken)
{
var firstResponse = supplierResponses.First().Response;
var data = new Dictionary<uint, List<CancellationPolicy>>();
foreach (var bookingRule in firstResponse.BookingRules)
{
data.Add((uint)bookingRule.ProductTokenIndex, bookingRule.CancellationPolicies);
}
_contextCache.TryAdd(data);
return new QuoteConnectorResponse(new QuoteRs(default, default, default, PaymentType.MerchantPay));
}
Retrieving Data from IContextCache
Data stored in the cache can be retrieved in another operation or preoperation:
_contextCache.TryGet<LoginTokenData>(out var response);