Skip to content

Getting Started with Consumer Data SDK

This SDK helps you to interact with Consumer Data API. Helpful methods include:

  • Finding consumer information by passing in a valid phone number
  • Simple response object that provides information about the consumer's phone number.
  • Helpful extension methods to know if the consumer is blacklisted

In order to get started with Hubtel.ConsumerData.Sdk, do the following activities:

Install-Package Hubtel.ConsumerData.Sdk

This command is run in the Package Manager Console in Visual Studio, and it adds the SDK to your project.

c#
    Install-Package Hubtel.ConsumerData.Sdk

Add to appsettings.json

  • "BearerToken": "<token>" is used to set the bearer token for authentication. The <token> placeholder should be replaced with the actual token value.

  • "Url": "consumer_data_url" This key-value pair sets the URL for the consumer data service. The consumer_data_url should be replaced with the actual URL of the service.

  • "RedisHost": "localhost" This key-value pair sets the host for a Redis database. In this case, the host is set to localhost, which means the Redis database is running on the same machine as the application.

  • "RedisPort": "<port>" This key-value pair sets the port number for the Redis database. The <port> placeholder should be replaced with the actual port number.

  • "RedisDbNumber": "<db_number>" is key-value pair sets the database number for the Redis database. The <db_number> placeholder should be replaced with the actual database number.

json
"ConsumerDataConfig": {
    "BearerToken": "<token>",
    "Url": "consumer_data_url",
    "RedisHost": "localhost",
    "RedisPort": "<port>",
    "RedisDbNumber": "<db_number>"
  },

Add to IServiceCollection in Program.cs or Startup.cs

  • The services.AddHubtelConsumerDataSdk(o => {...}) is a method call that adds the HubtelConsumerDataSdk service to the IServiceCollection .

  • IServiceCollection is an interface in .NET Core that is used for dependency injection. It maintains a collection of service descriptors, which are used to build a service container that provides requested services.

  • Inside the method call, a lambda expression is used to configure the HubtelConsumerDataSdk service. The o parameter represents the options object for the HubtelConsumerDataSdk service.

  • The Url, BearerToken, RedisHost, RedisPort, and RedisDbNumber properties of the options object are being set using values from the _configuration object. The _configuration object is an instance of IConfiguration, which is a standard .NET interface for working with configuration data. It's used to retrieve configuration settings from various sources like environment variables, command-line arguments, or configuration files like appsettings.json.

  • The keys used to retrieve the values (like "ConsumerDataConfig:Url") correspond to the keys in the configuration source. For example, in appsettings.json, you might have a ConsumerDataConfig section with Url, BearerToken, RedisHost, RedisPort, and RedisDbNumber properties.

  • The GetValue<T> method is used to retrieve the value of a configuration setting and cast it to a specific type. In this case, RedisHost is cast to a string, and RedisPort and RedisDbNumber are cast to int.

c#
      services.AddHubtelConsumerDataSdk(o =>
            {
                o.Url = _configuration["ConsumerDataConfig:Url"];
                o.BearerToken = _configuration["ConsumerDataConfig:BearerToken"];
                o.RedisHost = _configuration.GetValue<string>("ConsumerDataConfig:RedisHost");
                o.RedisPort = _configuration.GetValue<int>("ConsumerDataConfig:RedisPort");
                o.RedisDbNumber = _configuration.GetValue<int>("ConsumerDataConfig:RedisDbNumber");
            });

Inject and consume service methods

  • The code below shows the usage of an instance of IConsumerDataApi to find account information and perform some operations based on the response.

  • The private readonly IConsumerDataApi _consumerDataApi is a of type IConsumerDataApi, which is an interface defining methods for interacting with a consumer data API. The private keyword means this field can only be accessed within the same class. The readonly keyword means that the value of this field can only be assigned at the time of declaration or in the constructor of the class.

  • The line var response = await _consumerDataApi.FindAccount("<phone_number>") is calling the FindAccount method on the _consumerDataApi instance. The FindAccount method takes a phone number as a parameter and returns a task representing the asynchronous operation. The await keyword is used to pause the execution of the method until the awaited task completes. The result of the task is assigned to the response variable. The <phone_number> is a placeholder and should be replaced with an actual phone number when using this code.

  • The if (response.IsBlacklisted()) {...} block checks if the response indicates that the account is blacklisted. If it is, the code inside the block is executed.

  • The if (response.IsSuccessful) {...} block checks if the response indicates that the operation was successful. If it was, the code inside the block is executed. The var name = response.Data.GetName() line inside this block retrieves the name from the data in the response.

c#
    private readonly IConsumerDataApi _consumerDataApi;    

    //P.S. be sure to include as constructor parameter

    var response = await _consumerDataApi.FindAccount("<phone_number>");
    
    if (response.IsBlacklisted())
        {
            //your logic here
        }
        if (response.IsSuccessful)
        {
            //do something
            var name = response.Data.GetName();
        }

Was this page helpful?

Happy React is loading...