Code Examples are provided throughout this documentation to enhance the understanding of the functionality of the Babel Licensing Management API. In order to simplify the writing of these examples and provide a consistent framework, we introduce the WebApiClient class. This class is designed to ease the interaction with the API by efficiently handling HTTP communication, authentication, and JSON parsing of response messages.
using System.Net.Http;
using System.Threading.Tasks;
using Google.Protobuf;
using Babel.Licensing.Service.Management;
public class WebApiClient
{
public string WebApiUrl => "https://localhost:5455/";
public string WebApiKey => "<THE WEB APY KEY>";
public HttpClient CreateHttpClient()
{
var httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback =
(HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors) => true;
return new HttpClient(httpClientHandler) {
BaseAddress = new System.Uri(WebApiUrl)
};
}
public StringContent ToJSonContent(IMessage message)
{
var jsonRequest = JsonFormatter.Default.Format(message);
var content = new StringContent(jsonRequest, System.Text.Encoding.UTF8,
"application/json");
return content;
}
public HttpRequestMessage MakeApiRequest(IMessage message, HttpMethod method, string url)
{
var request = new HttpRequestMessage(method, url) {
Content = ToJSonContent(message)
};
request.Headers.Add("x-api-key", WebApiKey);
return request;
}
public async Task<TResponse> ParseResponseAsync<TResponse>(HttpResponseMessage response)
where TResponse : IMessage, new()
{
var jsonResponse = await response.Content.ReadAsStringAsync();
return JsonParser.Default.Parse<TResponse>(jsonResponse);
}
}
Imports System.Net.Http
Imports System.Threading.Tasks
Imports Google.Protobuf
Imports Babel.Licensing.Service.Management
Public Class WebApiClient
Public ReadOnly Property WebApiUrl As String
Get
Return "https://localhost:5455/"
End Get
End Property
Public ReadOnly Property WebApiKey As String
Get
Return "<THE WEB APY KEY>"
End Get
End Property
Public Function CreateHttpClient() As HttpClient
Dim httpClientHandler As New HttpClientHandler()
AddHandler httpClientHandler.ServerCertificateCustomValidationCallback,
Function(HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors) True
Dim client As New HttpClient(httpClientHandler)
client.BaseAddress = New Uri(WebApiUrl)
Return client
End Function
Public Function ToJSonContent(message As IMessage) As StringContent
Dim jsonRequest As String = JsonFormatter.Default.Format(message)
Dim content As New StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json")
Return content
End Function
Public Function MakeApiRequest(message As IMessage, method As HttpMethod, url As String) As HttpRequestMessage
Dim request As New HttpRequestMessage(method, url)
request.Content = ToJSonContent(message)
request.Headers.Add("x-api-key", WebApiKey)
Return request
End Function
Public Async Function ParseResponseAsync(Of TResponse As {IMessage, New})(response As HttpResponseMessage) As Task(Of TResponse)
Dim jsonResponse As String = Await response.Content.ReadAsStringAsync()
Return JsonParser.Default.Parse(Of TResponse)(jsonResponse)
End Function
End Class
The class structure is as follows:
WebApiUrl and WebApiKey: These properties store the API's base URL and the API key for authentication, ensuring secure access to the Babel Licensing Service.
CreateHttpClient: This method sets up an HttpClient instance, configuring it to bypass SSL certificate validation and to use the API's base URL. This is particularly useful for development and testing environments.
ToJSonContent: Converts a Protobuf message (IMessage) to a JSON string and encapsulates it in a StringContent object, ready for HTTP transmission. This method is essential for sending requests with JSON payloads.
MakeApiRequest: Constructs an HttpRequestMessage using the specified HTTP method, URL, and Protobuf message. It also includes the API key in the request headers. This method is a cornerstone for sending requests to different API endpoints.
ParseResponseAsync: An asynchronous method that parses the JSON response from the API back into a Protobuf message (IMessage). This generic method allows for flexible and type-safe parsing of various response types.
By utilizing the WebApiClient class, C# developers can seamlessly construct requests, send them to the Babel Licensing API, and handle the responses in a type-safe manner. This abstraction layer provided by the class not only streamlines the process of writing code examples but also ensures that these examples are clear, concise, and easy to replicate in real-world scenarios.