HttpsRequestHandler
Represents a wrapper around the class HttpClient. It provides an abstraction so you can interact with an Http Request with a single method.
Definition
Namespace: Tipi.Tools.Http
Assembly: Tipi.Tools.Http.dll
Package: Tipi.Tools.HttpRequestHandler
This class inherits from the IDisposable Interface in order to interact with the HttpClient.
Copied!
HttpsRequestHandler = new HttpsRequestHandler();
Example
Here is an example of the complete usage of the class.
Copied!
// Create a new instance of the HttpRequestHandler Class
using var requestHandler = new HttpRequestHandler();
// Execute an Http Verb
var response = await requestHandler.ExecuteAsync("GET", "https://codex.codingtipi.com/api/v1/langs");
Response interaction
The response object when calling for the ExecuteAsync method contains both a string body and an status code defined using the HttpStatusCode enum. Here is an example on how to interact with your response object:
Copied!
// Create a new instance of the HttpRequestHandler Class
using var requestHandler = new HttpRequestHandler();
// Execute an Http Verb
var response = await requestHandler.ExecuteAsync("GET", "https://codex.codingtipi.com/api/v1/langs");
// Validate the response to check for your desired status code.
if( response.StatusCode == HttpStatusCode.Created )
{
// We used NewtonSoft To Deserialize the response body.
var deselializedBody = awaitJsonConvert.DeserializeObject<MyObject>(response.Body);
}
Remarks
The HttpRequestHandler class is a wrapper from the HttpClient class provided by the .Net Framework, this wrapper is used in order to minimize the code needed in order to interact with HTTP calls, by simplifying all of the possible HTTP Verb methods into a single method called ExecuteAsync, with this method you can choose your HTTP Verb by simply providing a string with the verb name. This wrapper inherits the IDisposable interface to provide a Dispose method.
The class has a constructor that allows you to send over a Bearer Token or a Dictionary<string,string> that represents the headers you want to initialize the client with.
The response provided by the ExecuteAsync method returns an object that implements the HttpResponse class and contains the Status Code for the response as well as the body of the response.
Constructors
Initialize an instance of a HttpClient with default headers.
Initialize an instance of a HttpClient with a bearer token as Authentication.
Initialize an instance of a HttpClient with a the headers passed inside the Dictionary, this works by using the Dictionary Key as the Header Key and the value as its value.
Initialize an instance of a HttpClient with a bearer token as Authentication and custom Headers provided using the Dictionary.
Methods
Executes the HTTP Verb provided to the endpoint provided.
Executes the HTTP Verb provided to the endpoint provided passing a JSON string as Body Object.
Dispose the HttpClient.
Related Classes
Class used by the HttpRequestHandler as a response for it's ExecuteAsync method.