Dreamescaper / GenerateAspNetCoreClient

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

Download (FileStreamResult) file endpoint generation #6

Closed valentasm1 closed 2 years ago

valentasm1 commented 2 years ago

Tested with 0.6v I have

[HttpGet("test-file")]
public async Task<FileContentResult> TestFile()
{
    var bb = new byte[1];
    return File(bb, "application/pdf");
}

when i generate i get

[Get("/test-file")]
Task TestFile();

I expect to get

Task<HttpContent> TestFile();

My idea to be able download file via refit. Maybe i am doing wrong. Inspired by https://stackoverflow.com/questions/42141274/refit-c-downloading-image

Dreamescaper commented 2 years ago

Yeah, it is caused by this bug: https://github.com/dotnet/aspnetcore/issues/30465. I can probably add some workaround... I can see that Refit supports returning Task as well. WDYT, would that be better?

valentasm1 commented 2 years ago

I need to use refit in middle service where i get file via refit and forward it. Returning task wouldnt work in my case. If it takes much work, then no needed. I will use old but gold pure httpclient ;).

Dreamescaper commented 2 years ago

Oh, my bad. I meant returning Stream :)

valentasm1 commented 2 years ago

Oh yea. Stream would be ideal solution.

Dreamescaper commented 2 years ago

I have published version 0.7, which has a fix for this. Could you check it?

valentasm1 commented 2 years ago

It generated good result just not sure how refit behaves. Not sure if i will have enough time to check today. If not then only on monday.

valentasm1 commented 2 years ago

It didnt worked, since refit tries deserialize json. I havent checked but it is my assumptions. I did this.

[HttpGet("test-file")]
[ProducesResponseType(typeof(HttpContent), (int)HttpStatusCode.OK)]
public async Task<FileStreamResult> TestFile()
{
var bb = new byte[1];
return File(bb, "application/pdf");
}

Get this

Task<HttpContent> TestFile();

Then call refit like this and it worked

var data = await _someApi.TestFile();
var stream = await data.ReadAsStreamAsync();
return File(stream, "application/pdf");

So i assume we could close issue

Dreamescaper commented 2 years ago

I have created a small Refit test with Stream response, and it seems to work fine. I'm wondering what went wrong for you with Task return type.

using Refit;

var client = RestService.For<IExampleApi>("https://filesamples.com/");
var stream = await client.DownloadFile();

using (var fileStream = File.Create("download.jpg"))
{
    stream.CopyTo(fileStream);
}

interface IExampleApi
{
    [Get("/samples/image/jpg/sample_640%C3%97426.jpg")]
    Task<Stream> DownloadFile();
}
valentasm1 commented 2 years ago

True. This one works too.