SwedbankPay/swedbank-pay-sdk-dotnet

Operations design

asbjornu opened this issue · 2 comments

This issue is for discussion of an operations design, so that we retain the functionality provided by our APIs with a list of dynamically available operations, so the state management of operation availability is preserved by the server and not moved up into the client.

Here's one suggestion with an Operations list. Please note that only the minimum code required to explain the idea is present, validation code, exception handling, etc., is left out for brevity.

public class PaymentOrder : Resource
{
    private PaymentOrder(Uri id, Operations operations)
    {
        // Set id and operations
    }

    public static PaymentOrder Get(Uri id)
    {
        // Perform HTTP GET of the id
        var operations = ...;
        retur new PaymentOrder(id, operations);
    }

    public Operations Operations { get; }
}

public class Operations : Dictionary<LinkRelation, Operation>
{
    public Operation this[LinkRelation rel]
    {
        get { return base.ContainsKey(rel) ? base[rel] : null; }
    }
}

public class Operation
{
    public Operation(Resource resource)
    {
        Resource = resource;
        // Initialize the Request with Uri from Href, HttpMethod from Method, etc.
        Request = ...;
    }

    public Resource Resource { get; }
    public Uri Href { get; }
    public HttpMethod Method { get; }
    public LinkRelation Rel { get; }    
    public string ContentType { get; }
    public HttpRequest Request { get; }
}

public enum LinkRelation
{
    Cancel,
    Capture
}

This is now implemented.