Skip to content

Commit fff871d

Browse files
committed
Merge branch 'master' of git://github.com/jhoerr/RestSharp
Conflicts: RestSharp/RestClient.Async.cs RestSharp/RestClient.Sync.cs
2 parents fad3d6a + a0f67ed commit fff871d

File tree

8 files changed

+286
-139
lines changed

8 files changed

+286
-139
lines changed

RestSharp/Http.Async.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,27 @@ public HttpWebRequest PatchAsync(Action<HttpResponse> action)
7777
return PutPostInternalAsync("PATCH", action);
7878
}
7979

80-
private HttpWebRequest GetStyleMethodInternalAsync(string method, Action<HttpResponse> callback)
80+
/// <summary>
81+
/// Execute an async POST-style request with the specified HTTP Method.
82+
/// </summary>
83+
/// <param name="httpMethod">The HTTP method to execute.</param>
84+
/// <returns></returns>
85+
public HttpWebRequest AsPostAsync(Action<HttpResponse> action, string httpMethod)
86+
{
87+
return PutPostInternalAsync(httpMethod.ToUpperInvariant(), action);
88+
}
89+
90+
/// <summary>
91+
/// Execute an async GET-style request with the specified HTTP Method.
92+
/// </summary>
93+
/// <param name="httpMethod">The HTTP method to execute.</param>
94+
/// <returns></returns>
95+
public HttpWebRequest AsGetAsync(Action<HttpResponse> action, string httpMethod)
96+
{
97+
return GetStyleMethodInternalAsync(httpMethod.ToUpperInvariant(), action);
98+
}
99+
100+
private HttpWebRequest GetStyleMethodInternalAsync(string method, Action<HttpResponse> callback)
81101
{
82102
HttpWebRequest webRequest = null;
83103
try

RestSharp/Http.Sync.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,27 @@ public HttpResponse Patch()
8787
return PostPutInternal("PATCH");
8888
}
8989

90-
private HttpResponse GetStyleMethodInternal(string method)
90+
/// <summary>
91+
/// Execute a GET-style request with the specified HTTP Method.
92+
/// </summary>
93+
/// <param name="httpMethod">The HTTP method to execute.</param>
94+
/// <returns></returns>
95+
public HttpResponse AsGet(string httpMethod)
96+
{
97+
return GetStyleMethodInternal(httpMethod.ToUpperInvariant());
98+
}
99+
100+
/// <summary>
101+
/// Execute a POST-style request with the specified HTTP Method.
102+
/// </summary>
103+
/// <param name="httpMethod">The HTTP method to execute.</param>
104+
/// <returns></returns>
105+
public HttpResponse AsPost(string httpMethod)
106+
{
107+
return PostPutInternal(httpMethod.ToUpperInvariant());
108+
}
109+
110+
private HttpResponse GetStyleMethodInternal(string method)
91111
{
92112
var webRequest = ConfigureWebRequest(method, Url);
93113

RestSharp/IHttp.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public interface IHttp
5151
HttpWebRequest OptionsAsync(Action<HttpResponse> action);
5252
HttpWebRequest PostAsync(Action<HttpResponse> action);
5353
HttpWebRequest PutAsync(Action<HttpResponse> action);
54-
HttpWebRequest PatchAsync(Action<HttpResponse> action);
54+
HttpWebRequest PatchAsync(Action<HttpResponse> action);
55+
HttpWebRequest AsPostAsync(Action<HttpResponse> action, string httpMethod);
56+
HttpWebRequest AsGetAsync(Action<HttpResponse> action, string httpMethod);
5557

5658
#if FRAMEWORK
5759
HttpResponse Delete();
@@ -60,7 +62,9 @@ public interface IHttp
6062
HttpResponse Options();
6163
HttpResponse Post();
6264
HttpResponse Put();
63-
HttpResponse Patch();
65+
HttpResponse Patch();
66+
HttpResponse AsPost(string httpMethod);
67+
HttpResponse AsGet(string httpMethod);
6468

6569
IWebProxy Proxy { get; set; }
6670
#endif

RestSharp/IRestClient.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,44 @@ public interface IRestClient
7777
#endif
7878

7979
Uri BuildUri(IRestRequest request);
80+
81+
/// <summary>
82+
/// Executes a GET-style request and callback asynchronously, authenticating if needed
83+
/// </summary>
84+
/// <param name="request">Request to be executed</param>
85+
/// <param name="callback">Callback function to be executed upon completion providing access to the async handle.</param>
86+
/// <param name="httpMethod">The HTTP method to execute</param>
87+
RestRequestAsyncHandle ExecuteAsyncGet(IRestRequest request, Action<IRestResponse, RestRequestAsyncHandle> callback, string httpMethod);
88+
89+
/// <summary>
90+
/// Executes a POST-style request and callback asynchronously, authenticating if needed
91+
/// </summary>
92+
/// <param name="request">Request to be executed</param>
93+
/// <param name="callback">Callback function to be executed upon completion providing access to the async handle.</param>
94+
/// <param name="httpMethod">The HTTP method to execute</param>
95+
RestRequestAsyncHandle ExecuteAsyncPost(IRestRequest request, Action<IRestResponse, RestRequestAsyncHandle> callback, string httpMethod);
96+
97+
/// <summary>
98+
/// Executes a GET-style request and callback asynchronously, authenticating if needed
99+
/// </summary>
100+
/// <typeparam name="T">Target deserialization type</typeparam>
101+
/// <param name="request">Request to be executed</param>
102+
/// <param name="callback">Callback function to be executed upon completion</param>
103+
/// <param name="httpMethod">The HTTP method to execute</param>
104+
RestRequestAsyncHandle ExecuteAsyncGet<T>(IRestRequest request, Action<IRestResponse<T>, RestRequestAsyncHandle> callback, string httpMethod);
105+
106+
/// <summary>
107+
/// Executes a GET-style request and callback asynchronously, authenticating if needed
108+
/// </summary>
109+
/// <typeparam name="T">Target deserialization type</typeparam>
110+
/// <param name="request">Request to be executed</param>
111+
/// <param name="callback">Callback function to be executed upon completion</param>
112+
/// <param name="httpMethod">The HTTP method to execute</param>
113+
RestRequestAsyncHandle ExecuteAsyncPost<T>(IRestRequest request, Action<IRestResponse<T>, RestRequestAsyncHandle> callback, string httpMethod);
114+
115+
IRestResponse ExecuteAsGet(IRestRequest request, string httpMethod);
116+
IRestResponse ExecuteAsPost(IRestRequest request, string httpMethod);
117+
IRestResponse<T> ExecuteAsGet<T>(IRestRequest request, string httpMethod) where T : new();
118+
IRestResponse<T> ExecuteAsPost<T>(IRestRequest request, string httpMethod) where T : new();
80119
}
81120
}

RestSharp/RestClient.Async.cs

Lines changed: 109 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -32,57 +32,81 @@ public partial class RestClient
3232
/// <param name="callback">Callback function to be executed upon completion providing access to the async handle.</param>
3333
public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse, RestRequestAsyncHandle> callback)
3434
{
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+
}
8346
}
8447

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)
86110
{
87111
var restResponse = ConvertToRestResponse(request, httpResponse);
88112
callback(restResponse, asyncHandle);
@@ -96,16 +120,42 @@ private void ProcessResponse(IRestRequest request, HttpResponse httpResponse, Re
96120
/// <param name="callback">Callback function to be executed upon completion</param>
97121
public virtual RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<IRestResponse<T>, RestRequestAsyncHandle> callback)
98122
{
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));
109124
}
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+
}
110160
}
111161
}

0 commit comments

Comments
 (0)