@@ -70,11 +70,14 @@ public class Client : DynamicObject
70
70
public string UrlPath ;
71
71
public string MediaType ;
72
72
public WebProxy WebProxy ;
73
+ public TimeSpan Timeout ;
74
+
73
75
public enum Methods
74
76
{
75
77
DELETE , GET , PATCH , POST , PUT
76
78
}
77
79
80
+ private int TimeoutDefault = 10 ;
78
81
79
82
/// <summary>
80
83
/// REST API client.
@@ -90,15 +93,17 @@ public Client(WebProxy webProxy, string host, Dictionary<string, string> request
90
93
WebProxy = webProxy ;
91
94
}
92
95
96
+
93
97
/// <summary>
94
98
/// REST API client.
95
99
/// </summary>
96
100
/// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
97
101
/// <param name="requestHeaders">A dictionary of request headers</param>
98
102
/// <param name="version">API version, override AddVersion to customize</param>
99
103
/// <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>
100
105
/// <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 )
102
107
{
103
108
Host = host ;
104
109
if ( requestHeaders != null )
@@ -107,6 +112,7 @@ public Client(string host, Dictionary<string,string> requestHeaders = null, stri
107
112
}
108
113
Version = ( version != null ) ? version : null ;
109
114
UrlPath = ( urlPath != null ) ? urlPath : null ;
115
+ Timeout = ( timeOut != null ) ? ( TimeSpan ) timeOut : TimeSpan . FromSeconds ( TimeoutDefault ) ;
110
116
}
111
117
112
118
/// <summary>
@@ -172,11 +178,10 @@ private Client BuildClient(string name = null)
172
178
}
173
179
174
180
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 ) ;
176
182
177
183
}
178
184
179
- /// <summary>
180
185
/// Factory method to return the right HttpClient settings.
181
186
/// </summary>
182
187
/// <returns>Instance of HttpClient</returns>
@@ -198,6 +203,8 @@ private HttpClient BuildHttpClient()
198
203
return _httpClient ;
199
204
}
200
205
206
+ /// <summary>
207
+
201
208
/// <summary>
202
209
/// Add the authorization header, override to customize
203
210
/// </summary>
@@ -300,6 +307,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
300
307
/// <returns>Response object</returns>
301
308
public async virtual Task < Response > MakeRequest ( HttpClient client , HttpRequestMessage request )
302
309
{
310
+
303
311
HttpResponseMessage response = await client . SendAsync ( request ) . ConfigureAwait ( false ) ;
304
312
return new Response ( response . StatusCode , response . Content , response . Headers ) ;
305
313
}
@@ -311,16 +319,18 @@ public async virtual Task<Response> MakeRequest(HttpClient client, HttpRequestMe
311
319
/// <param name="requestBody">JSON formatted string</param>
312
320
/// <param name="queryParams">JSON formatted queary paramaters</param>
313
321
/// <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 )
315
323
{
316
- using ( var client = BuildHttpClient ( ) )
324
+ using ( var client = new HttpClient ( ) )
317
325
{
318
326
try
319
327
{
320
328
// Build the URL
321
329
client . BaseAddress = new Uri ( Host ) ;
330
+ client . Timeout = Timeout ;
322
331
string endpoint = BuildUrl ( queryParams ) ;
323
332
333
+
324
334
// Build the request headers
325
335
client . DefaultRequestHeaders . Accept . Clear ( ) ;
326
336
if ( RequestHeaders != null )
@@ -372,4 +382,4 @@ private async Task<Response> RequestAsync(string method, string requestBody = nu
372
382
}
373
383
}
374
384
}
375
- }
385
+ }
0 commit comments