A simple, flexible API client built for small projects using C#
. This client allows you to make HTTP requests with various HTTP methods (GET, POST, PUT, PATCH, DELETE) and supports Basic and Bearer token authentication. It can handle JSON requests and responses, making it easy to integrate with RESTful APIs.
Furthermore it can handle Binary and Image Responses.
This Project should only be used in personal project. I Do not recommand in any circumstances to use this in a production enviroment at all.
- Supports all common HTTP methods: GET, POST, PUT, PATCH, DELETE
- Flexible request types: Json, Binary, FormData, and more
- Simple authentication setup: Supports Basic and Bearer tokens
- Event handling for sending requests and receiving responses
- JSON deserialization of responses for easy data handling
- ** Image Retrieving of responses for easy image handling.
To use this API client in your project, add it as a reference and include the necessary namespaces:
using MinimalApiClient.Http.Api;
using Newtonsoft.Json.Linq;
#optional
using System.Drawing;
Initialize the ApiClient
with the base URL of your API:
ApiClient client = new ApiClient("https://jsonplaceholder.typicode.com/");
var bearerTokenRequest = new ApiRequest("/oauth/token", ApiRequest.HttpMethod.Post, ApiRequest.ApiRequestType.Json);
Set up the body with required authentication fields:
JObject body = new JObject
{
["client_id"] = "administration",
["grant_type"] = "password",
["scopes"] = "write",
["username"] = "user",
["password"] = "test123456"
};
Assign the body to the request:
bearerTokenRequest.SetBody(body.ToString());
Send the request to obtain the bearer token:
var bearerTokenJson = await client.Execute<JsonApiResponse>(bearerTokenRequest).Result.GetJsonFromContentAsync<JObject>();
Extract and set the access token:
var accessToken = bearerTokenJson["access_token"].Value<string>();
client.EnableAuthentification(new ApiAuthentication(ApiAuthentication.AuthenticationTypes.Bearer, accessToken));
Once authenticated, you can proceed with common CRUD operations:
Retrieve a specific resource using GET:
var reqGet = new ApiRequest("todos/{0}", ApiRequest.HttpMethod.Get, ApiRequest.ApiRequestType.Json);
reqGet.SetUriParameters(new object[] { 35 });
var resultGet = await client.Execute<JsonApiResponse>(reqGet).Result.GetJsonFromContentAsync<JObject>();
Console.WriteLine($"GET Result:\n{resultGet.ToString(Newtonsoft.Json.Formatting.Indented)}");
Create a new resource using POST:
var reqPost = new ApiRequest("todos/", ApiRequest.HttpMethod.Post, ApiRequest.ApiRequestType.Json);
var reqPostBody = new JObject() { ["title"] = "minimalApi", ["body"] = "MinimalApiTest", ["userId"] = resultGet["userId"] };
reqPost.SetBody(reqPostBody);
var resultPost = await client.Execute<JsonApiResponse>(reqPost).Result.GetJsonFromContentAsync<JObject>();
Console.WriteLine($"POST Result:\n{resultPost}");
Update a resource with PUT:
var reqPut = new ApiRequest("todos/{0}", ApiRequest.HttpMethod.Put, ApiRequest.ApiRequestType.Json);
var reqPutBody = new JObject() { ["title"] = "newerTitle", ["body"] = "Updated the Text of the post.", ["userId"] = resultPost["userId"], ["id"] = resultPost["id"] };
reqPut.SetUriParameters(new object[] { resultPost["id"] });
reqPut.SetBody(reqPutBody);
var resultPut = await client.Execute<JsonApiResponse>(reqPut).Result.GetJsonFromContentAsync<JArray>();
Console.WriteLine($"PUT Result:\n{resultPut}");
Partially update a resource with PATCH:
var reqPatch = new ApiRequest("todos/{0}", ApiRequest.HttpMethod.Patch, ApiRequest.ApiRequestType.Json);
var reqPatchBody = new JObject() { ["title"] = "patchedTitle" };
reqPatch.SetUriParameters(new object[] { resultPut["id"] });
reqPatch.SetBody(reqPatchBody);
var resultPatch = await client.Execute<JsonApiResponse>(reqPatch).Result.GetJsonFromContentAsync<JObject>();
Console.WriteLine($"PATCH Result:\n{resultPatch}");
Delete a specific resource using DELETE:
var reqDelete = new ApiRequest("todos/{0}", ApiRequest.HttpMethod.Delete, ApiRequest.ApiRequestType.Json);
reqDelete.SetUriParameters(new object[] { resultPatch["id"] });
var resultDelete = await client.Execute<JsonApiResponse>(reqDelete).Result.GetJsonFromContentAsync<JObject>();
Console.WriteLine(resultDelete);
You can retrieve an Image from an Api as following:
var imageReq = new ApiRequest("/id/{0}/2758/3622", ApiRequest.HttpMethod.Get, ApiRequest.ApiRequestType.Binary);
imageReq.SetUriParameters(new object[] { 35 });
var imageResult = await client.Execute<ImageResponse>(imageReq).Result.GetImage();
imageResult.Save("result.png");
Handles HTTP requests and includes methods to set headers, body content, and URI parameters. Supports different request types and HTTP methods.
Every Response inherits from ApiResponse. Currently there are three responses defined:
ApiResponse
providing basic methods likeGetContentAsStringAsync()
.JsonApiResponse
specifically providing JSON deserialization to the specified data type.ImageResponse
specifically providing Image deserialization.
Manages API authentication and currently supports Basic and Bearer token authentication types.
RequestSendEventArgs
is used to handle request-sending events.ResponseRecievedEventArgs
is used to handle response-receiving events.
The client includes basic error handling, such as:
- Unsupported Methods: Throws an
ArgumentException
if body content is set for methods that don’t support it (e.g., GET). - Deserialization Errors: Wraps deserialization errors in an exception for easier debugging.