Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated models #11

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/trustly-client-net.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 40 additions & 7 deletions src/Client/JsonRpcFactory.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
using System;
using Trustly.Api.Domain.Base;
using Trustly.Api.Domain;

namespace Trustly.Api.Client
{
public class JsonRpcFactory
{
public JsonRpcRequest<TReqData> Create<TReqData>(TReqData requestData, string method, string uuid = null)
where TReqData : IRequestParamsData
public JsonRpcRequest<TReqAttr, TReqData, JsonRpcRequestParams<TReqAttr, TReqData>> Create<TReqAttr, TReqData>(TReqData requestData, string method, string uuid = null)
where TReqAttr : AbstractRequestDataAttributes
where TReqData : AbstractRequestData<TReqAttr>
{
return new JsonRpcRequest<TReqData>
return new JsonRpcRequest<TReqAttr, TReqData, JsonRpcRequestParams<TReqAttr, TReqData>>(method)
{
Method = method,
Version = 1.1,
Params = new RequestParams<TReqData>
Params = new JsonRpcRequestParams<TReqAttr, TReqData>
{
UUID = uuid ?? Guid.NewGuid().ToString(),
Data = requestData
}
};
}


public JsonRpcResponse<TAckData, ResponseResult<TAckData>> CreateResponse<TReqData, TAckData>(
JsonRpcNotification<TReqData, JsonRpcNotificationParams<TReqData>> request,
TAckData data
)
{
return new JsonRpcResponse<TAckData, ResponseResult<TAckData>>()
{
Result = new ResponseResult<TAckData>
{
Method = request.Method,
Data = data,
UUID = request.Params.UUID
}
};
}

public JsonRpcResponse<TAckData, ResponseResult<TAckData>> CreateResponse<TAckData>(
TAckData data,
string method,
string uuid
)
{
return new JsonRpcResponse<TAckData, ResponseResult<TAckData>>()
{
Result = new ResponseResult<TAckData>
{
Method = method,
Data = data,
UUID = uuid
}
};
}
}
}
32 changes: 2 additions & 30 deletions src/Client/JsonRpcSigner.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.IO;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Trustly.Api.Domain.Base;

namespace Trustly.Api.Client
{
Expand All @@ -25,20 +22,7 @@ public string CreatePlaintext(string serializedData, string method, string uuid)
return string.Format("{0}{1}{2}", method, uuid, serializedData);
}

public void Sign<TData>(JsonRpcRequest<TData> request)
where TData : IRequestParamsData
{
request.Params.Signature = this.CreateSignature(request.Method, request.Params.UUID, request.Params.Data);
}

public void Sign<TData>(JsonRpcResponse<TData> response)
where TData : IResponseResultData
{
response.Result.Signature = this.CreateSignature(response.GetMethod(), response.GetUUID(), response.GetData());
}

private string CreateSignature<TData>(string method, string uuid, TData data)
where TData : IData
public string CreateSignature(string method, string uuid, object data)
{
var serializedData = this._serializer.SerializeData(data);
var plainText = this.CreatePlaintext(serializedData, method, uuid);
Expand All @@ -54,19 +38,7 @@ private string CreateSignature<TData>(string method, string uuid, TData data)
return Convert.ToBase64String(signedBytes);
}

public bool Verify<TData>(JsonRpcRequest<TData> request)
where TData : IRequestParamsData
{
return this.Verify(request.Method, request.Params.UUID, request.Params.Signature, request.Params.Data);
}

public bool Verify<TData>(JsonRpcResponse<TData> response)
where TData : IResponseResultData
{
return this.Verify(response.GetMethod(), response.GetUUID(), response.GetSignature(), response.GetData());
}

private bool Verify(string method, string uuid, string expectedSignature, IData data)
public bool Verify<TData>(string method, string uuid, TData data, string expectedSignature)
{
var serializedResponseData = this._serializer.SerializeData(data);
var responsePlainText = this.CreatePlaintext(serializedResponseData, method, uuid);
Expand Down
36 changes: 13 additions & 23 deletions src/Client/NotificationArgs.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
using System;
using Trustly.Api.Domain.Base;
using System.Threading.Tasks;

namespace Trustly.Api.Client
{
public delegate Task NotificationResponseDelegate(string method, string uuid);
public delegate Task NotificationAckDelegate<TAckData>(TAckData data);
public delegate Task NotificationRespondDelegate(string stringBody);

public delegate Task NotificationFailResponseDelegate(string method, string uuid, string message);

public class NotificationArgs<TData>
where TData : IRequestParamsData
public class NotificationArgs<TNotificationData, TAckData>
{
public TData Data { get; }
public TNotificationData Data { get; }

private readonly string _method;
private readonly string _uuid;
internal string Method { get; private set; }
internal string UUID { get; private set; }

private readonly NotificationResponseDelegate _onOK;
private readonly NotificationFailResponseDelegate _onFailed;
internal NotificationAckDelegate<TAckData> Callback { get; private set; }

public NotificationArgs(TData data, string method, string uuid, NotificationResponseDelegate onOK, NotificationFailResponseDelegate onFailed)
public NotificationArgs(TNotificationData data, string method, string uuid, NotificationAckDelegate<TAckData> callback)
{
this.Data = data;

this._method = method;
this._uuid = uuid;

this._onOK = onOK;
this._onFailed = onFailed;
}
this.Method = method;
this.UUID = uuid;

public void RespondWithOK()
{
this._onOK(this._method, this._uuid);
this.Callback = callback;
}

public void RespondWithFailed(string message)
public void Respond(TAckData result)
{
this._onFailed(this._method, this._uuid, message);
this.Callback(result);
}
}
}
63 changes: 35 additions & 28 deletions src/Client/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,41 @@
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Trustly.Api.Domain.Base;

namespace Trustly.Api.Client
{
public class Serializer
{
public string SerializeData<TData>(TData data)
where TData : IData
public string SerializeData<TData>(TData data, bool silent = false)
{
var settings = new JsonSerializerSettings
JToken jsonObject;
if (data is JToken token)
{
NullValueHandling = NullValueHandling.Include
};
// If the value to serialize is already a JToken, then we will assume it is an object.
// We can also work on the actual exact response, and not rely on flaky JSON -> DTO -> JSON -> String conversion.
jsonObject = (JToken) token;
}
else
{
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};

if (silent)
{
// We ignore any error; useful for test cases or other uncertain scenarios.
settings.Error = (sender, e) =>
{
e.ErrorContext.Handled = true;
};
}

var jsonString = JsonConvert.SerializeObject(data, settings);
var jsonSerializer = JsonSerializer.Create(settings);
jsonObject = JObject.FromObject(data, jsonSerializer);
}

var jsonObject = JToken.Parse(jsonString);
var sb = new StringBuilder();

this.SerializeToken(jsonObject, sb, new string[0]);

return sb.ToString();
Expand All @@ -41,22 +57,6 @@ private bool SerializeToken(JToken token, StringBuilder sb, string[] propertyPat
}
else if (token is JValue value)
{
if (propertyPath[0].ToLower().Equals("attributes"))
{
if (value.Value == null)
{
// NOTE: Special consideration is made for 'attributes' properties.
// Documentation says that <null> should be regarded as <empty string>
// But it does not specify that a not included 'attribute' property
// is that same as it not existing at all.
// This is contrary to how 'data' properties work, since they are typesafe.
// But 'attributes' properties were not typesafe, just a map, in older code.
// This discrepancy shows its' head here, since this code is typesafe.
return false;
}
}

//sb.Append(propertyPath[propertyPath.Length - 1]);
sb.Append(value.Value<string>());
}
else if (token is JProperty property)
Expand All @@ -65,11 +65,18 @@ private bool SerializeToken(JToken token, StringBuilder sb, string[] propertyPat
propertyPath.CopyTo(newPath, 0);
newPath[newPath.Length - 1] = property.Name;

var propertyBuffer = new StringBuilder();
if (this.SerializeToken(property.Value, propertyBuffer, newPath))
if (property.Value.Type == JTokenType.Null)
{
sb.Append(property.Name);
sb.Append(propertyBuffer);
}
else
{
var propertyBuffer = new StringBuilder();
if (this.SerializeToken(property.Value, propertyBuffer, newPath))
{
sb.Append(property.Name);
sb.Append(propertyBuffer);
}
}
}
else
Expand Down
36 changes: 26 additions & 10 deletions src/Client/SettlementReportParser.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Trustly.Api.Domain.Requests;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Trustly.Api.Domain;

namespace Trustly.Api.Client
{
Expand All @@ -19,16 +15,34 @@
private readonly static Mapper NOOP_MAPPER = (row, value) => { };
private readonly Dictionary<string, Mapper> _mappers = new Dictionary<string, Mapper>();

class EnumHelper<T>
{
public T Value { get; set; }
}

public SettlementReportParser()
{
this._mappers.Add("accountname", (row, value) => row.AccountName = value);
this._mappers.Add("currency", (row, value) => row.Currency = value);
this._mappers.Add("messageid", (row, value) => row.MessageID = value);
this._mappers.Add("messageid", (row, value) => row.MessageId = value);
this._mappers.Add("orderid", (row, value) => row.OrderID = value);
this._mappers.Add("ordertype", (row, value) => row.OrderType = value);
this._mappers.Add("ordertype", (row, value) =>
{
row.OrderTypeString = value;
try
{
var json = $"{{\"value\": \"{value}\"}}";
var result = JsonConvert.DeserializeObject<EnumHelper<OrderType>>(json);
row.OrderType = result.Value;
}
catch (Exception ex)

Check warning on line 38 in src/Client/SettlementReportParser.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used

Check warning on line 38 in src/Client/SettlementReportParser.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used

Check warning on line 38 in src/Client/SettlementReportParser.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used

Check warning on line 38 in src/Client/SettlementReportParser.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used
{
row.OrderType = null;
}
});
this._mappers.Add("username", (row, value) => row.Username = value);
this._mappers.Add("fxpaymentcurrency", (row, value) => row.FxPaymentCurrency = value);
this._mappers.Add("settlementbankwithdrawalid", (row, value) => row.SettlementBankWithdrawalID = value);
this._mappers.Add("settlementbankwithdrawalid", (row, value) => row.SettlementBankWithdrawalId = value);
this._mappers.Add("externalreference", (row, value) => row.ExternalReference = value);
this._mappers.Add("extraref", (row, value) => row.ExternalReference = value);

Expand Down Expand Up @@ -64,12 +78,14 @@

this._mappers.Add("datestamp", (row, value) =>
{
/*
if (!DateTime.TryParse(value, out DateTime result))
{
throw new ArgumentException($"Could not convert value '{value}' into a DateTime");
}
*/

row.Datestamp = result;
row.Datestamp = value;
});
}

Expand Down
Loading
Loading