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

Consider FileStreamResponse Handler #64

Open
DalSoft opened this issue Nov 14, 2018 · 0 comments
Open

Consider FileStreamResponse Handler #64

DalSoft opened this issue Nov 14, 2018 · 0 comments

Comments

@DalSoft
Copy link
Owner

DalSoft commented Nov 14, 2018

public class FileStreamHandler : DelegatingHandler
    {
        public static string FileStreamHandlerKey = nameof(FileStreamHandlerKey);
        
        
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            return await base.SendAsync(request, cancellationToken)
                .ContinueWith(task =>
                {
                    var response = task.Result;

                    if (response.Content.Headers.Contains("Content-Disposition"))
                    {
                        request.Properties[FileStreamHandlerKey] = response.Content.ReadAsStreamAsync().Result;
                        response.Content = null;
                    }

                    return response;

                }, cancellationToken).ConfigureAwait(false); // Next in the pipeline
        }
    }




        [Test]
        public async Task Stream_UsingHttpResponseMessage_CorrectlyStreams()
        {
            dynamic restClient = new RestClient("https://github.com", new Config(new FileStreamHandler()));

            HttpResponseMessage response = await restClient
                .Headers(new { Accept ="application/octet-stream" })
                .Resource("tugberkugurlu/ASPNETWebAPISamples/archive/master.zip").Get();

            var savePath = Path.GetTempPath() + "master.zip";
            var stream = response.RequestMessage.Properties[FileStreamHandler.FileStreamHandlerKey] as Stream;
            
            if (stream == null)
                return;

            using (var fileStream = File.Create(savePath))
            {
                stream.CopyTo(fileStream);
            }
        }
@DalSoft DalSoft added this to Backlog in RestClient 4.0 Jan 11, 2019
@DalSoft DalSoft added this to To do in RestClient 4.1 Jun 25, 2019
@DalSoft DalSoft removed this from Backlog in RestClient 4.0 Jun 25, 2019
@DalSoft DalSoft removed this from To do in RestClient 4.1 Aug 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant