Skip to content

Commit

Permalink
thunderpay-sdk-cs: Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
krishpranav committed Jan 19, 2025
1 parent 98e7c57 commit 4bf75f6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
83 changes: 71 additions & 12 deletions sdks/thunderpay-sdk-cs/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,52 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using Newtonsoft.Json;
using System.Dynamic;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Collections;
using Newtonsoft.Json;

namespace ThunderPay
{
public abstract class Resource
{
[ResourceField(seialize = false)]
[ResourceField(serialize = false)]
public string href
{
get; set;
}
} // public string href

[ResourceField(serialize = false)]
public string id
{
get; set;
}
} // public string id

[ResourceField]
public Dictionary<string, string> links
{
get; set;
}
} // public Dictionary<string, string> links

[ResourceField]
public Dictionary<string, string> meta
{
get; set;
} // public Dictionary<string, string> meta

[ResourceField(serialize = false)]
public DateTime created_at
{
get; set;
} // public DateTime created_at

[ResourceField(serialize = false)]
public DateTime updated_at
{
get; set;
} // public DateTime updaetd_at

public Resource() { }

Expand All @@ -54,12 +71,54 @@ public void Save<T>()
}
else
{
string href = this.GetType().GetProperties("resource_href").GetValue(this).ToString();
string href = this.GetType().GetProperty("resource_href").GetValue(this).ToString();
res = Client.Post<T>(href, Serialize(this));
}

UpdateResource<T>(res);
}
}
} // public void Save<T>()

public void Unstore()
{
Client.Delete(this.href);
} // publid void Unstore()

public static T Fetch<T>(string href)
{
return Client.Get<T>(href);
} // public static T Fetch<T>(string href)

public void Reload<T>()
{
dynamic res = Client.Get<T>(href);
UpdateResource<T>(res);
} // publid void Reload<T>()

public void UpdateResource<T>(dynamic res)
{
Type resType = this.GetType();
List<PropertyInfo> fields = resType.GetProperties().ToList();

foreach (PropertyInfo f in fields)
{
string propName = f.Name;
if (f.Name.Equals("resource_href"))
continue;
PropertyInfo propToCopy = res.GetType().GetProperty(propName);
object propValue = propToCopy.GetValue(res);

f.SetValue(this, propValue);
}
} // public void UpdateResource<T>(dynamic res)

}
public static string Serialize(object resource)
{
return JsonConvert.SerializeObject(resource,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new AllPropertiesResolver()
});
} // public static string Serialize(object resource)
} // public abstract class Resource
} // namespace ThunderPay
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[assembly: System.Reflection.AssemblyCompanyAttribute("thunderpay-sdk-cs")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+a2b25f7099d797eb3b6a14b72c669ddbd23f893d")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+98e7c57081cbc5ad8474a27db23593819e6cb71b")]
[assembly: System.Reflection.AssemblyProductAttribute("thunderpay-sdk-cs")]
[assembly: System.Reflection.AssemblyTitleAttribute("thunderpay-sdk-cs")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5035e2da64ffff0315c4e7709b49c057df41c37c0d95b348a217b69475677c26
4c8d7999ad4188bb686fd945a555d56f2637fc3d90af4c7f0e82d1b4d0e9da55

0 comments on commit 4bf75f6

Please sign in to comment.