Dreamescaper / GenerateAspNetCoreClient

DotNet tool to generate HTTP client classes from ASP.NET Core api controllers.
MIT License
63 stars 5 forks source link

Add a flag to replace Task with IObservable #3

Open obsean opened 3 years ago

obsean commented 3 years ago

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

Dreamescaper commented 3 years ago

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?

obsean commented 3 years ago

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> 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> Get(); } }`

Dreamescaper commented 3 years ago

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?

obsean commented 3 years ago

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 will not, so mixing the 2 may not make sense

Hope this makes sense and thanks for your time