API

A class to assist in APIs.

Constructors

public class Api(string apiUrl)

Methods

public async Task ApiCallAsync<Send>(string endpoint, Send data)

Post the API call.

Send

The object type of the data being sent.

string endpoint

The endpoint to call. This is appended to the base URL.

Send data

The data to send.

public async Task<ApiResponse<Response>> ApiCallAsync<Send, Response>(string endpoint, object data)

Post the API call and return an object of type Response.

ApiResponse

An ApiResponse object containing success and failure metadata.

Response

The object type of the data being returned.

Send

The object type of the data being sent.

string endpoint

The endpoint to call. This is appended to the base URL.

object data

The data to send.

ApiResponse<T>

A class to assist in API responses.

public bool Success

Is true when the call is successful.

public string Error

Error information.

public string InnerError

Inner exception information if it exists.

public string ReasonPhase

The reason phrase returned by the httpClient call.

public HttpStatusCode StatusCode

The status code returned by the httpClient call.

public T Response

The typed object returned from the call.

Example

C#
var myData = new SendType("My text to send");
var api = new Api("https://api.example.com");
var response = api.ApiCallAsync<SendType, ResponseType>("getsomething", myData);

if (response.Success)
... do something