Request#

Request class#

Represent a single request object to RPS Engine. Note: RPS Engine support multiple Request[] into a single call to RPS Engine.

public class Request
{
    public Guid Guid { get; } = Guid.NewGuid();

    public IRPSValue[] Values { get; set; }

    public Context RightsContext { get; set; }

    public ProcessingContext ProcessingContext { get; set; }

    public Context LoggingContext { get; set; }
}

RequestContext class#

RequestContext class used to combine several Requests into single call.

public class RequestContext
{
    private readonly ConcurrentDictionary<Guid, Request> _requestsByGuid
        = new ConcurrentDictionary<Guid, Request>();

    private readonly RPSEngine _engine;
    private readonly RPSEngineContextResolver _engineContextResolver;

    internal RequestContext(RPSEngine engine, RPSEngineContextResolver contextResolver = null)
    {
        _engine = engine;
        _engineContextResolver = contextResolver;
    }

    public IEnumerable<Request> Requests => _requestsByGuid.Values;

    public bool TryGetRequest(Guid requestGuid, out Request request)
        => _requestsByGuid.TryGetValue(requestGuid, out request);

    public void Transform() => _engine.Transform(this);

    public async Task TransformAsync() => await _engine.TransformAsync(this);

    public RequestContext WithRequest(IEnumerable<IRPSValue> rpsValues,
        string rightsContextName,
        string processingContextName,
        Context loggingContext = null)
    {
        if (_engineContextResolver == null)
            throw new InvalidOperationException("Context resolver not found");

        (Context RightsContext, ProcessingContext ProcessingContext) contexts =
            _engineContextResolver.Resolve(rightsContextName, processingContextName);

        return WithRequest(rpsValues,
            contexts.RightsContext,
            contexts.ProcessingContext,
            loggingContext: loggingContext);
    }

    public RequestContext WithRequest(IEnumerable<IRPSValue> rpsValues,
        Context rightsContext,
        ProcessingContext processingContext,
        Context loggingContext = null)
    {
        var request = new Request
        {
            Values = rpsValues.ToArray(),
            RightsContext = rightsContext,
            ProcessingContext = processingContext,
            LoggingContext = loggingContext
        };

        _requestsByGuid[request.Guid] = request;

        return this;
    }
}

RPSEngineContextResolver class#

An easy way to resolve both contexts by keys uses the IRPSEngineContextProvider interface.

public class RPSEngineContextResolver
{
    private readonly IRPSEngineContextProvider _contextProvider;

    public RPSEngineContextResolver(IRPSEngineContextProvider contextProvider)
    {
        _contextProvider = contextProvider;
    }

    public (RightsContext RightsContext, ProcessingContext ProcessingContext) Resolve(string rightsContextKey, string processingContextKey)
    {
        if (!_contextProvider.TryGetRightsContext(rightsContextKey, out RightsContext rightsContext))
            throw new Exception($"Rights context = '{rightsContextKey}' not found");

        if (!_contextProvider.TryGetProcessingContext(processingContextKey, out ProcessingContext processingContext))
            throw new Exception($"Processing context = '{processingContextKey}' not found");

        return (rightsContext, processingContext);
    }
}