@@ -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+ }
0 commit comments