-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
562 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Balance/SubsBase.SDK.Balance.Test/SubsBase.SDK.Balance.Test.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/Balance/SubsBase.SDK.Balance/Client/BalanceMovementClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
src/Balance/SubsBase.SDK.Balance/Contracts/BalanceDetails.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/Balance/SubsBase.SDK.Balance/Contracts/BalanceMovement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/Balance/SubsBase.SDK.Balance/SubsBase.SDK.Balance.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
src/Shared/SubsBase.Common.ApiClientHelper/DeleteHttpMethod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.