Skip to content

Commit

Permalink
Added ability to write response data directly to Stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebakken committed Sep 4, 2012
1 parent 7365eec commit ca4e7c8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 12 deletions.
25 changes: 23 additions & 2 deletions RestSharp.IntegrationTests/FileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FileTests
public void Handles_Binary_File_Download()
{
const string baseUrl = "http://localhost:8080/";
using(SimpleServer.Create(baseUrl, Handlers.FileHandler))
using (SimpleServer.Create(baseUrl, Handlers.FileHandler))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("Assets/Koala.jpg");
Expand All @@ -21,5 +21,26 @@ public void Handles_Binary_File_Download()
Assert.Equal(expected, response);
}
}

[Fact]
public void Writes_Response_To_Stream()
{
const string baseUrl = "http://localhost:8080/";
using (SimpleServer.Create(baseUrl, Handlers.FileHandler))
{
string tempFile = Path.GetTempFileName();
using (var writer = File.OpenWrite(tempFile))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("Assets/Koala.jpg");
request.ResponseWriter = (responseStream) => responseStream.CopyTo(writer);
var response = client.DownloadData(request);
Assert.Null(response);
}
var fromTemp = File.ReadAllBytes(tempFile);
var expected = File.ReadAllBytes(Environment.CurrentDirectory + "\\Assets\\Koala.jpg");
Assert.Equal(expected, fromTemp);
}
}
}
}
}
43 changes: 37 additions & 6 deletions RestSharp/Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using RestSharp.Extensions;

#if WINDOWS_PHONE
Expand Down Expand Up @@ -107,6 +107,10 @@ protected bool HasFiles
/// </summary>
public CookieContainer CookieContainer { get; set; }
/// <summary>
/// The method to use to write the response instead of reading into RawBytes
/// </summary>
public Action<Stream> ResponseWriter { get; set; }
/// <summary>
/// Collection of files to be sent with request
/// </summary>
public IList<HttpFile> Files { get; private set; }
Expand Down Expand Up @@ -326,7 +330,7 @@ private void WriteMultipartFormData(Stream requestStream)
WriteStringTo(requestStream, GetMultipartFooter());
}

private static void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
{
using (webResponse)
{
Expand All @@ -337,14 +341,41 @@ private static void ExtractResponseData(HttpResponse response, HttpWebResponse w
response.ContentType = webResponse.ContentType;
response.ContentLength = webResponse.ContentLength;
#if WINDOWS_PHONE
Stream responseStream = webResponse.GetResponseStream();
if (string.Equals(webResponse.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase))
response.RawBytes = new GZipStream(webResponse.GetResponseStream()).ReadAsBytes();
{
var gzStream = new GZipStream(responseStream);
if (ResponseWriter == null)
{
response.RawBytes = gzStream.ReadAsBytes();
}
else
{
ResponseWriter(gzStream);
}
}
else
response.RawBytes = webResponse.GetResponseStream().ReadAsBytes();
{
if (ResponseWriter == null)
{
response.RawBytes = responseStream.ReadAsBytes();
}
else
{
ResponseWriter(responseStream);
}
}
#else
response.RawBytes = webResponse.GetResponseStream().ReadAsBytes();
Stream responseStream = webResponse.GetResponseStream();
if (ResponseWriter == null)
{
response.RawBytes = responseStream.ReadAsBytes();
}
else
{
ResponseWriter(responseStream);
}
#endif
//response.Content = GetString(response.RawBytes);
response.StatusCode = webResponse.StatusCode;
response.StatusDescription = webResponse.StatusDescription;
response.ResponseUri = webResponse.ResponseUri;
Expand Down
9 changes: 5 additions & 4 deletions RestSharp/IHttp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;

namespace RestSharp
{
public interface IHttp
{
Action<Stream> ResponseWriter { get; set; }
CookieContainer CookieContainer { get; set; }
ICredentials Credentials { get; set; }
string UserAgent { get; set; }
Expand Down Expand Up @@ -62,9 +63,9 @@ public interface IHttp
HttpResponse Options();
HttpResponse Post();
HttpResponse Put();
HttpResponse Patch();
HttpResponse AsPost(string httpMethod);
HttpResponse AsGet(string httpMethod);
HttpResponse Patch();
HttpResponse AsPost(string httpMethod);
HttpResponse AsGet(string httpMethod);

IWebProxy Proxy { get; set; }
#endif
Expand Down
6 changes: 6 additions & 0 deletions RestSharp/IRestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using RestSharp.Serializers;

Expand All @@ -35,6 +36,11 @@ public interface IRestRequest
/// </summary>
ISerializer XmlSerializer { get; set; }

/// <summary>
/// Set this to write response to Stream rather than reading into memory.
/// </summary>
Action<Stream> ResponseWriter { get; set; }

/// <summary>
/// Container of all HTTP parameters to be passed with the request.
/// See AddParameter() for explanation of the types of parameters that can be passed
Expand Down
2 changes: 2 additions & 0 deletions RestSharp/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ private void ConfigureHttp(IRestRequest request, IHttp http)
{
http.CookieContainer = CookieContainer;

http.ResponseWriter = request.ResponseWriter;

// move RestClient.DefaultParameters into Request.Parameters
foreach(var p in DefaultParameters)
{
Expand Down
5 changes: 5 additions & 0 deletions RestSharp/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public class RestRequest : IRestRequest
/// </summary>
public ISerializer XmlSerializer { get; set; }

/// <summary>
/// Set this to write response to Stream rather than reading into memory.
/// </summary>
public Action<Stream> ResponseWriter { get; set; }

/// <summary>
/// Default constructor
/// </summary>
Expand Down

0 comments on commit ca4e7c8

Please sign in to comment.