Skip to content

Commit

Permalink
Add method to retrieve transaction data (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
illunix authored Dec 24, 2023
1 parent 51557e1 commit a0e0bfe
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Axepta.SDK/Entities/Response/TransactionResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Axepta.SDK;

public sealed record TransactionResponse
{
[JsonPropertyName("data")]
public required Data Data { get; init; }
}
33 changes: 33 additions & 0 deletions src/Axepta.SDK/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,39 @@ IConfiguration cfg
return services;
}

internal static async Task<T> GetAsync<T>(
this HttpClient http,
string url,
CancellationToken ct = default
)
{
HttpResponseMessage? httpRes = null;

var httpResContentAsJson = async () => httpRes is null ?
string.Empty :
await httpRes.Content.ReadAsStringAsync(ct);

try
{
httpRes = await http.GetAsync(
url,
ct
);

httpRes.EnsureSuccessStatusCode();

return (T)JsonSerializer.Deserialize(
await httpResContentAsJson().ConfigureAwait(false),
typeof(T),
_sourceGenOptions
)!;
}
catch (HttpRequestException)
{
throw new AxeptaException(await httpResContentAsJson().ConfigureAwait(false));
}
}

internal static async Task<K> PostAsync<T, K>(
this HttpClient http,
string url,
Expand Down
13 changes: 13 additions & 0 deletions src/Axepta.SDK/Services/Abstractions/IAxepta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ Task<ResponseRoot> CreateRefundAsync(
Refund refund,
CancellationToken ct = default
);

/// <summary>
/// Retrieves the details of a specific transaction asynchronously.
/// </summary>
/// <param name="transactionId">The unique identifier of the transaction to retrieve.</param>
/// <param name="ct">An optional cancellation token to cancel the request.</param>
/// <returns>
/// A task that represents the asynchronous operation. The task result contains the <see cref="Transaction"/> object with the details of the requested transaction.
/// </returns>
Task<Transaction> GetTransactionAsync(
Guid transactionId,
CancellationToken ct = default
);
}
9 changes: 9 additions & 0 deletions src/Axepta.SDK/Services/Axepta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ public Task<ResponseRoot> CreateRefundAsync(
refund,
ct
);

public async Task<Transaction> GetTransactionAsync(
Guid transationId,
CancellationToken ct = default
)
=> (await http.GetAsync<TransactionResponse>(
$"transaction/{transationId}",
ct
)).Data.Transaction!;
}

0 comments on commit a0e0bfe

Please sign in to comment.