@@ -54,11 +54,6 @@ public class SendGridClient
5454 /// <returns>Interface to the SendGrid REST API</returns>
5555 public SendGridClient ( IWebProxy webProxy , string apiKey , string host = null , Dictionary < string , string > requestHeaders = null , string version = "v3" , string urlPath = null )
5656 {
57- UrlPath = urlPath ;
58- Version = version ;
59-
60- var baseAddress = host ?? "https://api.sendgrid.com" ;
61- var clientVersion = GetType ( ) . GetTypeInfo ( ) . Assembly . GetName ( ) . Version . ToString ( ) ;
6257
6358 // Create client with WebProxy if set
6459 if ( webProxy != null )
@@ -76,6 +71,58 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic
7671 client = new HttpClient ( ) ;
7772 }
7873
74+ InitiateClient ( apiKey , host , requestHeaders , version , urlPath ) ;
75+ }
76+
77+ /// <summary>
78+ /// Constructor. Initializes a new instance of the <see cref="SendGridClient"/> class.
79+ /// </summary>
80+ /// <param name="httpClient">An optional http client which may me injected in order to facilitate testing.</param>
81+ /// <param name="apiKey">Your SendGrid API key.</param>
82+ /// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
83+ /// <param name="requestHeaders">A dictionary of request headers</param>
84+ /// <param name="version">API version, override AddVersion to customize</param>
85+ /// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param>
86+ /// <returns>Interface to the SendGrid REST API</returns>
87+ public SendGridClient ( HttpClient httpClient , string apiKey , string host = null , Dictionary < string , string > requestHeaders = null , string version = "v3" , string urlPath = null )
88+ {
89+ client = ( httpClient == null ) ? new HttpClient ( ) : httpClient ;
90+
91+ InitiateClient ( apiKey , host , requestHeaders , version , urlPath ) ;
92+ }
93+
94+
95+ /// <summary>
96+ /// Initializes a new instance of the <see cref="SendGridClient"/> class.
97+ /// </summary>
98+ /// <param name="apiKey">Your SendGrid API key.</param>
99+ /// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
100+ /// <param name="requestHeaders">A dictionary of request headers</param>
101+ /// <param name="version">API version, override AddVersion to customize</param>
102+ /// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param>
103+ /// <returns>Interface to the SendGrid REST API</returns>
104+ public SendGridClient ( string apiKey , string host = null , Dictionary < string , string > requestHeaders = null , string version = "v3" , string urlPath = null )
105+ : this ( httpClient : null , apiKey : apiKey , host : host , requestHeaders : requestHeaders , version : version , urlPath : urlPath )
106+ {
107+ }
108+
109+ /// <summary>
110+ /// Common method to initiate internal fields regardless of which constructor was used.
111+ /// </summary>
112+ /// <param name="apiKey"></param>
113+ /// <param name="host"></param>
114+ /// <param name="requestHeaders"></param>
115+ /// <param name="version"></param>
116+ /// <param name="urlPath"></param>
117+ private void InitiateClient ( string apiKey , string host , Dictionary < string , string > requestHeaders , string version , string urlPath )
118+ {
119+ UrlPath = urlPath ;
120+ Version = version ;
121+
122+ var baseAddress = host ?? "https://api.sendgrid.com" ;
123+ var clientVersion = GetType ( ) . GetTypeInfo ( ) . Assembly . GetName ( ) . Version . ToString ( ) ;
124+
125+
79126 // standard headers
80127 client . BaseAddress = new Uri ( baseAddress ) ;
81128 Dictionary < string , string > headers = new Dictionary < string , string >
@@ -113,20 +160,7 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic
113160 client . DefaultRequestHeaders . Add ( header . Key , header . Value ) ;
114161 }
115162 }
116- }
117163
118- /// <summary>
119- /// Initializes a new instance of the <see cref="SendGridClient"/> class.
120- /// </summary>
121- /// <param name="apiKey">Your SendGrid API key.</param>
122- /// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
123- /// <param name="requestHeaders">A dictionary of request headers</param>
124- /// <param name="version">API version, override AddVersion to customize</param>
125- /// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param>
126- /// <returns>Interface to the SendGrid REST API</returns>
127- public SendGridClient ( string apiKey , string host = null , Dictionary < string , string > requestHeaders = null , string version = "v3" , string urlPath = null )
128- : this ( null , apiKey , host , requestHeaders , version , urlPath )
129- {
130164 }
131165
132166 /// <summary>
@@ -192,40 +226,34 @@ public virtual AuthenticationHeaderValue AddAuthorization(KeyValuePair<string, s
192226 /// <param name="urlPath">The path to the API endpoint.</param>
193227 /// <param name="cancellationToken">Cancel the asynchronous call.</param>
194228 /// <returns>Response object</returns>
229+ /// <exception cref="Exception">The method will NOT catch and swallow exceptions generated by sending a request
230+ /// through the internal http client. Any underlying exception will pass right through.
231+ /// In particular, this means that you may expect
232+ /// a TimeoutException if you are not connected to the internet.</exception>
195233 public async Task < Response > RequestAsync (
196234 SendGridClient . Method method ,
197235 string requestBody = null ,
198236 string queryParams = null ,
199237 string urlPath = null ,
200238 CancellationToken cancellationToken = default ( CancellationToken ) )
201239 {
202- try
203- {
204- var endpoint = client . BaseAddress + BuildUrl ( urlPath , queryParams ) ;
205-
206- // Build the request body
207- StringContent content = null ;
208- if ( requestBody != null )
209- {
210- content = new StringContent ( requestBody , Encoding . UTF8 , this . MediaType ) ;
211- }
240+ var endpoint = client . BaseAddress + BuildUrl ( urlPath , queryParams ) ;
212241
213- // Build the final request
214- var request = new HttpRequestMessage
215- {
216- Method = new HttpMethod ( method . ToString ( ) ) ,
217- RequestUri = new Uri ( endpoint ) ,
218- Content = content
219- } ;
220- return await MakeRequest ( request , cancellationToken ) . ConfigureAwait ( false ) ;
221- }
222- catch ( Exception ex )
242+ // Build the request body
243+ StringContent content = null ;
244+ if ( requestBody != null )
223245 {
224- var response = new HttpResponseMessage ( ) ;
225- var message = ( ex is HttpRequestException ) ? ".NET HttpRequestException" : ".NET Exception" ;
226- response . Content = new StringContent ( message + ", raw message: \n \n " + ex . Message ) ;
227- return new Response ( response . StatusCode , response . Content , response . Headers ) ;
246+ content = new StringContent ( requestBody , Encoding . UTF8 , this . MediaType ) ;
228247 }
248+
249+ // Build the final request
250+ var request = new HttpRequestMessage
251+ {
252+ Method = new HttpMethod ( method . ToString ( ) ) ,
253+ RequestUri = new Uri ( endpoint ) ,
254+ Content = content
255+ } ;
256+ return await MakeRequest ( request , cancellationToken ) . ConfigureAwait ( false ) ;
229257 }
230258
231259 /// <summary>
0 commit comments