The next item that is on my list, which I just though of, is having read only arrays that contain values for API calls so that the user does not have to remember them. For example, according to the documentation on News Data’s website, you can call the API with a country code. These country codes are in a two-letter format (ISO 3166). There are quite a few of them, which makes it hard to remember what they are. So instead of remembering, we can just make another class that contains dictionaries with these values. So instead of remembering ca for Canada, they can just put Canada. Capisce?

Building the Class

This class will just be another service that contains all of the arrays, dictionaries, etc. that is needed. We will name this class OptionsService.cs and it will reside in (you probably guessed it) the Services directory. As of right now, this class will have two read only dictionaries and one enum. This file may contain more the further we progress with this application. The two dictionaries will be for the aforementioned country codes and language codes. The enum will contain the different categories to choose from.

Country Dictionary

We will start with the country dictionary. Dictionaries by default are not read only. Keys and values can be added and taken away at any time. In our instance we want to make them read only. I came across a new interface named IReadOnlyDictionary. Two cool things about this interface are, it will make our dictionary read only and you can initialize this object with a regular dictionary, so it is very easy to use. Here is the Microsoft Docs page on it. To use IReadOnlyDictionary, I had to add the System.Collections.ObjectModel namespace to my global Usings.cs file. Below is a little snippet of the country dictionary so that you can see how it is initialized:

public static IReadOnlyDictionary<string, string> Country { get; private set; } = new Dictionary<string, string>()
{
    { "argentina", "ar" },
    { "australia", "au" },
    { "austria", "at" },
    /*
     *  GAP
     */
    { "united kingdom", "gb" },
    { "united states of america", "us" },
    { "venezuela", "ve" }
};

Category Enum

Next up is the category enum type. News Data provides categories as a query option. Here I used the enum type in hopes of showcasing different data types instead of using the same ones over and over again. This will be used if the user would like to add a category filter to their search query. This variable might change in the future. We will see though. Below is the enum:

public enum Category
{
    business,
    entertainment,
    environment,
    food,
    health,
    politics,
    science,
    sports,
    technology,
    top,
    world
}

Language Dictionary

The final library that I wanted to add, is the language dictionary. Similar to the country dictionary, News Data provides a lot of language codes to choose from. These are also two-letter codes. Here is a snippet of the dictionary:

public static IReadOnlyDictionary<string, string> Language { get; private set; } = new Dictionary<string, string>()
{
    { "arabic", "ar" },
    { "bosnian", "bs" },
    { "bulgarian", "bg" },
    /*
     *  GAP
     */
    { "thai", "th" },
    { "turkish", "tr" },
    { "ukrainian", "uk" }
};

Testing

Like before, I unit tested all of these libraries to ensure that I was able to access them from other areas of the application and that I was retrieve data from them properly.

By adding the various libraries above, we were able to make it easier for the user to create complex queries to the API without having to remember specific values. This will also help by making it harder for the user to query the API incorrectly. Later we will use these libraries to create the query strings. If you missed out, here is Part II. Questions, comments, concerns? Hit me up on Twitter. Until next time.