MinimalApiClient 1.0.0
See the version list below for details.
dotnet add package MinimalApiClient --version 1.0.0
NuGet\Install-Package MinimalApiClient -Version 1.0.0
<PackageReference Include="MinimalApiClient" Version="1.0.0" />
<PackageVersion Include="MinimalApiClient" Version="1.0.0" />
<PackageReference Include="MinimalApiClient" />
paket add MinimalApiClient --version 1.0.0
#r "nuget: MinimalApiClient, 1.0.0"
#:package MinimalApiClient@1.0.0
#addin nuget:?package=MinimalApiClient&version=1.0.0
#tool nuget:?package=MinimalApiClient&version=1.0.0
MinimalApiClient
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.
Note
This Project is should only be used in personal project. I Do not recommand in any circumstances to use this is a production enviroment at all.
Features
- Supports all common HTTP methods: GET, POST, PUT, PATCH, DELETE
- Flexible request types: Json, 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
Installation
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;
Usage
1. Create an ApiClient instance
Initialize the ApiClient
with the base URL of your API:
ApiClient client = new ApiClient("https://jsonplaceholder.typicode.com/");
2. Example - Define a Bearer Token Request
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));
3. Basic CRUD Operations
Once authenticated, you can proceed with common CRUD operations:
GET Request
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)}");
POST Request
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}");
PUT Request
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}");
PATCH Request
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 Request
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);
Class Overview
ApiRequest
Handles HTTP requests and includes methods to set headers, body content, and URI parameters. Supports different request types and HTTP methods.
ApiResponse & JsonApiResponse
Represents responses, with JsonApiResponse
specifically providing JSON deserialization to the specified data type.
ApiAuthentication
Manages API authentication and currently supports Basic and Bearer token authentication types.
Events
RequestSendEventArgs
is used to handle request-sending events.ResponseRecievedEventArgs
is used to handle response-receiving events.
Error Handling
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.