Skip to content

Commit 7bb6fb0

Browse files
committed
Implemented ExecuteAsync overload as an extension method of IRestClient.
The concrete implementation `RestClient` has a nice `ExecuteAsync` method that's simply a wrapper to a call of another method on `IRestClient`. Since this method can be completely implemented in terms of the `IRestClient` interface, it makes sense to make it an extension method of `IRestClient`. The benefit is that I don't need to pass around a `RestClient` in my own API but I can pass around an `IRestClient`. Makes unit testing easier too, and that comes for free! Added bonus! :)
1 parent db44f42 commit 7bb6fb0

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

RestSharp/RestClient.Async.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ namespace RestSharp
2525
{
2626
public partial class RestClient
2727
{
28-
29-
/// <summary>
30-
/// Executes the request and callback asynchronously, authenticating if needed
31-
/// </summary>
32-
/// <param name="request">Request to be executed</param>
33-
/// <param name="callback">Callback function to be executed upon completion</param>
34-
public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<RestResponse> callback)
35-
{
36-
return ExecuteAsync(request, (response, handle) => callback(response));
37-
}
38-
3928
/// <summary>
4029
/// Executes the request and callback asynchronously, authenticating if needed
4130
/// </summary>

RestSharp/RestClientExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace RestSharp
4+
{
5+
public static class RestClientExtensions
6+
{
7+
/// <summary>
8+
/// Executes the request and callback asynchronously, authenticating if needed
9+
/// </summary>
10+
/// <param name="client">The IRestClient this method extends</param>
11+
/// <param name="request">Request to be executed</param>
12+
/// <param name="callback">Callback function to be executed upon completion</param>
13+
public static RestRequestAsyncHandle ExecuteAsync(this IRestClient client, IRestRequest request, Action<RestResponse> callback)
14+
{
15+
return client.ExecuteAsync(request, (response, handle) => callback(response));
16+
}
17+
}
18+
}

RestSharp/RestSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="Extensions\MonoHttp\Helpers.cs" />
108108
<Compile Include="Extensions\MonoHttp\HtmlEncoder.cs" />
109109
<Compile Include="Extensions\MonoHttp\HttpUtility.cs" />
110+
<Compile Include="RestClientExtensions.cs" />
110111
<Compile Include="Extensions\StringExtensions.cs" />
111112
<Compile Include="Http.Sync.cs" />
112113
<Compile Include="HttpCookie.cs" />

0 commit comments

Comments
 (0)