kevbite / CompaniesHouse.NET

A simple .NET client wrapper for CompaniesHouse API
MIT License
37 stars 44 forks source link

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

Closed mattu08 closed 4 years ago

mattu08 commented 4 years ago

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

kevbite commented 4 years ago

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.

mattu08 commented 4 years ago

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();

kevbite commented 4 years ago

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 👍.

mattu08 commented 4 years ago

Cheers for looking into this @kevbite 👍 Sounds good!

kevbite commented 4 years ago

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);
mattu08 commented 4 years ago

@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! 👍

kevbite commented 4 years ago

No worries @mattu08, glad it sorted out your problem

mattu08 commented 4 years ago

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

sakisk commented 4 years ago

@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 👏 🏅

mattu08 commented 4 years ago

@sakisk @kevbite I see what I did here 🤦‍♂️😂 I @ Tagged the wrong person, But i agree great idea!

kevbite commented 4 years ago

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

mattu08 commented 4 years ago

@kevbite No problem at all and thanks for your help! ☕ keep up the good work! 👍