Skip to content

Commit

Permalink
Enable automatic decompresion of GZIP
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
Krusen committed Mar 11, 2020
1 parent 6cc29cf commit b63c5a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
8 changes: 4 additions & 4 deletions PinSharp/Http/FormUrlEncodedContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace PinSharp.Http
{
internal class FormUrlEncodedContent : ByteArrayContent
public class FormUrlEncodedContent : ByteArrayContent
{
private static readonly Encoding DefaultHttpEncoding = Encoding.GetEncoding("ISO-8859-1");

Expand All @@ -16,7 +16,7 @@ public FormUrlEncodedContent(IEnumerable<KeyValuePair<string, string>> nameValue
Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
}

private static byte[] GetContentByteArray(IEnumerable<KeyValuePair<string, string>> nameValueCollection)
protected static byte[] GetContentByteArray(IEnumerable<KeyValuePair<string, string>> nameValueCollection)
{
if (nameValueCollection == null) throw new ArgumentNullException(nameof(nameValueCollection));

Expand All @@ -32,15 +32,15 @@ private static byte[] GetContentByteArray(IEnumerable<KeyValuePair<string, strin
return DefaultHttpEncoding.GetBytes(stringBuilder.ToString());
}

private static string Encode(string data)
protected static string Encode(string data)
{
if (string.IsNullOrEmpty(data))
return "";

return EscapeLongDataString(data);
}

private static string EscapeLongDataString(string data)
protected static string EscapeLongDataString(string data)
{
// Uri.EscapeDataString() does not support strings longer than this
const int maxLength = 65519;
Expand Down
29 changes: 20 additions & 9 deletions PinSharp/Http/UrlEncodedHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using PinSharp.Extensions;

namespace PinSharp.Http
{
internal class UrlEncodedHttpClient : IHttpClient
public class UrlEncodedHttpClient : IHttpClient
{
private HttpClient Client { get; }

public UrlEncodedHttpClient(string baseAddress, string accessToken)
{
Client = new HttpClient();
Client.BaseAddress = new Uri(baseAddress);
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var handler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};

Client = new HttpClient(handler)
{
BaseAddress = new Uri(baseAddress),
DefaultRequestHeaders =
{
Authorization = new AuthenticationHeaderValue("Bearer", accessToken)
}
};
}

public Task<HttpResponseMessage> GetAsync(string requestUri)
public virtual Task<HttpResponseMessage> GetAsync(string requestUri)
{
return Client.GetAsync(requestUri);
}

public Task<HttpResponseMessage> PostAsync<T>(string requestUri, T value)
public virtual Task<HttpResponseMessage> PostAsync<T>(string requestUri, T value)
{
var content = GetFormUrlEncodedContent(value);
return Client.PostAsync(requestUri, content);
}

public Task<HttpResponseMessage> PatchAsync<T>(string requestUri, T value)
public virtual Task<HttpResponseMessage> PatchAsync<T>(string requestUri, T value)
{
var request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri);
request.Headers.ExpectContinue = false;
request.Content = GetFormUrlEncodedContent(value);
return Client.SendAsync(request);
}

public Task<HttpResponseMessage> DeleteAsync(string requestUri)
public virtual Task<HttpResponseMessage> DeleteAsync(string requestUri)
{
return Client.DeleteAsync(requestUri);
}

private static FormUrlEncodedContent GetFormUrlEncodedContent(object obj)
protected static FormUrlEncodedContent GetFormUrlEncodedContent(object obj)
{
// TODO: Add attribute to ignore property?
var data =
Expand Down

0 comments on commit b63c5a0

Please sign in to comment.