Skip to content

Commit

Permalink
Merge pull request #811 from aron-ridgway-deltatre/base-client-event-…
Browse files Browse the repository at this point in the history
…handlers

feat add event handlers to make request async
  • Loading branch information
douglasmiller authored Dec 12, 2023
2 parents 0ce21d3 + 0570da7 commit f2c5780
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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

0 comments on commit f2c5780

Please sign in to comment.