diff --git a/.gitignore b/.gitignore index 610a24a..7dfead3 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ CSharpHTTPClient/CSharpHTTPClient.userprefs CSharpHTTPClient/packages/ CSharpHTTPClient/CSharpHTTPClient.sln.VisualState.xml *.PublicKey -*.pfx \ No newline at end of file +*.pfx diff --git a/CHANGELOG.md b/CHANGELOG.md index 02de94d..1b809e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,3 +41,4 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [1.0.2] - 2016-03-17 ### Added - We are live! + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 01e7da5..b485273 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -163,4 +163,4 @@ Generally, we follow the style guidelines as suggested by the official language. 7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/) with a clear title and description against the `master` branch. All tests must be passing before we will review the PR. -If you have any additional questions, please feel free to [email](mailto:dx@sendgrid.com) us or create an issue in this repo. \ No newline at end of file +If you have any additional questions, please feel free to [email](mailto:dx@sendgrid.com) us or create an issue in this repo. diff --git a/CSharpHTTPClient/Client.cs b/CSharpHTTPClient/Client.cs index f54da8a..fb7bd33 100644 --- a/CSharpHTTPClient/Client.cs +++ b/CSharpHTTPClient/Client.cs @@ -63,17 +63,21 @@ public virtual Dictionary DeserializeResponseHeaders(HttpRespons public class Client : DynamicObject { + private static HttpClient _httpClient = new HttpClient(); public string Host; public Dictionary RequestHeaders; public string Version; public string UrlPath; public string MediaType; public WebProxy WebProxy; + public TimeSpan Timeout; + public enum Methods { DELETE, GET, PATCH, POST, PUT } + private int TimeoutDefault = 10; /// /// REST API client. @@ -89,6 +93,7 @@ public Client(WebProxy webProxy, string host, Dictionary request WebProxy = webProxy; } + /// /// REST API client. /// @@ -96,8 +101,9 @@ public Client(WebProxy webProxy, string host, Dictionary request /// A dictionary of request headers /// API version, override AddVersion to customize /// Path to endpoint (e.g. /path/to/endpoint) + /// Set an Timeout parameter for the HTTP Client /// Fluent interface to a REST API - public Client(string host, Dictionary requestHeaders = null, string version = null, string urlPath = null) + public Client(string host, Dictionary requestHeaders = null, string version = null, string urlPath = null, TimeSpan? timeOut = null) { Host = host; if(requestHeaders != null) @@ -106,6 +112,7 @@ public Client(string host, Dictionary requestHeaders = null, stri } Version = (version != null) ? version : null; UrlPath = (urlPath != null) ? urlPath : null; + Timeout = (timeOut != null) ? (TimeSpan)timeOut : TimeSpan.FromSeconds(TimeoutDefault); } /// @@ -171,11 +178,10 @@ private Client BuildClient(string name = null) } UrlPath = null; // Reset the current object's state before we return a new one - return new Client(Host, RequestHeaders, Version, endpoint); + return new Client(Host, RequestHeaders, Version, endpoint, Timeout); } - /// /// Factory method to return the right HttpClient settings. /// /// Instance of HttpClient @@ -194,9 +200,11 @@ private HttpClient BuildHttpClient() return new HttpClient(httpClientHandler); } - return new HttpClient(); + return _httpClient; } + /// + /// /// Add the authorization header, override to customize /// @@ -299,6 +307,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o /// Response object public async virtual Task MakeRequest(HttpClient client, HttpRequestMessage request) { + HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false); return new Response(response.StatusCode, response.Content, response.Headers); } @@ -310,16 +319,18 @@ public async virtual Task MakeRequest(HttpClient client, HttpRequestMe /// JSON formatted string /// JSON formatted queary paramaters /// Response object - private async Task RequestAsync(string method, string requestBody = null, string queryParams = null) + private async Task RequestAsync(string method, String requestBody = null, String queryParams = null) { - using (var client = BuildHttpClient()) + using (var client = new HttpClient()) { try { // Build the URL client.BaseAddress = new Uri(Host); + client.Timeout = Timeout; string endpoint = BuildUrl(queryParams); + // Build the request headers client.DefaultRequestHeaders.Accept.Clear(); if(RequestHeaders != null) @@ -371,4 +382,4 @@ private async Task RequestAsync(string method, string requestBody = nu } } } -} +} \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt index 9b2cba2..c1631d6 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -19,3 +19,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/README.md b/README.md index 97ee1c0..8c1e1ce 100644 --- a/README.md +++ b/README.md @@ -96,3 +96,4 @@ csharp-http-client is maintained and funded by SendGrid, Inc. The names and logo ![SendGrid Logo] (https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png) +