Extensibility- API Client Hooks

Learn how to extend the BELLATRIX API client using hooks.

Introduction

Another way to execute BELLATRIX is to create an API client plugin. You can execute your logic in various points such as:

  • OnClientInitialized - executed after the API client is initialized. Here you can fine-tune the client.
  • OnRequestTimeout - executed if some of the requests timeout.
  • OnMakingRequest - executed before making request.
  • OnRequestMade - executed after the request is made.
  • OnRequestFailed - executed in case of request failure.

To create a custom plugin, you need to derive the class- ApiClientExecutionPlugin. Then you can override some of its protected methods. You can create plugins for logging the request failures, modifying the requests. The possibilities are limitless.

Example

public class LogRequestTimeApiClientExecutionPlugin : ApiClientExecutionPlugin
{
    private Stopwatch _requestStopwatch = Stopwatch.StartNew();

    protected override void OnMakingRequest(object sender, RequestEventArgs client) => _requestStopwatch = Stopwatch.StartNew();

    protected override void OnRequestMade(object sender, ResponseEventArgs client)
    {
        _requestStopwatch.Stop();
        Console.WriteLine($"Request made for {_requestStopwatch.ElapsedMilliseconds}");
    }
}
public class ApiClientHooksTests : APITest
{
    public override void TestsArrange() => App.AddApiClientExecutionPlugin<LogRequestTimeApiClientExecutionPlugin>();

    [Test]
    public void GetAlbumById()
    {
        var request = new RestRequest("api/Albums/10");

        var client = App.GetApiClientService();

        var response = client.Get<Albums>(request);

        Assert.AreEqual(10, response.Data.AlbumId);
    }

    [Test]
    public void SecondGetAlbumById()
    {
        var request = new RestRequest("api/Albums/10");

        var client = App.GetApiClientService();

        var response = client.Get<Albums>(request);

        Assert.AreEqual(10, response.Data.AlbumId);
    }
}

Explanations

public override void TestsArrange() => 
  App.AddApiClientExecutionPlugin<LogRequestTimeApiClientExecutionPlugin>();

The plugin needs to be registered through App service method AddApiClientExecutionPlugin.

[OneTimeSetUp]
public void AssemblyInitialize()
{
    App.AddApiClientExecutionPlugin<LogRequestTimeApiClientExecutionPlugin>();
}

Usually, this happens in the AssemblyInitialize method so that it is executed once before all tests.