Introduction

When building mobile applications you might need to implement a video feature into your app. But what if an actual video player doesn’t make sense in your app. In that case, you might want to get the most relevant data from the video and display it in your app. When a user clicks on that item, they will be redirected to the YouTube app.

In this post, we will discuss in detail an API that connects another API to extract the video data from a YouTube channel. The API will then take a request with the channel id and return a list that includes data, such as title, video URL, and thumbnail.

The source code for this project is available on GitHub: https://github.com/nativoplus/YouTubeApi

Getting Started

A few things before we continue. This API is written in ASP.Net Core 2.1 and all the packages are either in the latest release or the pre-release version. Also, we used Visual Studio 2017 Preview for this example. Please keep this in mind, as we will be using some libraries that may trigger issues in Visual Studio 2017.

ASP.Net Core 2.1 Project

The project is divided into different class libraries so we can have a better project structure. If you want to learn more about this project structure you may go here.

One thing before we continue

In this article, we will be talking about the appsettings.json file on multiple occasions. You may find this file on the startup project. This file will hold all of the configurations settings for this app.

About Services…

Since we are going to be using Dependency Injection we need a class capable of registering all of our services so that we can inject them and use them to get data. This service will also work to configure our settings from appsettings.json. Also, this service will implement a retry policy so that if the request to the external API fails, then the retry policy is executed.

Things to note about this service are:

  • Static Class
    Since we are only going to call this class once in the Startup.cs, we declare it as a static class.
  • The Method
    This method uses two parameters:
    1. Services – An extension method of the type IServiceCollection. By doing this we can keep the Startup.cs clean.
    2. Configuration – Type of IConfiguration. This is used to set the configurations at startup.
  • The Variables
    1. RetryPolicy – We are using Polly to handle retry attempts. Read more information about Polly here.
    2. YouTubeApiConfig – Set the configuration values from the appsettings.json.
Since our API is calling an external API we must have a service responsible for that task. This service must send the request with the custom parameters to the external API.

Things to note about this service are:

  • Dependency Injection.
    Since we will be sending a request to YouTube API, we need to include the HttpClient class.
    1. baseAddress
    2. key
    3. channelId
    4. part
    5. maxResults.he Method
      Our method will use five parameters:
  • The Variables
    1. _client– Unsing HttpClient Factory to send the HTTP request to the YouTube API. Read more information about HttpClient Factory here.
    2. youtubeDTO – A new instance of the YouTubeDTO will then deserialize a string into a JSON Object.
    3. url – Set the request URL address with the parameters it expects.
    4. response – Get the response of the API.
    5. data – An empty string that will then receive a stream.
    6. stream – Get the response as a stream to improve performance.
    7. myStream – A new instance of the class StreamReader that takes stream as it’s input parameter.

Now that we know how the YouTubeChannelHttpService service works, let’s move on to our API service.

This service will be responsible for sending our request and then passing it to the YouTubeHttpService class. Once the request is successfully processed, we will receive our response:

Things to note about this service are:

  • Dependency Injection.
    Similar to the previous class, we need to do Dependency Injection. In this class, we will be adding IYouTubeApiConfig and YouTubeChannelHttpService.
  • The Method
    On this method, we just need the request that we will be sending to the external API.
  • The Variables
    1. _youTubeApiConfig – Get the configuration values from the YouTubeApiConfig Model using IOptions.
    2. _youTubeChannelHttpService – An interface of type YouTubeChannelHttpService to give us access to the class’s method YouTubeChannelsSearch.
    3. getResponse – Get the response from the external API.
    4. response – Gets the response mapped as an IYouTubeResponse type.

Since the response from the YouTube API is different compared to our response, we will create a class that will map the properties from the external API to our response model. In order to achieve this, we use AutoMapper, which you can read more about here.

The API

Now that we have covered the service, let’s move to the actual API project.

In our Startup file is where we are going to initialize our Configuration Settings. To do so we need to add this piece of code,  services.ServicesConfigurationForYouTubeApi(Configuration), to the ConfigureServices method.

Also, notice that we are using Swagger UI and in order to use it we need to implement API Versioning. If you want to find more information about API Versioning, you may go here.

Swagger UI allows us to see all the APIs that we have in our project. You may also see information about the type fAPI, what it expects, and what it returns, and then test the API directly from there. To access Swager UI, go to localhost/swagger on the browser.

Testing the Api

To test our API, we are using POSTMAN.
For convenience, you may use this link to access the POSTMAN collection. Please note that you may need to change the localhost port.

In order to get the channel’s video list, send a request that has the channelId in the body.

Assuming the request was successful, we will get a list of video data as a response.

Conclusion

In this article, we went through the key components of an API that fetched a channel’s video data. We saw the services, the classes that compose the project, and the settings configuration of the project. At this point, you should be able to understand how to use this project or how to create a similar project on ASP.Net Core 2.1.