IHttpClientService
Overview
The IHttpClientService in Wisej.AI is utilized whenever an HTTP request is necessary, such as when interacting with LLM providers, vector databases, web searches, and other external services.
For instance, here's how the WebSearchServiceBase
class utilizes the IHttpClientService
:
public WebSearchServiceBase(string url)
{
_client = ServiceHelper.GetService<IHttpClientService>();
}
internal virtual IHttpClientService Client
{
get
{
if (!_clientInitialized)
{
_clientInitialized = true;
_client.SetHandler(new HttpClientHandler
{
AllowAutoRedirect = true,
#if !NETCOREAPP
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
#else
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli
#endif
});
var apiKey = GetApiKey();
if (!String.IsNullOrEmpty(apiKey) && !String.IsNullOrEmpty(this.Authentication))
{
_client.AddDefaultHeader(this.Authentication, apiKey);
}
}
return _client;
}
}
private bool _clientInitialized;
private IHttpClientService _client;
The sample code above class creates an instance of the service and utilizes the SetHandler()
method to enable decompression. It also employs the AddDefaultHeader()
method to configure the authentication method and API key. Extending from WebSearchServiceBase
, the BingWebSearchService
class uses the GetAsync()
method on the service instance to perform its operations.
Default Implementation
The default implementation of IHttpClientService
is in DefaultHttpClientService
and leverages an instance of System.Net.Http.HttpClient
to handle HTTP requests.
This service acts as an intermediary between the application code utilizing it and the HttpClient
instance. This design enables developers to implement their own version of IHttpClientService
, allowing them to intercept and customize all HTTP interactions as needed.
Last updated