Generic Class makes .NET Core HttpClient Calls

Note: This class has been updated.

Here’s a generic class that abstracts async HttpClient calls in .NET Core. It can be used, for example, to make calls from a desktop application or web site back end to a data API.

POST, PUT, DELETE and GET are implemented. Because it’s generic, any data type can be involved. Also, the class is static, so there’s no need to instantiate it. For example, to read a single Application object from a data API endpoint url:

Application application = await HTTPClientHelper.GetAsync<Application>(url, MySecretKey);

The url, of course, is the data API endpoint. MySecretKey is placed in an HTTP Header field named APIKey. This is required by my particular API for security. All users of the API must send this key.

This is simpler than JSON Web Tokens (JWT). It uses only a single secret key that every client must know. Users log in to a web application using vanilla Identity. The web application knows the secret key, so it can access data from the API.

using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace ITProjects.Models
{
    public static class HTTPClientHelper
    {
        #region Abstract, Async, static HTTP functions for GET, POST, PUT, DELETE               
        public static async Task<T> GetAsync<T>(string url, string APIKey)
        {
            T data;

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("APIKey", APIKey);
                using (HttpResponseMessage response = await client.GetAsync(url))
                using (HttpContent content = response.Content)
                {
                    string d = await content.ReadAsStringAsync();
                    if (d != null)
                    {
                        data = JsonConvert.DeserializeObject<T>(d);
                        return (T)data;
                    }
                }
            }

            Object o = new Object();
            return (T)o;
        }

        public static async Task<T> PostAsync<T>(string url, 
                                                 HttpContent contentPost, 
                                                 string APIKey)
        {
            T data;

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("APIKey", APIKey);
                using (HttpResponseMessage response = await client.PostAsync(url, 
                                                                             contentPost))
                using (HttpContent content = response.Content)
                {
                    string d = await content.ReadAsStringAsync();
                    if (d != null)
                    {
                        data = JsonConvert.DeserializeObject<T>(d);
                        return (T)data;
                    }
                }
            }
            Object o = new Object();
            return (T)o;
        }

        public static async Task<T> PutAsync<T>(string url, 
                                                HttpContent contentPut, 
                                                string APIKey)
        {
            T data;

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("APIKey", APIKey);
                using (HttpResponseMessage response = await client.PutAsync(url, 
                                                                            contentPut))
                using (HttpContent content = response.Content)
                {
                    string d = await content.ReadAsStringAsync();
                    if (d != null)
                    {
                        data = JsonConvert.DeserializeObject<T>(d);
                        return (T)data;
                    }
                }
            }
            Object o = new Object();
            return (T)o;
        }

        public static async Task<T> DeleteAsync<T>(string url, string APIKey)
        {
            T newT;

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("APIKey", APIKey);

                using (HttpResponseMessage response = await client.DeleteAsync(url))
                using (HttpContent content = response.Content)
                {
                    string data = await content.ReadAsStringAsync();
                    if (data != null)
                    {
                        newT = JsonConvert.DeserializeObject<T>(data);
                        return newT;
                    }
                }
            }
            Object o = new Object();
            return (T)o;
        }

        #endregion

    }
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *