enjin / platform-csharp-sdk

SDK for connecting to and interacting with the Enjin Platform.
GNU Lesser General Public License v3.0
7 stars 8 forks source link

Expose FragmentParameters to correctly build requests with DispatchInput #29

Closed CliffCawley closed 5 months ago

CliffCawley commented 5 months ago

This allows you to get the FragmentParameters and add things like WithId() and WithEncodedData()

I.e. it allows the following extension method:

public static DispatchInputType SetupFromRequestObject<TRequest, TFragment>(this DispatchInputType dispatchInputType, GraphQlRequest<TRequest, TFragment> request, DispatchCall? call, out JsonDocument disposeDocument)
    where TRequest : GraphQlRequest<TRequest, TFragment>
    where TFragment : IGraphQlFragment

{
    if (request.FragmentParameters != null && request.FragmentParameters is TransactionFragment transactionFragment)
    {
        // Ensure that Id and EncodedData are included, because they're a requirement of the DispatchInput call
        transactionFragment
            .WithId()
            .WithEncodedData();
    }

    var options = new JsonSerializerOptions
    {
        Converters = { new BigIntegerJsonConverter() }
    };

    var jsonString = JsonSerializer.Serialize(request.VariablesWithoutTypes, options);
    disposeDocument = JsonDocument.Parse(jsonString);
    var jsonElement = disposeDocument.RootElement;
    return dispatchInputType.SetCall(call)
        .SetQuery(request.Compile())
        .SetVariables(jsonElement);
}

Which can be used to call DispatchAndTouch with the relevant request, such as:

 var reqDispatch = new DispatchAndTouch()
     .SetTankId(_settings.Value.EnjinPlatformFuelTankId)
     .SetRuleSetId(BigInteger.Parse(_settings.Value.EnjinPlatformFuelTankRuleSetId))
     .SetDispatch(new DispatchInputType()
         .SetupFromRequestObject(reqTransferBalance, DispatchCall.MultiTokens, out var disposeDocument)
     );

 reqDispatch.Fragment(new TransactionFragment().WithTransactionHash());
 var response = await _enjinPlatformService.Client.SendDispatchAndTouch(reqDispatch);
 disposeDocument.Dispose();

Perhaps something like this could be integrated into the SDK in the future, if it's desirable.

(It would also be nice to remove this dependency on a JsonDocument, but I haven't got the time to work on it)