RestApiEngine 0.1.0

dotnet add package RestApiEngine --version 0.1.0
                    
NuGet\Install-Package RestApiEngine -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RestApiEngine" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RestApiEngine" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="RestApiEngine" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RestApiEngine --version 0.1.0
                    
#r "nuget: RestApiEngine, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RestApiEngine@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RestApiEngine&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=RestApiEngine&version=0.1.0
                    
Install as a Cake Tool

RestApiEngine

Wrapper of HttpClient to make calls easier. I was looking for an easy wrapper and superior implementation of HttpClient and came up with this. I'm using it daily in other projects so I will be updating it day by day.

You set up the engine pointing to a base URL and you can make calls to different endpoints, different accept and adding different URI and Query parameters.

Install

To install this package via Nuget:

Install-Package RestApiEngine -Version 0.0.1

Easy Engine Set Up and First Get Call

//Creates engine with base url and accept content type
var engine = new RestEngine("reqres.in/api/", "application/json")
    .AddHeader("User-Agent", "RestEngine .NET");
var processGetResult = engine.ProcessGetSync("users");
var result = processGetResult.Content.ReadAsStringAsync().Result;

First Get Call with Query parameters

engine.ClearUriParams() //Clears URI Params
    .ClearHeaders() // Clears Headers
    .AddAccept("application/json") //Sets Accept
    .AddHeader("User-Agent", ".NET Foundation Repository Reporter")
    .AddUriParam("users") // adss /users to URI
    .AddQueryParam("page", "3"); // adds ?page=3 to URL

// executes get call to reqres.in/api/users?page=3
var processGetResult2 = engine.ProcessGetSync();
var result2 = processGetResult2.Content.ReadAsStringAsync().Result;

Post Call

engine.ClearQueryParams();
var body="{\"name\": \"morpheus\",\"job\": \"leader\"}";
engine.AddBodyString(body, Encoding.UTF8, "application/json");
var processPostResult=engine.ProcessGetSync();
var postResult=processPostResult.Content.ReadAsStringAsync().Result;

Important Methods

Engine Initialization

Blank Engine
var engine=new RestEngine();
Passing Only Base URL
var url="http://example.com";
var engine=new RestEngine(url);
Passing baseUrl and Accept Content-Type
var url="http://example.com";
var acceptContentType="application/json";
var engine=new RestEngine(url, acceptContentType);

Headers, Uri Params and Query Params

Add Accept Content-Type

When we've intialized a blank engine, we'll need to add a Content-Type:

engine.AddAccept("application/json")
Add Header

We can also add a header in a Key/value way:

engine.AddHeader("Engine", "My Awesome Engine");
Add URI Parameter

Adds URI Param, e.g. "/users" to our base URL:

engine.AddUriParam("users");
Add Query Parameter

Adds Query Param, e.g. "?page=5" to our base URL in a key/value way:

engine.AddQueryParam("page", "5");

We can add as much query params as we want, th engine processes the ? and & automatically.

// this is http://example.com/users?page=5&resultsPerPage=15
engine.AddQueryParam("page", "5").AddQueryParam("resultsPerPage", "15");
Add Authentication
Adds Bearer Token Authentication

Adds Bearer token for most .NET JWT based backend applications.

engine.AddBearerAuthentication("myAwesomeTokenYoullNeverKnow");
Adds Custom Token Authentication

Adds custom token auth for the any backend applications.

engine.AddBearerAuthentication("myScheme", "myNotSoAwesomeToken");
Adds Body String to make POST and PUT calls (or any call you want)

We pass the body string, the encoding type and the content-type.

engine.AddBodyString("My Body String", Encoding.UTF8, "Accept Content Type");

GET Calls

GET by Path

We can make calls directly to a path if we don't want to set up all the engine, we can set up an engine with a base URL and then make calls to a /path.

Async GET By Path
var engine=new RestEngine("http://example.com");
var taskResult = await engine.ProcessGetAsync("/api/users");
Sync GET By Path
var engine=new RestEngine("http://example.com");
var result = engine.ProcessGetSync("/api/users");
Process Get With everything setted up

This makes the call to the environment or context built with headers, baseUrl, Accept, uri and query params.

Async GET by context
var taskResult = await engine.ProcessGetAsync();
Sync GET by context
var result = engine.ProcessGetSync();

POST Calls

POST by Path

We can make calls directly to a path if we don't want to set up all the engine, we can set up an engine with a base URL and then make calls to a /path.

Async POST By Path
var engine=new RestEngine("http://example.com");
var taskResult = await engine.ProcessPostAsync("/api/users");
Sync POST By Path
var engine=new RestEngine("http://example.com");
var result = engine.ProcessPostSync("/api/users");
Process POST With everything setted up

This makes the post call to the environment or context built with headers, baseUrl, Accept, body string, uri and query params.

Async POST by context
var taskResult = await engine.ProcessPostAsync();
Sync POST by context
var result = engine.ProcessPostSync();

PUT Calls (Min Version 0.1)

PUT by Path

We can make calls directly to a path if we don't want to set up all the engine, we can set up an engine with a base URL and then make calls to a /path.

Async PUT By Path
var engine=new RestEngine("http://example.com");
var taskResult = await engine.ProcessPutAsync("/api/users");
Sync PUT By Path
var engine=new RestEngine("http://example.com");
var result = engine.ProcessPutSync("/api/users");
Process PUT With everything setted up

This makes the Put call to the environment or context built with headers, baseUrl, Accept, body string, uri and query params.

Async PUT by context
var taskResult = await engine.ProcessPutAsync();
Sync PUT by context
var result = engine.ProcessPutSync();

DELETE Calls (Min Version 0.1)

DELETE by Path

We can make calls directly to a path if we don't want to set up all the engine, we can set up an engine with a base URL and then make calls to a /path.

Async DELETE By Path
var engine=new RestEngine("http://example.com");
var taskResult = await engine.ProcessDeleteAsync("/api/users");
Sync DELETE By Path
var engine=new RestEngine("http://example.com");
var result = engine.ProcessDeleteSync("/api/users");
Process PUT With everything setted up

This makes the Put call to the environment or context built with headers, baseUrl, Accept, body string, uri and query params.

Async DELETE by context
var taskResult = await engine.ProcessDeleteAsync();
Sync DELETE by context
var result = engine.ProcessDeleteSync();

Contribute

This is the first package/library made by carjimfa. Doubts, support and more, ask me anything on twitter: @carjimfa 😃

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 2,771 6/18/2018
0.0.1 1,357 6/7/2018

RestApiEngine 0.1
First update of RestApiEngine, from 0.0.1 to 0.1.
We have increased the number of processing verbs the engine supports, plus, we have adopted a different versioning number.
-PUT method support via ProcessPutAsync and ProcessPutSync for async and sync processing.
-DELETE method support via ProcessDeleteAsync and ProcessDeleteSync for async and sync processing.
-PUT and DELETE available using explicit path (considering deprecating this functionality).
PUT and DELETE methods are supported in a similar way to GET/POST but just changing the method called in HttpClient.
What more can I say. I'm officially using this library in a couple of projects in production and is working properly so I'm very proud of this library. Small, but useful.
This product is MIT Licensed so feel free to do anything you want with it.
Any bug or information, you can contact me via twitter: @carjimfa.