2024-06-27 23:26:53 -05:00
|
|
|
using AnthropicClient.Models;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents the result of an Anthropic API operation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The type of the result value.</typeparam>
|
|
|
|
|
public class AnthropicResult<T>
|
|
|
|
|
{
|
2024-07-06 11:55:47 -05:00
|
|
|
private T _value = default!;
|
|
|
|
|
|
2024-06-27 23:26:53 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// The value of the result.
|
|
|
|
|
/// </summary>
|
2024-07-06 11:55:47 -05:00
|
|
|
/// <exception cref="InvalidOperationException">Thrown when the result is not successful.</exception>
|
|
|
|
|
public T Value
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return IsSuccess ? _value : throw new InvalidOperationException("The result is not successful. Check the error property for more information.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private set
|
|
|
|
|
{
|
|
|
|
|
_value = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AnthropicError _error = default!;
|
2024-06-27 23:26:53 -05:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The error of the result.
|
|
|
|
|
/// </summary>
|
2024-07-06 11:55:47 -05:00
|
|
|
/// <exception cref="InvalidOperationException">Thrown when the result is successful.</exception>
|
|
|
|
|
public AnthropicError Error
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return IsSuccess ? throw new InvalidOperationException("The result is successful. Check the value property for more information.") : _error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private set
|
|
|
|
|
{
|
|
|
|
|
_error = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-27 23:26:53 -05:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates whether the operation was successful.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsSuccess { get; }
|
|
|
|
|
|
2024-07-06 11:55:47 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates whether the operation failed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsFailure => !IsSuccess;
|
|
|
|
|
|
2024-06-27 23:26:53 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// The request ID of the operation.
|
|
|
|
|
/// </summary>
|
2024-06-28 16:30:09 -05:00
|
|
|
public AnthropicHeaders Headers { get; }
|
2024-06-27 23:26:53 -05:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="AnthropicResult{T}"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">The value of the result.</param>
|
|
|
|
|
/// <param name="error">The error of the result.</param>
|
|
|
|
|
/// <param name="isSuccess">Indicates whether the operation was successful.</param>
|
2024-06-28 16:30:09 -05:00
|
|
|
/// <param name="headers">The Anthropic headers for the result.</param>
|
2024-06-27 23:26:53 -05:00
|
|
|
/// <returns>A new instance of the <see cref="AnthropicResult{T}"/> class.</returns>
|
2024-06-28 16:30:09 -05:00
|
|
|
protected AnthropicResult(T value, AnthropicError error, bool isSuccess, AnthropicHeaders headers)
|
2024-06-27 23:26:53 -05:00
|
|
|
{
|
|
|
|
|
Value = value;
|
|
|
|
|
Error = error;
|
|
|
|
|
IsSuccess = isSuccess;
|
2024-06-28 16:30:09 -05:00
|
|
|
Headers = headers;
|
2024-06-27 23:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a successful result.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">The value of the result.</param>
|
2024-06-28 16:30:09 -05:00
|
|
|
/// <param name="headers">The Anthropic headers for the result.</param>
|
2024-06-27 23:26:53 -05:00
|
|
|
/// <returns>A new instance of the <see cref="AnthropicResult{T}"/> class.</returns>
|
2024-06-28 16:30:09 -05:00
|
|
|
public static AnthropicResult<T> Success(T value, AnthropicHeaders headers)
|
2024-06-27 23:26:53 -05:00
|
|
|
{
|
2024-06-28 16:30:09 -05:00
|
|
|
return new AnthropicResult<T>(value, null!, true, headers);
|
2024-06-27 23:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a failed result.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="error">The error of the result.</param>
|
2024-06-28 16:30:09 -05:00
|
|
|
/// <param name="headers">The Anthropic headers for the result.</param>
|
2024-06-27 23:26:53 -05:00
|
|
|
/// <returns>A new instance of the <see cref="AnthropicResult{T}"/> class.</returns>
|
2024-06-28 16:30:09 -05:00
|
|
|
public static AnthropicResult<T> Failure(AnthropicError error, AnthropicHeaders headers)
|
2024-06-27 23:26:53 -05:00
|
|
|
{
|
2024-06-28 16:30:09 -05:00
|
|
|
return new AnthropicResult<T>(default!, error, false, headers);
|
2024-06-27 23:26:53 -05:00
|
|
|
}
|
2024-07-01 22:46:02 -05:00
|
|
|
}
|