Custard is a .NET standard plugin to call web APIs intuitively. ๐
Fully compatible with:
- .NET MAUI
- Xamarin Forms
- Package manager
Install-Package Custard -Version 0.3.7
- .NET CLI
dotnet add package Custard --version 0.3.7
Service yourService = new Service(string host, int port = 80, bool sslCertificate = false);
yourService.RequestHeaders.Add("Hearder", "Value "); // Do this for every headers
โ For every method below, we recommend being very explicit with your parameters. As there are too many options for parameters, this could lead to ambiguities.
e.g: use
Get(controller: theController)
instead ofGet(theController)
-
Parameters:
Name Type Required controller string
โ action string
โ headers IDictionary<string, string>
โ jsonBody string
โ parameters string[] / IDictonary<string,string>
โ Usage:
- To return a string:
yourService.Post (controller: controller, action: action, singleUseHeaders: headers, jsonBody: jsonBody, parameters: parameters);
- To return a model (T is the model):
yourService.Post<T> (controller: controller, action: action, singleUseHeaders: headers, jsonBody: jsonBody, parameters: parameters);
- To return a string:
-
Parameters:
Name Type Required controller string
โ action string
โ headers IDictionary<string, string>
โ jsonBody string
โ parameters string[] / IDictonary<string,string>
โ Usage:
- To return a string:
yourService.Put (controller: controller, action: action, singleUseHeaders: headers, jsonBody: jsonBody, parameters: parameters);
- To return a model (T is the model):
yourService.Put<T> (controller: controller, action: action, singleUseHeaders: headers, jsonBody: jsonBody, parameters: parameters);
- To return a string:
-
Parameters:
Name Type Required controller string
โ action string
โ headers IDictionary<string, string>
โ jsonBody string
โ parameters string[] / IDictonary<string,string>
โ Usage:
- To return a string:
yourService.Get (controller: controller, action: action, singleUseHeaders: headers, jsonBody: jsonBody, parameters: parameters);
- To return a model (T is the model):
yourService.Get<T> (controller: controller, action: action, singleUseHeaders: headers, jsonBody: jsonBody, parameters: parameters);
Custard now supports two types of parameters:
- Path parameters
- Query parameters
To pass path parameters to your requests, you have to pass them as string[]
:
E.g: for /users/api/2/3/4
we would use:
string action = "users";
string controller = "api";
string[] param = { "2", "3", "4" };
var resultStr = await yourService.Get(controller: controller,
action: action,
parameters: param);
To pass query parameters to your requests, you have to pass them as Dictionary<string, string>
:
E.g: for /users/api?two=2&three=3&four=4
we would use:
string action = "users";
string controller = "api";
Dictionary<string, string> param = new Dictionary<string, string>
{
{ "two", "2" },
{ "three", "3" },
{ "four", "4" }
};
var resultStr = await yourService.Get(controller: controller,
action: action,
parameters: param);
โ If you want to return a model, the HTTP response body has to be in JSON format
From v0.3.5, you can now add a cancellation token to you request.
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
var cancelationToken = cancellationTokenSource.Token;
//
// Act
Task task = _wService.Get(controller: controller,
cancellationToken: cancelationToken);
-
If needed, you can add a callback if the request faces an HTTP error. This will work with any method mentioned above. This will allow you to do an handle the error more easily.
Here's how it works:
var actualResult = await yourService.Get(controller: "todolists", singleUseHeader: headers, callbackError: (err) => { });
- code: the error status code (HttpStatusCode).