9
9
using System . Threading . Tasks ;
10
10
using System . Web . Script . Serialization ;
11
11
using System . Web ;
12
+ using System . Diagnostics ;
12
13
13
14
namespace SendGrid . CSharp . HTTP . Client
14
15
{
@@ -17,43 +18,39 @@ public class Response
17
18
public HttpStatusCode StatusCode ;
18
19
public HttpContent ResponseBody ;
19
20
public HttpResponseHeaders ResponseHeaders ;
20
- public Dictionary < string , dynamic > DSResponseBody ;
21
- public Dictionary < string , string > DSResponseHeaders ;
22
21
23
22
public Response ( HttpStatusCode statusCode , HttpContent responseBody , HttpResponseHeaders responseHeaders )
24
23
{
25
24
StatusCode = statusCode ;
26
25
ResponseBody = responseBody ;
27
26
ResponseHeaders = responseHeaders ;
28
- DSResponseBody = DeserializeResponseBody ( responseBody ) ;
29
- DSResponseHeaders = DeserializeResponseHeaders ( responseHeaders ) ;
30
27
}
31
28
32
- public Dictionary < string , dynamic > DeserializeResponseBody ( HttpContent content )
29
+ public virtual Dictionary < string , dynamic > DeserializeResponseBody ( HttpContent content )
33
30
{
34
31
JavaScriptSerializer jss = new JavaScriptSerializer ( ) ;
35
- var ds_content = jss . Deserialize < Dictionary < string , dynamic > > ( content . ReadAsStringAsync ( ) . Result ) ;
36
- return ds_content ;
32
+ var dsContent = jss . Deserialize < Dictionary < string , dynamic > > ( content . ReadAsStringAsync ( ) . Result ) ;
33
+ return dsContent ;
37
34
}
38
35
39
- public Dictionary < string , string > DeserializeResponseHeaders ( HttpResponseHeaders content )
36
+ public virtual Dictionary < string , string > DeserializeResponseHeaders ( HttpResponseHeaders content )
40
37
{
41
- var ds_content = new Dictionary < string , string > ( ) ;
38
+ var dsContent = new Dictionary < string , string > ( ) ;
42
39
foreach ( var pair in content )
43
40
{
44
- ds_content . Add ( pair . Key , pair . Value . First ( ) ) ;
41
+ dsContent . Add ( pair . Key , pair . Value . First ( ) ) ;
45
42
}
46
- return ds_content ;
43
+ return dsContent ;
47
44
}
48
45
49
46
}
50
47
51
48
public class Client : DynamicObject
52
49
{
53
- private string _host ;
54
- private Dictionary < string , string > _requestHeaders ;
55
- private string _version ;
56
- private string _urlPath ;
50
+ public string Host ;
51
+ public Dictionary < string , string > RequestHeaders ;
52
+ public string Version ;
53
+ public string UrlPath ;
57
54
public string MediaType ;
58
55
public enum Methods
59
56
{
@@ -62,26 +59,26 @@ public enum Methods
62
59
63
60
public Client ( string host , Dictionary < string , string > requestHeaders = null , string version = null , string urlPath = null )
64
61
{
65
- _host = host ;
62
+ Host = host ;
66
63
if ( requestHeaders != null )
67
64
{
68
- _requestHeaders = ( _requestHeaders != null )
69
- ? _requestHeaders . Union ( requestHeaders ) . ToDictionary ( pair => pair . Key , pair => pair . Value ) : requestHeaders ;
65
+ RequestHeaders = ( RequestHeaders != null )
66
+ ? RequestHeaders . Union ( requestHeaders ) . ToDictionary ( pair => pair . Key , pair => pair . Value ) : requestHeaders ;
70
67
}
71
- _version = ( version != null ) ? version : null ;
72
- _urlPath = ( urlPath != null ) ? urlPath : null ;
68
+ Version = ( version != null ) ? version : null ;
69
+ UrlPath = ( urlPath != null ) ? urlPath : null ;
73
70
}
74
71
75
72
private string BuildUrl ( string query_params = null )
76
73
{
77
74
string endpoint = null ;
78
- if ( _version != null )
75
+ if ( Version != null )
79
76
{
80
- endpoint = _host + "/" + _version + _urlPath ;
77
+ endpoint = Host + "/" + Version + UrlPath ;
81
78
}
82
79
else
83
80
{
84
- endpoint = _host + _urlPath ;
81
+ endpoint = Host + UrlPath ;
85
82
}
86
83
87
84
if ( query_params != null )
@@ -105,19 +102,25 @@ private Client BuildClient(string name = null)
105
102
string endpoint ;
106
103
if ( name != null )
107
104
{
108
- endpoint = _urlPath + "/" + name ;
105
+ endpoint = UrlPath + "/" + name ;
109
106
}
110
107
else
111
108
{
112
- endpoint = _urlPath ;
109
+ endpoint = UrlPath ;
113
110
}
114
- _urlPath = null ; // Reset the current object's state before we return a new one
115
- return new Client ( _host , _requestHeaders , _version , endpoint ) ;
111
+ UrlPath = null ; // Reset the current object's state before we return a new one
112
+ return new Client ( Host , RequestHeaders , Version , endpoint ) ;
116
113
}
117
114
118
- private void AddVersion ( string version )
115
+ public virtual AuthenticationHeaderValue AddAuthorization ( KeyValuePair < string , string > header )
119
116
{
120
- _version = version ;
117
+ string [ ] split = header . Value . Split ( new char [ 0 ] ) ;
118
+ return new AuthenticationHeaderValue ( split [ 0 ] , split [ 1 ] ) ;
119
+ }
120
+
121
+ public virtual void AddVersion ( string version )
122
+ {
123
+ Version = version ;
121
124
}
122
125
123
126
// Magic method to handle special cases
@@ -171,7 +174,13 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
171
174
result = null ;
172
175
return false ;
173
176
}
174
-
177
+
178
+ }
179
+
180
+ public async virtual Task < Response > MakeRequest ( HttpClient client , HttpRequestMessage request )
181
+ {
182
+ HttpResponseMessage response = await client . SendAsync ( request ) ;
183
+ return new Response ( response . StatusCode , response . Content , response . Headers ) ;
175
184
}
176
185
177
186
private async Task < Response > RequestAsync ( string method , String request_body = null , String query_params = null )
@@ -180,24 +189,26 @@ private async Task<Response> RequestAsync(string method, String request_body = n
180
189
{
181
190
try
182
191
{
183
- client . BaseAddress = new Uri ( _host ) ;
192
+ client . BaseAddress = new Uri ( Host ) ;
184
193
string endpoint = BuildUrl ( query_params ) ;
185
194
client . DefaultRequestHeaders . Accept . Clear ( ) ;
186
- foreach ( KeyValuePair < string , string > header in _requestHeaders )
195
+ if ( RequestHeaders != null )
187
196
{
188
- if ( header . Key == "Authorization" )
189
- {
190
- string [ ] split = header . Value . Split ( new char [ 0 ] ) ;
191
- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( split [ 0 ] , split [ 1 ] ) ; ;
192
- }
193
- else if ( header . Key == "Content-Type" )
197
+ foreach ( KeyValuePair < string , string > header in RequestHeaders )
194
198
{
195
- MediaType = header . Value ;
196
- client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( MediaType ) ) ;
197
- }
198
- else
199
- {
200
- client . DefaultRequestHeaders . Add ( header . Key , header . Value ) ;
199
+ if ( header . Key == "Authorization" )
200
+ {
201
+ client . DefaultRequestHeaders . Authorization = AddAuthorization ( header ) ;
202
+ }
203
+ else if ( header . Key == "Content-Type" )
204
+ {
205
+ MediaType = header . Value ;
206
+ client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( MediaType ) ) ;
207
+ }
208
+ else
209
+ {
210
+ client . DefaultRequestHeaders . Add ( header . Key , header . Value ) ;
211
+ }
201
212
}
202
213
}
203
214
@@ -213,8 +224,7 @@ private async Task<Response> RequestAsync(string method, String request_body = n
213
224
RequestUri = new Uri ( endpoint ) ,
214
225
Content = content
215
226
} ;
216
- HttpResponseMessage response = await client . SendAsync ( request ) ;
217
- return new Response ( response . StatusCode , response . Content , response . Headers ) ;
227
+ return await MakeRequest ( client , request ) ;
218
228
219
229
}
220
230
catch ( Exception ex )
0 commit comments