55using System . Threading . Tasks ;
66using System . Text ;
77using SendGrid . Resources ;
8- using System . Web . Script . Serialization ;
98using System . Net ;
9+ using Newtonsoft . Json . Linq ;
1010
1111namespace SendGrid
1212{
1313 public class Client
1414 {
15- private HttpResponseMessage _response = new HttpResponseMessage ( ) ;
1615 private string _apiKey ;
17- private Uri _baseUri ;
1816 public APIKeys ApiKeys ;
17+ public string Version ;
18+ private Uri _baseUri ;
19+ private const string MediaType = "application/json" ;
20+ private enum Methods
21+ {
22+ GET , POST , PATCH , DELETE
23+ }
1924
2025 /// <summary>
21- /// Create a client that connects to the SendGrid Web API
22- /// </summary>
23- /// <param name="apiKey">Your SendGrid API Key</param>
24- /// <param name="baseUri">Base SendGrid API Uri</param>
26+ /// Create a client that connects to the SendGrid Web API
27+ /// </summary>
28+ /// <param name="apiKey">Your SendGrid API Key</param>
29+ /// <param name="baseUri">Base SendGrid API Uri</param>
2530 public Client ( string apiKey , string baseUri = "https://api.sendgrid.com/" )
2631 {
2732 _baseUri = new Uri ( baseUri ) ;
2833 _apiKey = apiKey ;
34+ Version = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . ToString ( ) ;
2935 ApiKeys = new APIKeys ( this ) ;
3036 }
3137
@@ -34,96 +40,86 @@ public Client(string apiKey, string baseUri = "https://api.sendgrid.com/")
3440 /// </summary>
3541 /// <param name="method">HTTP verb, case-insensitive</param>
3642 /// <param name="endpoint">Resource endpoint, do not prepend slash</param>
37- /// <param name="data">An object representing the resource's data</param>
43+ /// <param name="data">An JObject representing the resource's data</param>
3844 /// <returns>An asyncronous task</returns>
39- private async Task RequestAsync ( string method , string endpoint , Object data )
45+ private async Task < HttpResponseMessage > RequestAsync ( Methods method , string endpoint , JObject data )
4046 {
4147 using ( var client = new HttpClient ( ) )
4248 {
4349 try
4450 {
4551 client . BaseAddress = _baseUri ;
4652 client . DefaultRequestHeaders . Accept . Clear ( ) ;
47- client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
53+ client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( MediaType ) ) ;
4854 client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , _apiKey ) ;
49- var version = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . ToString ( ) ;
50- client . DefaultRequestHeaders . TryAddWithoutValidation ( "User-Agent" , "sendgrid/" + version + ";csharp" ) ;
55+ client . DefaultRequestHeaders . TryAddWithoutValidation ( "User-Agent" , "sendgrid/" + Version + ";csharp" ) ;
5156
52- switch ( method . ToLower ( ) )
57+ switch ( method )
5358 {
54- case "get" :
55- _response = await client . GetAsync ( endpoint ) ;
56- break ;
57- case "post" :
58- _response = await client . PostAsJsonAsync ( endpoint , data ) ;
59- break ;
60- case "patch" :
59+ case Methods . GET :
60+ return await client . GetAsync ( endpoint ) ;
61+ case Methods . POST :
62+ return await client . PostAsJsonAsync ( endpoint , data ) ;
63+ case Methods . PATCH :
6164 endpoint = _baseUri + endpoint ;
62- StringContent content = new StringContent ( data . ToString ( ) , Encoding . UTF8 , "application/json" ) ;
65+ StringContent content = new StringContent ( data . ToString ( ) , Encoding . UTF8 , MediaType ) ;
6366 HttpRequestMessage request = new HttpRequestMessage
6467 {
6568 Method = new HttpMethod ( "PATCH" ) ,
6669 RequestUri = new Uri ( endpoint ) ,
6770 Content = content
6871 } ;
69- _response = await client . SendAsync ( request ) ;
70- break ;
71- case "delete" :
72- _response = await client . DeleteAsync ( endpoint ) ;
73- break ;
72+ return await client . SendAsync ( request ) ;
73+ case Methods . DELETE :
74+ return await client . DeleteAsync ( endpoint ) ;
7475 default :
75- _response . StatusCode = HttpStatusCode . MethodNotAllowed ;
76- _response . Content = new StringContent ( "Bad method call: " + method ) ;
77- break ;
76+ HttpResponseMessage response = new HttpResponseMessage ( ) ;
77+ response . StatusCode = HttpStatusCode . MethodNotAllowed ;
78+ var message = "{\" errors\" :[{\" message\" :\" Bad method call, supported methods are GET, POST, PATCH and DELETE\" }]}" ;
79+ response . Content = new StringContent ( message ) ;
80+ return response ;
7881 }
7982 }
80- catch ( HttpRequestException hre )
81- {
82- _response . StatusCode = HttpStatusCode . InternalServerError ;
83- _response . Content = new StringContent ( hre . ToString ( ) ) ;
84- }
8583 catch ( Exception ex )
8684 {
87- _response . StatusCode = HttpStatusCode . InternalServerError ;
88- _response . Content = new StringContent ( ex . ToString ( ) ) ;
85+ HttpResponseMessage response = new HttpResponseMessage ( ) ;
86+ string message ;
87+ message = ( ex is HttpRequestException ) ? ".NET HttpRequestException" : ".NET Exception" ;
88+ message = message + ", raw message: \n \n " ;
89+ response . Content = new StringContent ( message + ex . Message ) ;
90+ return response ;
8991 }
9092 }
9193 }
9294
9395 /// <param name="endpoint">Resource endpoint, do not prepend slash</param>
9496 /// <returns>The resulting message from the API call</returns>
95- public HttpResponseMessage Get ( string endpoint )
97+ public async Task < HttpResponseMessage > Get ( string endpoint )
9698 {
97- RequestAsync ( "GET" , endpoint , null ) . Wait ( ) ;
98- return _response ;
99+ return await RequestAsync ( Methods . GET , endpoint , null ) ;
99100 }
100101
101102 /// <param name="endpoint">Resource endpoint, do not prepend slash</param>
102- /// <param name="data">An object representing the resource's data</param>
103+ /// <param name="data">An JObject representing the resource's data</param>
103104 /// <returns>The resulting message from the API call</returns>
104- public HttpResponseMessage Post ( string endpoint , object data )
105+ public async Task < HttpResponseMessage > Post ( string endpoint , JObject data )
105106 {
106- RequestAsync ( "POST" , endpoint , data ) . Wait ( ) ;
107- return _response ;
107+ return await RequestAsync ( Methods . POST , endpoint , data ) ;
108108 }
109109
110110 /// <param name="endpoint">Resource endpoint, do not prepend slash</param>
111111 /// <returns>The resulting message from the API call</returns>
112- public HttpResponseMessage Delete ( string endpoint )
112+ public async Task < HttpResponseMessage > Delete ( string endpoint )
113113 {
114- RequestAsync ( "DELETE" , endpoint , null ) . Wait ( ) ;
115- return _response ;
114+ return await RequestAsync ( Methods . DELETE , endpoint , null ) ;
116115 }
117116
118117 /// <param name="endpoint">Resource endpoint, do not prepend slash</param>
119- /// <param name="data">An object representing the resource's data</param>
118+ /// <param name="data">An JObject representing the resource's data</param>
120119 /// <returns>The resulting message from the API call</returns>
121- public HttpResponseMessage Patch ( string endpoint , object data )
120+ public async Task < HttpResponseMessage > Patch ( string endpoint , JObject data )
122121 {
123- var json = new JavaScriptSerializer ( ) . Serialize ( data ) ;
124- RequestAsync ( "PATCH" , endpoint , json ) . Wait ( ) ;
125- return _response ;
122+ return await RequestAsync ( Methods . PATCH , endpoint , data ) ;
126123 }
127-
128124 }
129125}
0 commit comments