Skip to content

Commit

Permalink
Add ReadBody and HttpCompletionOption request options
Browse files Browse the repository at this point in the history
  • Loading branch information
matteocontrini committed Jun 18, 2020
1 parent 808f9e4 commit ce0dded
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
27 changes: 13 additions & 14 deletions PlainHttp/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public class HttpRequest : IHttpRequest

public Encoding ResponseEncoding { get; set; }

public HttpCompletionOption HttpCompletionOption { get; set; } = HttpCompletionOption.ResponseContentRead;

public bool ReadBody { get; set; } = true;

private static AsyncLocal<TestingMode> testingMode
= new AsyncLocal<TestingMode>();

Expand All @@ -60,7 +64,7 @@ public HttpRequest(string url)
this.Uri = new Uri(url);
}

public async Task<HttpResponse> SendAsync(CancellationToken cancellationToken = default(CancellationToken))
public async Task<IHttpResponse> SendAsync(CancellationToken cancellationToken = default(CancellationToken))
{
if (testingMode.Value != null)
{
Expand Down Expand Up @@ -128,7 +132,7 @@ public HttpRequest(string url)
}

// Send the request
responseMessage = await client.SendAsync(requestMessage, cts.Token).ConfigureAwait(false);
responseMessage = await client.SendAsync(requestMessage, this.HttpCompletionOption, cts.Token).ConfigureAwait(false);

// Wrap the content into an HttpResponse instance,
// also reading the body (string or file)
Expand All @@ -145,24 +149,19 @@ public HttpRequest(string url)
}
}

private async Task<HttpResponse> CreateHttpResponse(HttpResponseMessage responseMessage)
private async Task<IHttpResponse> CreateHttpResponse(HttpResponseMessage responseMessage)
{
// No file name, given read the body of the response as string
// No file name given, read the body of the response as a string
if (this.DownloadFileName == null)
{
string body;
IHttpResponse response = new HttpResponse(this, responseMessage);

if (this.ResponseEncoding != null)
{
byte[] array = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
body = this.ResponseEncoding.GetString(array);
}
else
if (this.ReadBody)
{
body = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
await response.ReadBody().ConfigureAwait(false);
}

return new HttpResponse(this, responseMessage, body);
return response;
}
// Copy the response to a file
else
Expand All @@ -177,7 +176,7 @@ private async Task<HttpResponse> CreateHttpResponse(HttpResponseMessage response
}
}

private async Task<HttpResponse> MockedResponse()
private async Task<IHttpResponse> MockedResponse()
{
// Get the testing mode instance for this async context
HttpResponseMessage message = testingMode.Value.RequestsQueue.Dequeue();
Expand Down
14 changes: 14 additions & 0 deletions PlainHttp/HttpResponse.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace PlainHttp
{
Expand Down Expand Up @@ -48,5 +49,18 @@ public string GetSingleHeader(string name)
return null;
}
}

public async Task ReadBody()
{
if (this.Request.ResponseEncoding != null)
{
byte[] array = await Message.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
this.Body = this.Request.ResponseEncoding.GetString(array);
}
else
{
this.Body = await this.Message.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
}
}
4 changes: 3 additions & 1 deletion PlainHttp/IHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public interface IHttpRequest
Uri Proxy { get; set; }
TimeSpan Timeout { get; set; }
Uri Uri { get; set; }
HttpCompletionOption HttpCompletionOption { get; set; }
bool ReadBody { get; set; }

Task<HttpResponse> SendAsync(CancellationToken cancellationToken = default(CancellationToken));
Task<IHttpResponse> SendAsync(CancellationToken cancellationToken = default(CancellationToken));
}
}
2 changes: 2 additions & 0 deletions PlainHttp/IHttpResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
using System.Threading.Tasks;

namespace PlainHttp
{
Expand All @@ -10,5 +11,6 @@ public interface IHttpResponse
bool Succeeded { get; }

string GetSingleHeader(string name);
Task ReadBody();
}
}
2 changes: 1 addition & 1 deletion PlainHttp/PlainHttp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>7.3</LangVersion>
<VersionPrefix>1.0.3</VersionPrefix>
<VersionPrefix>1.1.0</VersionPrefix>
<Description>Easy HTTP client with support for serialization, proxies, testing and more </Description>
<RepositoryUrl>https://github.com/matteocontrini/PlainHttp</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down

0 comments on commit ce0dded

Please sign in to comment.