Skip to content

Commit b1b975c

Browse files
Merge pull request sendgrid#23 from PandaBoy00/master
Timeout Parameter
2 parents 0ddabdf + 7204b8e commit b1b975c

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ CSharpHTTPClient/CSharpHTTPClient.userprefs
1313
CSharpHTTPClient/packages/
1414
CSharpHTTPClient/CSharpHTTPClient.sln.VisualState.xml
1515
*.PublicKey
16-
*.pfx
16+
*.pfx

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4646
## [1.0.2] - 2016-03-17
4747
### Added
4848
- We are live!
49+

Diff for: CSharpHTTPClient/Client.cs

+16-6
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,14 @@ public class Client : DynamicObject
7070
public string UrlPath;
7171
public string MediaType;
7272
public WebProxy WebProxy;
73+
public TimeSpan Timeout;
74+
7375
public enum Methods
7476
{
7577
DELETE, GET, PATCH, POST, PUT
7678
}
7779

80+
private int TimeoutDefault = 10;
7881

7982
/// <summary>
8083
/// REST API client.
@@ -90,15 +93,17 @@ public Client(WebProxy webProxy, string host, Dictionary<string, string> request
9093
WebProxy = webProxy;
9194
}
9295

96+
9397
/// <summary>
9498
/// REST API client.
9599
/// </summary>
96100
/// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
97101
/// <param name="requestHeaders">A dictionary of request headers</param>
98102
/// <param name="version">API version, override AddVersion to customize</param>
99103
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param>
104+
/// <param name="timeOut">Set an Timeout parameter for the HTTP Client</param>
100105
/// <returns>Fluent interface to a REST API</returns>
101-
public Client(string host, Dictionary<string,string> requestHeaders = null, string version = null, string urlPath = null)
106+
public Client(string host, Dictionary<string,string> requestHeaders = null, string version = null, string urlPath = null, TimeSpan? timeOut = null)
102107
{
103108
Host = host;
104109
if(requestHeaders != null)
@@ -107,6 +112,7 @@ public Client(string host, Dictionary<string,string> requestHeaders = null, stri
107112
}
108113
Version = (version != null) ? version : null;
109114
UrlPath = (urlPath != null) ? urlPath : null;
115+
Timeout = (timeOut != null) ? (TimeSpan)timeOut : TimeSpan.FromSeconds(TimeoutDefault);
110116
}
111117

112118
/// <summary>
@@ -172,11 +178,10 @@ private Client BuildClient(string name = null)
172178
}
173179

174180
UrlPath = null; // Reset the current object's state before we return a new one
175-
return new Client(Host, RequestHeaders, Version, endpoint);
181+
return new Client(Host, RequestHeaders, Version, endpoint, Timeout);
176182

177183
}
178184

179-
/// <summary>
180185
/// Factory method to return the right HttpClient settings.
181186
/// </summary>
182187
/// <returns>Instance of HttpClient</returns>
@@ -198,6 +203,8 @@ private HttpClient BuildHttpClient()
198203
return _httpClient;
199204
}
200205

206+
/// <summary>
207+
201208
/// <summary>
202209
/// Add the authorization header, override to customize
203210
/// </summary>
@@ -300,6 +307,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
300307
/// <returns>Response object</returns>
301308
public async virtual Task<Response> MakeRequest(HttpClient client, HttpRequestMessage request)
302309
{
310+
303311
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
304312
return new Response(response.StatusCode, response.Content, response.Headers);
305313
}
@@ -311,16 +319,18 @@ public async virtual Task<Response> MakeRequest(HttpClient client, HttpRequestMe
311319
/// <param name="requestBody">JSON formatted string</param>
312320
/// <param name="queryParams">JSON formatted queary paramaters</param>
313321
/// <returns>Response object</returns>
314-
private async Task<Response> RequestAsync(string method, string requestBody = null, string queryParams = null)
322+
private async Task<Response> RequestAsync(string method, String requestBody = null, String queryParams = null)
315323
{
316-
using (var client = BuildHttpClient())
324+
using (var client = new HttpClient())
317325
{
318326
try
319327
{
320328
// Build the URL
321329
client.BaseAddress = new Uri(Host);
330+
client.Timeout = Timeout;
322331
string endpoint = BuildUrl(queryParams);
323332

333+
324334
// Build the request headers
325335
client.DefaultRequestHeaders.Accept.Clear();
326336
if(RequestHeaders != null)
@@ -372,4 +382,4 @@ private async Task<Response> RequestAsync(string method, string requestBody = nu
372382
}
373383
}
374384
}
375-
}
385+
}

Diff for: LICENSE.txt

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22+

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,4 @@ csharp-http-client is maintained and funded by SendGrid, Inc. The names and logo
9696

9797
![SendGrid Logo]
9898
(https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)
99+

0 commit comments

Comments
 (0)