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 #14

Merged
merged 15 commits into from
Mar 24, 2023

Conversation

christianhelle
Copy link
Owner

@christianhelle christianhelle commented Mar 24, 2023

This adds support for generating interfaces that returns Task<IApiResponse<T>> as the return type by introducing the --use-api-response CLI tool argument

The generated method in the interface looks something like this:

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

If the call to GetPetById() fails then the consumer can handle the negative response themselves 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
@christianhelle christianhelle linked an issue Mar 24, 2023 that may be closed by this pull request
@christianhelle christianhelle merged commit c39b3cc into main 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 this pull request may close these issues.

Add support for generating IApiResponse<T> as return types
1 participant