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.
To install this package via Nuget:
Install-Package RestApiEngine -Version 0.0.1
//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;
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;
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;
var engine=new RestEngine();
var url="http://example.com";
var engine=new RestEngine(url);
var url="http://example.com";
var acceptContentType="application/json";
var engine=new RestEngine(url, acceptContentType);
When we've intialized a blank engine, we'll need to add a Content-Type:
engine.AddAccept("application/json")
We can also add a header in a Key/value way:
engine.AddHeader("Engine", "My Awesome Engine");
Adds URI Param, e.g. "/users" to our base URL:
engine.AddUriParam("users");
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");
Adds Bearer token for most .NET JWT based backend applications.
engine.AddBearerAuthentication("myAwesomeTokenYoullNeverKnow");
Adds custom token auth for the any backend applications.
engine.AddBearerAuthentication("myScheme", "myNotSoAwesomeToken");
We pass the body string, the encoding type and the content-type.
engine.AddBodyString("My Body String", Encoding.UTF8, "Accept Content Type");
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.
var engine=new RestEngine("http://example.com");
var taskResult = await engine.ProcessGetAsync("/api/users");
var engine=new RestEngine("http://example.com");
var result = engine.ProcessGetSync("/api/users");
This makes the call to the environment or context built with headers, baseUrl, Accept, uri and query params.
var taskResult = await engine.ProcessGetAsync();
var result = engine.ProcessGetSync();
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.
var engine=new RestEngine("http://example.com");
var taskResult = await engine.ProcessPostAsync("/api/users");
var engine=new RestEngine("http://example.com");
var result = engine.ProcessPostSync("/api/users");
This makes the post call to the environment or context built with headers, baseUrl, Accept, body string, uri and query params.
var taskResult = await engine.ProcessPostAsync();
var result = engine.ProcessPostSync();
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.
var engine=new RestEngine("http://example.com");
var taskResult = await engine.ProcessPutAsync("/api/users");
var engine=new RestEngine("http://example.com");
var result = engine.ProcessPutSync("/api/users");
This makes the Put call to the environment or context built with headers, baseUrl, Accept, body string, uri and query params.
var taskResult = await engine.ProcessPutAsync();
var result = engine.ProcessPutSync();
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.
var engine=new RestEngine("http://example.com");
var taskResult = await engine.ProcessDeleteAsync("/api/users");
var engine=new RestEngine("http://example.com");
var result = engine.ProcessDeleteSync("/api/users");
This makes the Put call to the environment or context built with headers, baseUrl, Accept, body string, uri and query params.
var taskResult = await engine.ProcessDeleteAsync();
var result = engine.ProcessDeleteSync();
This is the first package/library made by carjimfa. Doubts, support and more, ask me anything on twitter: @carjimfa :)