bricefriha / CustardApi

.NET standard plugin to intuitively call web APIs

Home Page:https://bricefriha.github.io/CustardApi/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What's Custard?

NuGet

Custard is a .NET standard plugin to call web APIs intuitively. 😁

Fully compatible with:

  • .NET MAUI
  • Xamarin Forms

Documentation πŸ“„

Installation

  • Package manager
    Install-Package Custard -Version 0.3.3
  • .NET CLI
    dotnet add package Custard --version 0.3.3

Custard.Service

  • Instantiate a service object:

Service yourService = new Service(string host, int port = 80, bool sslCertificate = false); 
  • Create headers

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 of Get(theController)

  • Call a POST method

    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);
  • Call a PUT method

    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);
  • Call a GET method

    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);

Passing Parameters to your requests

Custard now supports two types of parameters:

  • Path parameters
  • Query parameters

Path 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);

Query parameters

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

  • Callback Error

    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).

About

.NET standard plugin to intuitively call web APIs

https://bricefriha.github.io/CustardApi/

License:MIT License


Languages

Language:C# 100.0%