Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat add event handlers to make request async #811

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Recurly.Tests/BaseClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ public void WillTriggerHookIfAvailable()
Assert.Equal("benjamin", resource.MyString);
}

[Fact]
public async void WillTriggerHookIfAvailableAsync()
{
var client = MockClient.Build(SuccessResponse(System.Net.HttpStatusCode.OK));
var mockHandler = new Mock<IEventHandler>();
mockHandler
.Setup(x => x.OnRequest(It.IsAny<Recurly.Http.Request>()));
mockHandler
.Setup(x => x.OnResponse(It.IsAny<Recurly.Http.Response>()));
client.AddEventHandler(mockHandler.Object);
MyResource resource = await client.GetResourceAsync("benjamin", "param1", new DateTime());

Assert.Equal("benjamin", resource.MyString);
mockHandler.Verify(v => v.OnRequest(It.IsAny<Recurly.Http.Request>()), Times.Once());
mockHandler.Verify(v => v.OnResponse(It.IsAny<Recurly.Http.Response>()), Times.Once());
}

private Mock<IRestResponse<MyResource>> SuccessResponse(System.Net.HttpStatusCode status)
{
var data = new MyResource()
Expand Down
12 changes: 12 additions & 0 deletions Recurly/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,24 @@ public int Timeout
Body = body
};
var restRequest = BuildRequest(method, url, body, queryParams, options);

foreach (var handler in this.EventHandlers)
{
handler.OnRequest(httpRequest);
}

var task = RestClient.ExecuteAsync<T>(restRequest, cancellationToken);
return await task.ContinueWith(t =>
{
var restResponse = t.Result;
this.HandleResponse(restResponse);
var httpResponse = Http.Response.Build(restResponse, httpRequest);

foreach (var handler in this.EventHandlers)
{
handler.OnResponse(httpResponse);
}

if (restResponse.Data is Resource)
restResponse.Data.SetResponse(httpResponse);
return restResponse.Data;
Expand Down
Loading