Skip to content

Timeout Parameter #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ CSharpHTTPClient/CSharpHTTPClient.userprefs
CSharpHTTPClient/packages/
CSharpHTTPClient/CSharpHTTPClient.sln.VisualState.xml
*.PublicKey
*.pfx
*.pfx
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [1.0.2] - 2016-03-17
### Added
- We are live!

2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]) us or create an issue in this repo.
If you have any additional questions, please feel free to [email](mailto:[email protected]) us or create an issue in this repo.
25 changes: 18 additions & 7 deletions CSharpHTTPClient/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,21 @@ public virtual Dictionary<string, string> DeserializeResponseHeaders(HttpRespons

public class Client : DynamicObject
{
private static HttpClient _httpClient = new HttpClient();
public string Host;
public Dictionary <string,string> 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;

/// <summary>
/// REST API client.
Expand All @@ -89,15 +93,17 @@ public Client(WebProxy webProxy, string host, Dictionary<string, string> request
WebProxy = webProxy;
}


/// <summary>
/// REST API client.
/// </summary>
/// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
/// <param name="requestHeaders">A dictionary of request headers</param>
/// <param name="version">API version, override AddVersion to customize</param>
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param>
/// <param name="timeOut">Set an Timeout parameter for the HTTP Client</param>
/// <returns>Fluent interface to a REST API</returns>
public Client(string host, Dictionary<string,string> requestHeaders = null, string version = null, string urlPath = null)
public Client(string host, Dictionary<string,string> requestHeaders = null, string version = null, string urlPath = null, TimeSpan? timeOut = null)
{
Host = host;
if(requestHeaders != null)
Expand All @@ -106,6 +112,7 @@ public Client(string host, Dictionary<string,string> requestHeaders = null, stri
}
Version = (version != null) ? version : null;
UrlPath = (urlPath != null) ? urlPath : null;
Timeout = (timeOut != null) ? (TimeSpan)timeOut : TimeSpan.FromSeconds(TimeoutDefault);
}

/// <summary>
Expand Down Expand Up @@ -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);

}

/// <summary>
/// Factory method to return the right HttpClient settings.
/// </summary>
/// <returns>Instance of HttpClient</returns>
Expand All @@ -194,9 +200,11 @@ private HttpClient BuildHttpClient()
return new HttpClient(httpClientHandler);
}

return new HttpClient();
return _httpClient;
}

/// <summary>

/// <summary>
/// Add the authorization header, override to customize
/// </summary>
Expand Down Expand Up @@ -299,6 +307,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
/// <returns>Response object</returns>
public async virtual Task<Response> MakeRequest(HttpClient client, HttpRequestMessage request)
{

HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
return new Response(response.StatusCode, response.Content, response.Headers);
}
Expand All @@ -310,16 +319,18 @@ public async virtual Task<Response> MakeRequest(HttpClient client, HttpRequestMe
/// <param name="requestBody">JSON formatted string</param>
/// <param name="queryParams">JSON formatted queary paramaters</param>
/// <returns>Response object</returns>
private async Task<Response> RequestAsync(string method, string requestBody = null, string queryParams = null)
private async Task<Response> 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)
Expand Down Expand Up @@ -371,4 +382,4 @@ private async Task<Response> RequestAsync(string method, string requestBody = nu
}
}
}
}
}
1 change: 1 addition & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)