Code Examples

Streamlining API Interactions with code examples

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

The class structure is as follows:

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

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

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

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

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

Last updated