Skip to content

Commit

Permalink
balance sdk project has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
asamir1 committed Jun 6, 2024
1 parent 94e526c commit 0c4243f
Show file tree
Hide file tree
Showing 19 changed files with 562 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/.idea/.idea.SubsBase.SDK/.idea/vcs.xml

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

18 changes: 18 additions & 0 deletions src/Balance/SubsBase.SDK.Balance.Test/BalanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace SubsBase.SDK.Balance.Test;

public class BalanceTests
{
[OneTimeSetUp]
public void Setup()
{

}

[Test]
public void Test1()
{


Assert.Pass();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SubsBase.SDK.Balance\SubsBase.SDK.Balance.csproj" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions src/Balance/SubsBase.SDK.Balance/Client/BalanceClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using SubsBase.Common.ApiClientHelper;
using SubsBase.SDK.Balance.Contracts;

namespace SubsBase.SDK.Balance.Client;

public class BalanceClient
{
private readonly ApiClient _apiClient;

public BalanceClient(IHttpClientFactory httpClientFactory)
{
_apiClient = new ApiClient(httpClientFactory, baseAddress: "");
}

public async Task<Result<BalanceInfo?>> CreateAsync(BalanceInfoNew balanceInfoNew)
{
var result = await _apiClient.PostAsync<BalanceInfoNew, BalanceInfo>(
uri: "",
request: balanceInfoNew);

return result;
}

public async Task<Result<BalanceInfo?>> GetAsync(Guid id)
{
var result = await _apiClient.GetAsync<BalanceInfo>(uri: $"{id}");

return result;
}

public async Task<Result<BalanceInfo?>> UpdateAsync(Guid id, BalanceInfoUpdate balanceInfoNew)
{
var result = await _apiClient.PutAsync<BalanceInfoUpdate, BalanceInfo>(
uri: $"{id}",
request: balanceInfoNew);

return result;
}
}
12 changes: 12 additions & 0 deletions src/Balance/SubsBase.SDK.Balance/Client/BalanceMovementClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace SubsBase.SDK.Balance.Client;

public class BalanceMovementClient
{
private readonly IHttpClientFactory _httpClientFactory;

public BalanceMovementClient(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}

}
11 changes: 11 additions & 0 deletions src/Balance/SubsBase.SDK.Balance/Contracts/BalanceDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SubsBase.SDK.Balance.Contracts;

public class BalanceDetails
{
public Guid Id { get; set; }
public decimal Amount { get; set; }
public DateTime? Expiry { get; set; }

public Guid BalanceId { get; set; }
public BalanceInfo? BalanceInfo { get; set; }
}
11 changes: 11 additions & 0 deletions src/Balance/SubsBase.SDK.Balance/Contracts/BalanceHold.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SubsBase.SDK.Balance.Contracts;

public class BalanceHold
{
public Guid Id { get; set; }
public decimal Amount { get; set; }
public DateTime? Expiry { get; set; }

public Guid BalanceId { get; set; }
public BalanceInfo? BalanceInfo { get; set; }
}
24 changes: 24 additions & 0 deletions src/Balance/SubsBase.SDK.Balance/Contracts/BalanceInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace SubsBase.SDK.Balance.Contracts;

public class BalanceInfo
{
public Guid Id { get; set; }
public string Unit { get; set; } = string.Empty;
public Dictionary<string, object>? Metadata { get; set; }

public List<BalanceMovement>? BalanceMovements { get; set; }
public List<BalanceDetails>? BalanceDetails { get; set; }
public List<BalanceHold>? BalanceHolds { get; set; }
}

public class BalanceInfoNew
{
public string Unit { get; set; } = string.Empty;
public Dictionary<string, object>? Metadata { get; set; }
}


public class BalanceInfoUpdate
{
public Dictionary<string, object>? Metadata { get; set; }
}
32 changes: 32 additions & 0 deletions src/Balance/SubsBase.SDK.Balance/Contracts/BalanceMovement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Text.Json.Serialization;

namespace SubsBase.SDK.Balance.Contracts;

public enum MovementType
{
Debit,
Credit
}

public class BalanceMovement
{
public Guid Id { get; set; }
[JsonConverter(typeof(JsonStringEnumConverter))] public MovementType Type { get; set; }
public decimal Amount { get; set; }
public DateTime Timestamp { get; set; }
public DateTime? Expiry { get; set; }
public string Description { get; set; } = string.Empty;

public Guid BalanceId { get; set; }
public BalanceInfo? BalanceInfo { get; set; }
}


public class BalanceMovementNew
{
[JsonConverter(typeof(JsonStringEnumConverter))] public MovementType Type { get; set; }
public decimal Amount { get; set; }
public DateTime? Expiry { get; set; }
public string Description { get; set; } = string.Empty;
public Guid BalanceId { get; set; }
}
14 changes: 14 additions & 0 deletions src/Balance/SubsBase.SDK.Balance/Install.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.DependencyInjection;
using SubsBase.SDK.Balance.Client;

namespace SubsBase.SDK.Balance;

public static class Install
{
public static IServiceCollection AddBalanceServices(this IServiceCollection services)
{
services.AddTransient<BalanceClient>();

return services;
}
}
14 changes: 14 additions & 0 deletions src/Balance/SubsBase.SDK.Balance/SubsBase.SDK.Balance.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Shared\SubsBase.Common.ApiClientHelper\SubsBase.Common.ApiClientHelper.csproj" />
<ProjectReference Include="..\Shared\SubsBase.Common.ApiClientHelper\SubsBase.Common.ApiClientHelper.csproj" />
</ItemGroup>

</Project>
91 changes: 91 additions & 0 deletions src/Shared/SubsBase.Common.ApiClientHelper/ApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Text;
using System.Text.Json;

namespace SubsBase.Common.ApiClientHelper;

public partial class ApiClient
{
private readonly HttpClient _httpClient;

public ApiClient(IHttpClientFactory httpClientFactory, string baseAddress)
{
_httpClient = httpClientFactory.CreateClient();
_httpClient.BaseAddress = new Uri(baseAddress);
}

public async Task<Result<TResult?>> ResponseAsync<TRequest, TResult>(
string uri,
HttpMethod httpMethod,
TRequest? request = null,
string? mediaType = null,
Encoding? encoding = null,
Dictionary<string, string>? headers = null,
JsonSerializerOptions? jsonSerializerOptions = null)
where TResult : class
where TRequest : class
{
mediaType ??= "application/json";
encoding ??= Encoding.UTF8;

var content = request != null ? JsonSerializer.Serialize(request, jsonSerializerOptions) : "";

var httpRequest = new HttpRequestMessage(httpMethod, uri);
httpRequest.Content = new StringContent(content, encoding, mediaType);

if (headers != null)
{
foreach (var header in headers)
{
if (_httpClient.DefaultRequestHeaders.Contains(header.Key))
{
_httpClient.DefaultRequestHeaders.Remove(header.Key);
}

_httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}

var httpResponseMessage = await _httpClient.SendAsync(httpRequest);

var stringResponse = await httpResponseMessage.Content.ReadAsStringAsync();

if (!httpResponseMessage.IsSuccessStatusCode) return Result.Fail<TResult?>(stringResponse);

var result = JsonSerializer.Deserialize<TResult>(stringResponse, jsonSerializerOptions);

return Result.Ok(result);
}

public async Task<Result<TResult?>> ResponseAsync<TResult>(
string uri,
HttpMethod httpMethod,
Dictionary<string, string>? headers = null,
JsonSerializerOptions? jsonSerializerOptions = null)
where TResult : class
{
var httpRequest = new HttpRequestMessage(httpMethod, uri);

if (headers != null)
{
foreach (var header in headers)
{
if (_httpClient.DefaultRequestHeaders.Contains(header.Key))
{
_httpClient.DefaultRequestHeaders.Remove(header.Key);
}

_httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}

var httpResponseMessage = await _httpClient.SendAsync(httpRequest);

var stringResponse = await httpResponseMessage.Content.ReadAsStringAsync();

if (!httpResponseMessage.IsSuccessStatusCode) return Result.Fail<TResult?>(stringResponse);

var result = JsonSerializer.Deserialize<TResult>(stringResponse, jsonSerializerOptions);

return Result.Ok(result);
}
}
40 changes: 40 additions & 0 deletions src/Shared/SubsBase.Common.ApiClientHelper/DeleteHttpMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Text;
using System.Text.Json;

namespace SubsBase.Common.ApiClientHelper;

public partial class ApiClient
{
public async Task<Result<TResult?>> DeleteAsync<TRequest, TResult>(
string uri,
TRequest? request = null,
string? mediaType = null,
Encoding? encoding = null,
Dictionary<string, string>? headers = null,
JsonSerializerOptions? jsonSerializerOptions = null)
where TResult : class
where TRequest : class
{
return await ResponseAsync<TRequest, TResult>(
uri,
httpMethod: HttpMethod.Delete,
request,
mediaType,
encoding,
headers,
jsonSerializerOptions);
}

public async Task<Result<TResult?>> DeleteAsync<TResult>(
string uri,
Dictionary<string, string>? headers = null,
JsonSerializerOptions? jsonSerializerOptions = null)
where TResult : class
{
return await ResponseAsync<TResult>(
uri,
httpMethod: HttpMethod.Delete,
headers,
jsonSerializerOptions);
}
}
Loading

0 comments on commit 0c4243f

Please sign in to comment.