Skip to content

Commit

Permalink
Merge pull request #38 from hanssens/vnext
Browse files Browse the repository at this point in the history
Preparing for release v2.1.0
  • Loading branch information
hanssens authored May 30, 2017
2 parents f872d40 + 8101f34 commit aae32eb
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 107 deletions.
6 changes: 3 additions & 3 deletions src/Hanssens.Net/Hanssens.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<PackageProjectUrl>https://github.com/hanssens/extensions</PackageProjectUrl>
<PackageReleaseNotes>Release notes can be found at:
https://github.com/hanssens/extensions/releases</PackageReleaseNotes>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.0.0</FileVersion>
<Version>2.0.0</Version>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0.0</FileVersion>
<Version>2.1.0</Version>
<Authors>Juliën Hanssens</Authors>
<Company />
<PackageId>Hanssens.Net.Extensions</PackageId>
Expand Down
36 changes: 36 additions & 0 deletions src/Hanssens.Net/Http/HttpFactory.Async.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Hanssens.Net.Http
{
public partial class HttpFactory
{
public async Task<HttpResponseMessage> DeleteAsync(string requestUri, Dictionary<string, string> headers = null)
{
// TODO: investigate the proper use of a 'body' in a DELETE operation
// see also: http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
return await Execute(HttpMethod.Delete, requestUri, body: string.Empty, headers: null);
}

public async Task<HttpResponseMessage> GetAsync(string requestUri, Dictionary<string, string> headers = null)
{
return await Execute(HttpMethod.Get, requestUri, body: string.Empty, headers: headers);
}

public async Task<HttpResponseMessage> PatchAsync<T>(string requestUri, T body, Dictionary<string, string> headers = null)
{
return await Execute(new HttpMethod("PATCH"), requestUri, body, headers);
}

public async Task<HttpResponseMessage> PostAsync<T>(string requestUri, T body, Dictionary<string, string> headers = null)
{
return await Execute(HttpMethod.Post, requestUri, body, headers);
}

public async Task<HttpResponseMessage> PutAsync<T>(string requestUri, T body, Dictionary<string, string> headers = null)
{
return await Execute(HttpMethod.Put, requestUri, body, headers);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Hanssens.Net.Http
/// <summary>
/// Http utility, optimized for multiple http calls.
/// </summary>
public class HttpFactory : IDisposable
public partial class HttpFactory : IDisposable
{
/// <summary>
/// Single instance of the HttpClient.
Expand Down Expand Up @@ -51,48 +51,6 @@ public HttpFactory(HttpClient httpClient)
self_disposable = false;
}

public HttpResponseMessage Delete(string requestUri)
{
return DeleteAsync(requestUri).Result;
}

public HttpResponseMessage Get(string requestUri)
{
return GetAsync(requestUri).Result;
}

public HttpResponseMessage Post<T>(string requestUri, T body, Dictionary<string, string> headers = null)
{
return PostAsync(requestUri, body: body, headers: headers).Result;
}

public HttpResponseMessage Put<T>(string requestUri, T body, Dictionary<string, string> headers = null)
{
return PutAsync(requestUri, body: body, headers: headers).Result;
}

public async Task<HttpResponseMessage> DeleteAsync(string requestUri)
{
// TODO: investigate the proper use of a 'body' in a DELETE operation
// see also: http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
return await Execute(HttpMethod.Delete, requestUri, body: string.Empty, headers: null);
}

public async Task<HttpResponseMessage> GetAsync(string requestUri)
{
return await Execute(HttpMethod.Get, requestUri, body: string.Empty, headers: null);
}

public async Task<HttpResponseMessage> PostAsync<T>(string requestUri, T body, Dictionary<string, string> headers = null)
{
return await Execute(HttpMethod.Post, requestUri, body, headers);
}

public async Task<HttpResponseMessage> PutAsync<T>(string requestUri, T body, Dictionary<string, string> headers = null)
{
return await Execute(HttpMethod.Put, requestUri, body, headers);
}

private async Task<HttpResponseMessage> Execute<T>(HttpMethod httpMethod, string requestUri, T body, Dictionary<string, string> headers)
{
var request = new HttpRequestMessage(httpMethod, requestUri);
Expand Down Expand Up @@ -182,65 +140,5 @@ public void Dispose()
http.Dispose();
}

/// <summary>
/// Provides 'simple', shorthand functions intended for quickly making a single http call.
/// </summary>
public static class Simple
{
private static readonly HttpFactory Factory;

static Simple()
{
Factory = new HttpFactory();
}

public static HttpResponseMessage Delete(string requestUri)
{
return Factory.DeleteAsync(requestUri).Result;
}

public static async Task<HttpResponseMessage> DeleteAsync(string requestUri)
{
return await Factory.DeleteAsync(requestUri);
}

/// <summary>
/// Executes a single 'GET' request.
/// </summary>
/// <param name="requestUri">Endpoint to the remote resource</param>
public static HttpResponseMessage Get(string requestUri)
{
return Factory.Get(requestUri);
}

/// <summary>
/// Executes a single async 'GET' request.
/// </summary>
/// <param name="requestUri">Endpoint to the remote resource</param>
public static async Task<HttpResponseMessage> GetAsync(string requestUri)
{
return await Factory.GetAsync(requestUri);
}

public static HttpResponseMessage Post<T>(string requestUri, T body) where T: class
{
return Factory.Post(requestUri, body);
}

public static async Task<HttpResponseMessage> PostAsync<T>(string requestUri, T body) where T : class
{
return await Factory.PostAsync(requestUri, body);
}

public static HttpResponseMessage Put<T>(string requestUri, T body) where T : class
{
return Factory.Put(requestUri, body);
}

public static async Task<HttpResponseMessage> PutAsync<T>(string requestUri, T body) where T : class
{
return await Factory.PutAsync(requestUri, body);
}
}
}
}
80 changes: 80 additions & 0 deletions src/Hanssens.Net/Http/HttpFactory.Simple.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Hanssens.Net.Http
{
public partial class HttpFactory
{
/// <summary>
/// Provides 'simple', shorthand functions intended for quickly making a single http call.
/// </summary>
public static class Simple
{
private static readonly HttpFactory Factory;

static Simple()
{
Factory = new HttpFactory();
}

public static HttpResponseMessage Delete(string requestUri, Dictionary<string, string> headers = null)
{
return Factory.DeleteAsync(requestUri, headers).Result;
}

public static async Task<HttpResponseMessage> DeleteAsync(string requestUri, Dictionary<string, string> headers = null)
{
return await Factory.DeleteAsync(requestUri, headers);
}

/// <summary>
/// Executes a single 'GET' request.
/// </summary>
/// <param name="requestUri">Endpoint to the remote resource</param>
public static HttpResponseMessage Get(string requestUri)
{
return Factory.Get(requestUri);
}

/// <summary>
/// Executes a single async 'GET' request.
/// </summary>
/// <param name="requestUri">Endpoint to the remote resource</param>
public static async Task<HttpResponseMessage> GetAsync(string requestUri, Dictionary<string, string> headers = null)
{
return await Factory.GetAsync(requestUri, headers);
}

public static HttpResponseMessage Patch<T>(string requestUri, T body) where T : class
{
return Factory.Patch(requestUri, body);
}

public static async Task<HttpResponseMessage> PatchAsync<T>(string requestUri, T body) where T : class
{
return await Factory.PatchAsync(requestUri, body);
}

public static HttpResponseMessage Post<T>(string requestUri, T body) where T : class
{
return Factory.Post(requestUri, body);
}

public static async Task<HttpResponseMessage> PostAsync<T>(string requestUri, T body) where T : class
{
return await Factory.PostAsync(requestUri, body);
}

public static HttpResponseMessage Put<T>(string requestUri, T body) where T : class
{
return Factory.Put(requestUri, body);
}

public static async Task<HttpResponseMessage> PutAsync<T>(string requestUri, T body) where T : class
{
return await Factory.PutAsync(requestUri, body);
}
}
}
}
33 changes: 33 additions & 0 deletions src/Hanssens.Net/Http/HttpFactory.Sync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Net.Http;

namespace Hanssens.Net.Http
{
public partial class HttpFactory
{
public HttpResponseMessage Delete(string requestUri, Dictionary<string, string> headers = null)
{
return DeleteAsync(requestUri, headers).Result;
}

public HttpResponseMessage Get(string requestUri, Dictionary<string, string> headers = null)
{
return GetAsync(requestUri, headers).Result;
}

public HttpResponseMessage Patch<T>(string requestUri, T body, Dictionary<string, string> headers = null)
{
return PatchAsync(requestUri, body: body, headers: headers).Result;
}

public HttpResponseMessage Post<T>(string requestUri, T body, Dictionary<string, string> headers = null)
{
return PostAsync(requestUri, body: body, headers: headers).Result;
}

public HttpResponseMessage Put<T>(string requestUri, T body, Dictionary<string, string> headers = null)
{
return PutAsync(requestUri, body: body, headers: headers).Result;
}
}
}
29 changes: 29 additions & 0 deletions src/Hanssens.Net/Http/HttpResponseMessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Hanssens.Net.Http
{
public static class HttpResponseMessageExtensions
{
/// <summary>
/// Extension allowing easy access to get the value from a HttpResponseMessage.
/// </summary>
public static string Value(this HttpResponseMessage httpResponse)
{
var response = httpResponse.Content.ReadAsStringAsync().Result;
return response;
}

/// <summary>
/// Extension allowing easy access to get the value from a HttpResponseMessage.
/// </summary>
public static async Task<string> ValueAsync(this HttpResponseMessage httpResponse)
{
return await httpResponse.Content.ReadAsStringAsync();
}
}
}
8 changes: 7 additions & 1 deletion test/Hanssens.Net.Tests/HttpTests/HttpFactorySimpleTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Threading.Tasks;
using Hanssens.Net.Http;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -28,7 +29,7 @@ public void HttpFactory_Simple_Delete_Should_Return_Json_String()
}

[Fact]
public void HttpFactory_Simple_Get_Should_Return_Json_String()
public async Task HttpFactory_Simple_Get_Should_Return_Json_String()
{
// arrange
var endpoint = @"http://jsonplaceholder.typicode.com/posts";
Expand All @@ -40,6 +41,11 @@ public void HttpFactory_Simple_Get_Should_Return_Json_String()
target.Should().NotBeNull();
target.StatusCode.Should().Be(HttpStatusCode.OK);
target.IsSuccessStatusCode.Should().BeTrue();
var c = target.Content.ToString();
var foo = await target.Content.ReadAsStringAsync();

//requestContent.ReadAsStringAsync().Result;
foo.Should().Be("asdfsdafklasdjfklsdajfsdfkj");
}

[Fact]
Expand Down

0 comments on commit aae32eb

Please sign in to comment.