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 support for generating IApiResponse<T> as return types #13

Closed
christianhelle opened this issue Mar 24, 2023 · 0 comments · Fixed by #14
Closed

Add support for generating IApiResponse<T> as return types #13

christianhelle opened this issue Mar 24, 2023 · 0 comments · Fixed by #14
Assignees
Labels
enhancement New feature, bug fix, or request

Comments

@christianhelle
Copy link
Owner

christianhelle commented Mar 24, 2023

Add support for generating interfaces that returns Task<IApiResponse<T>> as the return type

Like this:

[Get("/pet/{petId}")]
Task<IApiResponse<Pet>> GetPetById(long petId);

So that if the call to GetPetById() fails then we can handle the negative response ourselves instead of catching an ApiException

var client = RestService.For<ISwaggerPetstore>("https://petstore3.swagger.io/api/v3");
var response = await client.GetPetById(2);

if (!response. IsSuccessStatusCode)
{
    // Do something about the failed response
}

var pet = response.Content;

The IApiResponse<T> interface comes from Refit and is defined as

/// <inheritdoc/>
public interface IApiResponse<out T> : IApiResponse
{
    /// <summary>
    /// Deserialized request content as <typeparamref name="T"/>.
    /// </summary>
    T? Content { get; }
}

/// <summary>
/// Base interface used to represent an API response.
/// </summary>
public interface IApiResponse : IDisposable
{
    /// <summary>
    /// HTTP response headers.
    /// </summary>
    HttpResponseHeaders Headers { get; }

    /// <summary>
    /// HTTP response content headers as defined in RFC 2616.
    /// </summary>
    HttpContentHeaders? ContentHeaders { get; }

    /// <summary>
    /// Indicates whether the request was successful.
    /// </summary>
    bool IsSuccessStatusCode { get; }

    /// <summary>
    /// HTTP response status code.
    /// </summary>
    HttpStatusCode StatusCode { get; }

    /// <summary>
    /// The reason phrase which typically is sent by the server together with the status code.
    /// </summary>
    string? ReasonPhrase { get; }

    /// <summary>
    /// The HTTP Request message which led to this response.
    /// </summary>
    HttpRequestMessage? RequestMessage { get; }

    /// <summary>
    /// HTTP Message version.
    /// </summary>
    Version Version { get; }

    /// <summary>
    /// The <see cref="ApiException"/> object in case of unsuccessful response.
    /// </summary>
    ApiException? Error { get; }
}
@christianhelle christianhelle added the enhancement New feature, bug fix, or request label Mar 24, 2023
@christianhelle christianhelle self-assigned this Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, bug fix, or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant