kevbite/CompaniesHouse.NET

Where can I get the raw JSON from the request within this library for reference.

mattu08 opened this issue ยท 12 comments

Hi,

Where can I get the raw JSON from the request within this library for reference.

Is there an entry point so I can grab the raw JSON coming in from the API request just to have the request as backup.

Kind regards,

Matt

The requests for the API are mostly GET requests with the parameters on a query string, these are built up in different places depending on which search/lookup you are executing.

Ideally, if you want to inspect the requests and responses from the library, we'd need to extend it so you can pass in a custom DelegatingHandler.

We could maybe accept a client handler in the settings and plumb it in at the HttpClientFactory.

I'm quite surprised this is not already extendable in this way but I guess people are mocking out the ICompaniesHouseClient.

Interesting, maybe something like the following that is addressed in CompaniesHouse.Tests/StubHttpMessageHandler.cs e.g. using HttpClient with the HttpResponseMessage to pull back the RAW JSON request. Example using: message.Content.ReadAsStringAsync();

Yeah something like that, once we allow people to add in their own HttpMessageHandler to use as a handler then they'll be able to take over from that point.

I'll add something later tonight, and give you an example ๐Ÿ‘.

Cheers for looking into this @kevbite ๐Ÿ‘ Sounds good!

I've just updated the code in the latest version (7.6.0) so you can customize the HttpMessageHandler that is being used.

Here's some sample code to get it working, you'll need your own custom delegating handler:

public class MyDelegatingHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        string requestBody;
        if (request.Content != null)
        {
            requestBody = await request.Content.ReadAsStringAsync();
        }

        var result = await base.SendAsync(request, cancellationToken);
        var responseBody = await result.Content.ReadAsStringAsync();

        return result;
    }
}

Then within the creation of the CompaniesHouseClient you can pass in some settings like the following:

 Func<HttpMessageHandler> httpMessageHandlerCreator =
    () => new MyDelegatingHandler()
    {
        InnerHandler = CompaniesHouseSettings.DefaultHttpMessageHandlerCreator()
};

var settings = new CompaniesHouseSettings(CompaniesHouseUris.Default, Keys.ApiKey, httpMessageHandlerCreator);

_client = new CompaniesHouseClient(settings);

@kevbite Excellent work! I managed to get this working with the above examples, slightly modified by pulling in the var responseBody = await result.Content.ReadAsStringAsync(); directly from the public class MyDelegatingHandler : DelegatingHandler, Class. Again thanks for your help! ๐Ÿ‘

No worries @mattu08, glad it sorted out your problem

@sakisk I'll close issue. If you had a buymeacoffee i'd drop you a few quid, thanks for your help!

@sakisk I'll close issue. If you had a buymeacoffee i'd drop you a few quid, thanks for your help!

thanks @mattu08 i'll set it up and first thing to do would be to buy a coffee for @kevbite that has been maintaining this code for all these years now ๐Ÿ‘ ๐Ÿ…

@sakisk @kevbite I see what I did here ๐Ÿคฆโ€โ™‚๏ธ๐Ÿ˜‚ I @ Tagged the wrong person, But i agree great idea!

No worries glad to be useful :-) @sakisk has bought me so many coffees's in our time... โ˜• them were the days before COVID-19 ๐Ÿ˜ข.

I do have a Buy me a coffee account if really fancy buying me a coffee ๐Ÿ˜‰

buymeacoff.ee/d5qgmSF

@kevbite No problem at all and thanks for your help! โ˜• keep up the good work! ๐Ÿ‘