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

Add method to retrieve transaction data #16

Merged
merged 1 commit into from
Dec 24, 2023
Merged
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
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!;
}