-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added new overload for ExecuteAsync to support Cancellation Tokens
* Added more comments in the code and on the methods * Updated readme file
- Loading branch information
Showing
17 changed files
with
335 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 25 additions & 129 deletions
154
RestApiBuilder.Core/Providers/BaseConnectionProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,41 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace RestApiClientBuilder.Core.Providers | ||
{ | ||
public class ConnectionRequest | ||
{ | ||
public string Content { get; set; } | ||
public Uri RelativeUri { get; set; } | ||
public HttpMethod Method { get; set; } | ||
public Uri BaseAddress { get; set; } | ||
} | ||
|
||
public class ConnectionRequestResponse | ||
{ | ||
public string ResponseString { get; set; } | ||
public int StatusCode { get; set; } | ||
public bool IsSuccess { get; set; } | ||
public string ErrorReason { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Base class for a connection provider | ||
/// </summary> | ||
public abstract class BaseConnectionProvider : IRestConnectionProvider | ||
{ | ||
/// <summary> | ||
/// True when a custom interception handler is added to the client | ||
/// </summary> | ||
public bool HasHandlers { get; set; } | ||
|
||
/// <summary> | ||
/// Called when the client needs to be created | ||
/// </summary> | ||
public Func<bool, object> OnCreateClient { get; set; } | ||
|
||
public abstract ConnectionRequest CreateRequest(HttpMethod post, Uri baseAddress, Uri endpointRelativeUri, string content); | ||
/// <summary> | ||
/// Called when a request is created | ||
/// </summary> | ||
/// <param name="method">HTTP method to invoke</param> | ||
/// <param name="baseAddress">Base Uri to invoke to</param> | ||
/// <param name="endpointRelativeUri">Relative uri for the endpoint</param> | ||
/// <param name="content">Optional content for body of PUT and POST requests</param> | ||
/// <returns>Connection request summary data</returns> | ||
public abstract ConnectionRequest CreateRequest(HttpMethod method, Uri baseAddress, Uri endpointRelativeUri, string content); | ||
|
||
/// <summary> | ||
/// Executes a request in an async manner. | ||
/// </summary> | ||
/// <param name="connectionRequest">Request definition to execute</param> | ||
/// <param name="token">Cancellation token to use when executing</param> | ||
/// <returns>Response object of the request</returns> | ||
public abstract Task<ConnectionRequestResponse> ProcessRequestAsync(ConnectionRequest connectionRequest, CancellationToken token); | ||
} | ||
|
||
public interface IRestConnectionProvider | ||
{ | ||
bool HasHandlers { get; set; } | ||
|
||
Func<bool, object> OnCreateClient { get; set; } | ||
|
||
ConnectionRequest CreateRequest(HttpMethod post, Uri baseAddress, Uri endpointRelativeUri, string content); | ||
|
||
Task<ConnectionRequestResponse> ProcessRequestAsync(ConnectionRequest connectionRequest, CancellationToken token); | ||
} | ||
|
||
public class HttpClientConnectionProvider : BaseConnectionProvider | ||
{ | ||
private static HttpClient _httpClient; | ||
|
||
public HttpClientConnectionProvider() | ||
{ | ||
OnCreateClient = (hasHandlers) => | ||
{ | ||
var client = new HttpClient(); | ||
client.DefaultRequestHeaders.Accept.Clear(); | ||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
HasHandlers = false; | ||
return client; | ||
}; | ||
} | ||
|
||
public override ConnectionRequest CreateRequest(HttpMethod method, Uri baseAddress, Uri endpointRelativeUri, string content) | ||
{ | ||
return new ConnectionRequest | ||
{ | ||
Method = method, | ||
BaseAddress = baseAddress, | ||
RelativeUri = endpointRelativeUri, | ||
Content = content | ||
}; | ||
} | ||
|
||
public override async Task<ConnectionRequestResponse> ProcessRequestAsync(ConnectionRequest connectionRequest, CancellationToken token) | ||
{ | ||
_httpClient = (HttpClient)OnCreateClient(HasHandlers); | ||
|
||
_httpClient.BaseAddress = connectionRequest.BaseAddress; | ||
|
||
if (connectionRequest.Method == HttpMethod.Post) | ||
{ | ||
HttpContent content = new StringContent(connectionRequest.Content); | ||
|
||
HttpRequestMessage request = new HttpRequestMessage(System.Net.Http.HttpMethod.Post, connectionRequest.RelativeUri); | ||
request.Content = content; | ||
|
||
HttpResponseMessage response = await _httpClient.SendAsync(request, token); | ||
|
||
return new ConnectionRequestResponse | ||
{ | ||
IsSuccess = response.IsSuccessStatusCode, | ||
ResponseString = response.IsSuccessStatusCode ? await response.Content.ReadAsStringAsync() : null, | ||
StatusCode = (int) response.StatusCode, | ||
ErrorReason = !response.IsSuccessStatusCode ? response.ReasonPhrase : null | ||
}; | ||
} | ||
if (connectionRequest.Method == HttpMethod.Put) | ||
{ | ||
HttpContent content = new StringContent(connectionRequest.Content); | ||
|
||
HttpRequestMessage request = new HttpRequestMessage(System.Net.Http.HttpMethod.Put, connectionRequest.RelativeUri); | ||
request.Content = content; | ||
|
||
HttpResponseMessage response = await _httpClient.SendAsync(request, token); | ||
|
||
return new ConnectionRequestResponse | ||
{ | ||
IsSuccess = response.IsSuccessStatusCode, | ||
ResponseString = response.IsSuccessStatusCode ? await response.Content.ReadAsStringAsync() : null, | ||
StatusCode = (int)response.StatusCode, | ||
ErrorReason = !response.IsSuccessStatusCode ? response.ReasonPhrase : null | ||
}; | ||
} | ||
if (connectionRequest.Method == HttpMethod.Get) | ||
{ | ||
HttpRequestMessage request = new HttpRequestMessage(System.Net.Http.HttpMethod.Get, connectionRequest.RelativeUri); | ||
|
||
HttpResponseMessage response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, token); | ||
|
||
return new ConnectionRequestResponse | ||
{ | ||
IsSuccess = response.IsSuccessStatusCode, | ||
ResponseString = response.IsSuccessStatusCode ? await response.Content.ReadAsStringAsync() : null, | ||
StatusCode = (int)response.StatusCode, | ||
ErrorReason = !response.IsSuccessStatusCode ? response.ReasonPhrase : null | ||
}; | ||
} | ||
if (connectionRequest.Method == HttpMethod.Delete) | ||
{ | ||
HttpRequestMessage request = new HttpRequestMessage(System.Net.Http.HttpMethod.Delete, connectionRequest.RelativeUri); | ||
|
||
HttpResponseMessage response = await _httpClient.SendAsync(request, token); | ||
|
||
return new ConnectionRequestResponse | ||
{ | ||
IsSuccess = response.IsSuccessStatusCode, | ||
ResponseString = response.IsSuccessStatusCode ? await response.Content.ReadAsStringAsync() : null, | ||
StatusCode = (int)response.StatusCode, | ||
ErrorReason = !response.IsSuccessStatusCode ? response.ReasonPhrase : null | ||
}; | ||
} | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
|
||
namespace RestApiClientBuilder.Core.Providers | ||
{ | ||
/// <summary> | ||
/// Connection request wrapper | ||
/// </summary> | ||
public class ConnectionRequest | ||
{ | ||
/// <summary> | ||
/// Optional content for PUT or POST requests | ||
/// </summary> | ||
public string Content { get; set; } | ||
|
||
/// <summary> | ||
/// Relative Uri for the request | ||
/// </summary> | ||
public Uri RelativeUri { get; set; } | ||
|
||
/// <summary> | ||
/// Method of the request | ||
/// </summary> | ||
public HttpMethod Method { get; set; } | ||
|
||
/// <summary> | ||
/// Base Uri for the request | ||
/// </summary> | ||
public Uri BaseAddress { get; set; } | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
RestApiBuilder.Core/Providers/ConnectionRequestResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace RestApiClientBuilder.Core.Providers | ||
{ | ||
/// <summary> | ||
/// Response of request wrapper | ||
/// </summary> | ||
public class ConnectionRequestResponse | ||
{ | ||
/// <summary> | ||
/// Response in string format (raw) | ||
/// </summary> | ||
public string ResponseString { get; set; } | ||
|
||
/// <summary> | ||
/// Statuscode of the response | ||
/// </summary> | ||
public int StatusCode { get; set; } | ||
|
||
/// <summary> | ||
/// True when the call is a success | ||
/// </summary> | ||
public bool IsSuccess { get; set; } | ||
|
||
/// <summary> | ||
/// Filled when an error occured. | ||
/// </summary> | ||
public string ErrorReason { get; set; } | ||
} | ||
} |
Oops, something went wrong.