GenerateAspNetCoreClient icon indicating copy to clipboard operation
GenerateAspNetCoreClient copied to clipboard

Add a flag to replace Task with IObservable

Open obsean opened this issue 5 years ago • 4 comments

Refit allows a return type if Task or IObservable. It would be nice if there was a flag to replace Tasks with Iobservables

obsean avatar Apr 19 '21 10:04 obsean

I haven't really worked with Observables with Refit. Could you briefly explain in which cases IObservable return type would be more beneficial comparing to Task?

Dreamescaper avatar Apr 19 '21 11:04 Dreamescaper

Generally if you are working with the reactive extensions then it is preferable to use IObservable, more for consistencies sake then anything else. So when using ReactiveUI, this would be preferable.

the change would be that instead of the:

`//

using System.Collections.Generic; using System.Threading.Tasks; using Refit; using Shared;

namespace Api.Client { public interface IWeatherForecastApi { [Get("/WeatherForecast")] Task<IEnumerable<WeatherForecast>> Get(); } }`

it should generate this:

`//

using System; using System.Collections.Generic; using Refit; using Shared;

namespace Api.Client { public interface IWeatherForecastApi { [Get("/WeatherForecast")] IObservable<IEnumerable<WeatherForecast>> Get(); } }`

obsean avatar Apr 19 '21 12:04 obsean

Sorry for delay! There is a couple of other similar cases, e.g. to use Task<IApiResponse<T>> or Task<HttpResponseMessage>.

So I'm thinking, maybe it would be better to parametrize it? Add two parameters - void-response-type and non-void-response-type. In your case, something like void-response-type "IObservable<{T}>" non-void-response-type "IObservable<HttpResponseMessage>"

Would that help?

Dreamescaper avatar May 15 '21 21:05 Dreamescaper

Good catch!

Looking at the samples I think these are the only return types:

Task
Task<HttpResponseMessage>
Task<T>
Task<ApiResponse<T>>
Observable<HttpResponseMessage>
Observable<T>
Observable<ApiResponse<T>>

Note that there is no generic System.Observable

I think that the best way of representing this would be with 3 switches, something like:

--return-observables --return-httpresponsemessage-for-void --return-apiresponse-for-non-void

Note that --return-observables will always assume --return-httpresponsemessage-for-void

you might also want to combine the last 2, because for example a method that returns Task will throw on a failure while a method that returns Task<HttpResponseMessage> will not, so mixing the 2 may not make sense

Hope this makes sense and thanks for your time

obsean avatar May 16 '21 09:05 obsean