@@ -32,57 +32,81 @@ public partial class RestClient
32
32
/// <param name="callback">Callback function to be executed upon completion providing access to the async handle.</param>
33
33
public virtual RestRequestAsyncHandle ExecuteAsync ( IRestRequest request , Action < IRestResponse , RestRequestAsyncHandle > callback )
34
34
{
35
- var http = HttpFactory . Create ( ) ;
36
- AuthenticateIfNeeded ( this , request ) ;
37
-
38
- // add Accept header based on registered deserializers
39
- var accepts = string . Join ( ", " , AcceptTypes . ToArray ( ) ) ;
40
- this . AddDefaultParameter ( "Accept" , accepts , ParameterType . HttpHeader ) ;
41
-
42
- ConfigureHttp ( request , http ) ;
43
-
44
- HttpWebRequest webRequest = null ;
45
- var asyncHandle = new RestRequestAsyncHandle ( ) ;
46
-
47
- Action < HttpResponse > response_cb = r => ProcessResponse ( request , r , asyncHandle , callback ) ;
48
-
49
- if ( UseSynchronizationContext && SynchronizationContext . Current != null ) {
50
- var ctx = SynchronizationContext . Current ;
51
- var cb = response_cb ;
52
-
53
- response_cb = resp => ctx . Post ( s => cb ( resp ) , null ) ;
54
- }
55
-
56
- switch ( request . Method )
57
- {
58
- case Method . GET :
59
- webRequest = http . GetAsync ( response_cb ) ;
60
- break ;
61
- case Method . POST :
62
- webRequest = http . PostAsync ( response_cb ) ;
63
- break ;
64
- case Method . PUT :
65
- webRequest = http . PutAsync ( response_cb ) ;
66
- break ;
67
- case Method . DELETE :
68
- webRequest = http . DeleteAsync ( response_cb ) ;
69
- break ;
70
- case Method . HEAD :
71
- webRequest = http . HeadAsync ( response_cb ) ;
72
- break ;
73
- case Method . OPTIONS :
74
- webRequest = http . OptionsAsync ( response_cb ) ;
75
- break ;
76
- case Method . PATCH :
77
- webRequest = http . PatchAsync ( response_cb ) ;
78
- break ;
79
- }
80
-
81
- asyncHandle . WebRequest = webRequest ;
82
- return asyncHandle ;
35
+
36
+ string method = Enum . GetName ( typeof ( Method ) , request . Method ) ;
37
+ switch ( request . Method )
38
+ {
39
+ case Method . PATCH :
40
+ case Method . POST :
41
+ case Method . PUT :
42
+ return ExecuteAsync ( request , callback , method , DoAsGetAsync ) ;
43
+ default :
44
+ return ExecuteAsync ( request , callback , method , DoAsPostAsync ) ;
45
+ }
83
46
}
84
47
85
- private void ProcessResponse ( IRestRequest request , HttpResponse httpResponse , RestRequestAsyncHandle asyncHandle , Action < IRestResponse , RestRequestAsyncHandle > callback )
48
+ /// <summary>
49
+ /// Executes a GET-style request and callback asynchronously, authenticating if needed
50
+ /// </summary>
51
+ /// <param name="request">Request to be executed</param>
52
+ /// <param name="callback">Callback function to be executed upon completion providing access to the async handle.</param>
53
+ /// <param name="httpMethod">The HTTP method to execute</param>
54
+ public virtual RestRequestAsyncHandle ExecuteAsyncGet ( IRestRequest request , Action < IRestResponse , RestRequestAsyncHandle > callback , string httpMethod )
55
+ {
56
+ return ExecuteAsync ( request , callback , httpMethod , DoAsPostAsync ) ;
57
+ }
58
+
59
+ /// <summary>
60
+ /// Executes a POST-style request and callback asynchronously, authenticating if needed
61
+ /// </summary>
62
+ /// <param name="request">Request to be executed</param>
63
+ /// <param name="callback">Callback function to be executed upon completion providing access to the async handle.</param>
64
+ /// <param name="httpMethod">The HTTP method to execute</param>
65
+ public virtual RestRequestAsyncHandle ExecuteAsyncPost ( IRestRequest request , Action < IRestResponse , RestRequestAsyncHandle > callback , string httpMethod )
66
+ {
67
+ request . Method = Method . POST ; // Required by RestClient.BuildUri...
68
+ return ExecuteAsync ( request , callback , httpMethod , DoAsGetAsync ) ;
69
+ }
70
+
71
+ private RestRequestAsyncHandle ExecuteAsync ( IRestRequest request , Action < IRestResponse , RestRequestAsyncHandle > callback , string httpMethod , Func < IHttp , Action < HttpResponse > , string , HttpWebRequest > getWebRequest )
72
+ {
73
+ var http = HttpFactory . Create ( ) ;
74
+ AuthenticateIfNeeded ( this , request ) ;
75
+
76
+ // add Accept header based on registered deserializers
77
+ var accepts = string . Join ( ", " , AcceptTypes . ToArray ( ) ) ;
78
+ this . AddDefaultParameter ( "Accept" , accepts , ParameterType . HttpHeader ) ;
79
+
80
+ ConfigureHttp ( request , http ) ;
81
+
82
+ var asyncHandle = new RestRequestAsyncHandle ( ) ;
83
+
84
+ Action < HttpResponse > response_cb = r => ProcessResponse ( request , r , asyncHandle , callback ) ;
85
+
86
+ if ( UseSynchronizationContext && SynchronizationContext . Current != null )
87
+ {
88
+ var ctx = SynchronizationContext . Current ;
89
+ var cb = response_cb ;
90
+
91
+ response_cb = resp => ctx . Post ( s => cb ( resp ) , null ) ;
92
+ }
93
+
94
+ asyncHandle . WebRequest = getWebRequest ( http , response_cb , httpMethod ) ;
95
+ return asyncHandle ;
96
+ }
97
+
98
+ private static HttpWebRequest DoAsGetAsync ( IHttp http , Action < HttpResponse > response_cb , string method )
99
+ {
100
+ return http . AsGetAsync ( response_cb , method ) ;
101
+ }
102
+
103
+ private static HttpWebRequest DoAsPostAsync ( IHttp http , Action < HttpResponse > response_cb , string method )
104
+ {
105
+ return http . AsPostAsync ( response_cb , method ) ;
106
+ }
107
+
108
+
109
+ private void ProcessResponse ( IRestRequest request , HttpResponse httpResponse , RestRequestAsyncHandle asyncHandle , Action < IRestResponse , RestRequestAsyncHandle > callback )
86
110
{
87
111
var restResponse = ConvertToRestResponse ( request , httpResponse ) ;
88
112
callback ( restResponse , asyncHandle ) ;
@@ -96,16 +120,42 @@ private void ProcessResponse(IRestRequest request, HttpResponse httpResponse, Re
96
120
/// <param name="callback">Callback function to be executed upon completion</param>
97
121
public virtual RestRequestAsyncHandle ExecuteAsync < T > ( IRestRequest request , Action < IRestResponse < T > , RestRequestAsyncHandle > callback )
98
122
{
99
- return ExecuteAsync ( request , ( response , asyncHandle ) =>
100
- {
101
- IRestResponse < T > restResponse = response as RestResponse < T > ;
102
- if ( response . ResponseStatus != ResponseStatus . Aborted )
103
- {
104
- restResponse = Deserialize < T > ( request , response ) ;
105
- }
106
-
107
- callback ( restResponse , asyncHandle ) ;
108
- } ) ;
123
+ return ExecuteAsync ( request , ( response , asyncHandle ) => DeserializeResponse ( request , callback , response , asyncHandle ) ) ;
109
124
}
125
+
126
+ /// <summary>
127
+ /// Executes a GET-style request and callback asynchronously, authenticating if needed
128
+ /// </summary>
129
+ /// <typeparam name="T">Target deserialization type</typeparam>
130
+ /// <param name="request">Request to be executed</param>
131
+ /// <param name="callback">Callback function to be executed upon completion</param>
132
+ /// <param name="httpMethod">The HTTP method to execute</param>
133
+ public virtual RestRequestAsyncHandle ExecuteAsyncGet < T > ( IRestRequest request , Action < IRestResponse < T > , RestRequestAsyncHandle > callback , string httpMethod )
134
+ {
135
+ return ExecuteAsyncGet ( request , ( response , asyncHandle ) => DeserializeResponse ( request , callback , response , asyncHandle ) , httpMethod ) ;
136
+ }
137
+
138
+ /// <summary>
139
+ /// Executes a POST-style request and callback asynchronously, authenticating if needed
140
+ /// </summary>
141
+ /// <typeparam name="T">Target deserialization type</typeparam>
142
+ /// <param name="request">Request to be executed</param>
143
+ /// <param name="callback">Callback function to be executed upon completion</param>
144
+ /// <param name="httpMethod">The HTTP method to execute</param>
145
+ public virtual RestRequestAsyncHandle ExecuteAsyncPost < T > ( IRestRequest request , Action < IRestResponse < T > , RestRequestAsyncHandle > callback , string httpMethod )
146
+ {
147
+ return ExecuteAsyncPost ( request , ( response , asyncHandle ) => DeserializeResponse ( request , callback , response , asyncHandle ) , httpMethod ) ;
148
+ }
149
+
150
+ private void DeserializeResponse < T > ( IRestRequest request , Action < IRestResponse < T > , RestRequestAsyncHandle > callback , IRestResponse response , RestRequestAsyncHandle asyncHandle )
151
+ {
152
+ IRestResponse < T > restResponse = response as RestResponse < T > ;
153
+ if ( response . ResponseStatus != ResponseStatus . Aborted )
154
+ {
155
+ restResponse = Deserialize < T > ( request , response ) ;
156
+ }
157
+
158
+ callback ( restResponse , asyncHandle ) ;
159
+ }
110
160
}
111
161
}
0 commit comments