Consume 3rd party API


Citrix NetScaler exposes its Nitro API for management through a series of Json endpoints. Although there are official packages from Citrix, they are for .NET Framework and not for .Net Core. I’m working on my own implementation, which will format all the request in the correct format before executing the requests.

I’m looking into the native HttpClient for .NET Core, but as I like the way how ServiceStack handles requests towards 3rd party APIs, that option is viable too.

However, the documentation states that for .Net Core it is better to use the HttpClient ServiceClient because of a sub-optimal implementation. Am I correct that ServiceStack.Text is still using HttpWebRequest?

So what would be the better option?

By using HttpClient, I am able to override the HttpMessageHandler to disable SSL certificate checks.
Although a bad security practice, I need to be able to disable them as many NetScaler management interfaces do not have a proper certificate attached.

ServiceStack’s Service Clients are always going to be optimized for consuming ServiceStack Services, in general we’d recommend using HTTP Utils for consuming 3rd Party APIs.

The Stripe 3rd Party API shows how you can build a similar “typed API” on top of HTTP Utils using strong-typed DTOs, see StripeGateway.cs. It requires a custom jsConfigScope in Stripe because their ruby snake_case conventions is very different to .NET’s default conventions and implementing IUrlFilter.ToUrl() in some cases to handle their unusual complex type conventions in query strings.

Well HttpWebRequest.cs is just a wrapper over HttpClient so you should only using the async APIs in .NET Core otherwise you’d be calling “sync over async” where performance with suffer.

Yes HttpUtils.cs still uses HttpWebRequest.

For 3rd Party APIs I’d just use the async APIs in HTTP Utils myself, if it’s only for internal use I’d just be constructing requests with URL Extensions but if it’s to provide a public typed API over a 3rd Party library I’d take the same declarative Typed DTO approach in Stripe API.

So as a quick follow-up after skimming through the StripGateway.cs:
Can I override the HttpMessageHandler in Http Utils?

There is no HttpMessageHandler exposed in HttpWebRequest but if you just wanted to disable the SSL certificate checks you can access the ServerCertificateValidationCallback with:

url.GetJsonFromUrlAsync(requestFilter:req => 
    req.ServerCertificateValidationCallback = ...);