Giter Site home page Giter Site logo

finalrest's Introduction

FinalRest

FinalRest removes the need to write all the RestAPI Client code over and over again. Just use a declarative way to create your requests and client. A simple Builder style allows for inheritance and individual behaviour for each individual request, without the need of much code.


FinalRest is available on NuGet

Nuget

HowTo

The request

To start a FinalRestRequestBuilder needs to be created:

var request = new FinalRestRequestBuilder()


The basic building blocks are fairly simple:

.SetRoute("api/")
.SetMethod(ERestMethod.GET)
.AddHeader("some-header", "the-header-value")
.Copy()


To handle Pre- and Post request handlers (for authentication as example) there are the needed blocks too Find this handler in the sample here: CustomJwtAuthHandler.cs

.AddPreRequestHandler<CustomJwtAuthHandler>() 
.AddPostRequestHandler<CustomJwtAuthHandler>()


To invoke actions on certain responses you can use the behaviours. All behaviours exist as normal callbacks and as async callbacks. ResponseBehaviours let you access the Response. Result Behaviour just give you the HttpStatusCode.

.AddAsyncResponseBehaviour(          HttpStatusCode.OK,                   (status)       => Task.Factory.StartNew(() => Debug.WriteLine(status.ToString())))
.AddResponseBehaviour(               HttpStatusCode.OK,                   (status)       => Debug.WriteLine($"Sync: {status}"))
.AddAsyncResultBehaviour<ApiResult>( HttpStatusCode.Unauthorized,         (status, data) => Nav.HandleAuthFailAsync($"damn {status} => {data.Info.Version}"))
.AddResultBehaviour<ApiResult>(      HttpStatusCode.InternalServerError,  (status, data) => Dialog.ShowErrorDialog(data.Info.Seed))

\ Now you can copy and build more out of this defined builder, or build it to supply this to a client

.Build();

The client

The client itself also is in the form of a Builder. The Generic Argument will be the Type of wich the InvocationKeys is. (Usually a enum is the intended way, but you can just use any type):

var client = new FinalRestClientBuilder<ERequests>()


For now choosing the HttpClient is optional, but later more implementations will be possible. (HttpClient will be always the Default)

.UseHttpClient()


The mandatory base url to the api needs to be supplied:

.SetBaseUrl("https://randomuser.me/")


In case you need to verify the SSL certificate yourself you can add this handler if you need:

.UseCustomCertificateValidation(ValidateCert)


Now its time to add all your requests to this client:

.AddRequest(randomUsers, ERequests.RandomUsers)


Build and enjoy your RinalRestClient:

.Build();

Use the client

To invoke a request we just need 1 method with a few parameters:

  • The generic type is your expected result type on success
  • The Enum is your key that you gave the request when you added it to your client
  • The Body is null on this request.
  • The following parameters are the url parameters
var users = await client.InvokeAsync<ApiResult>(ERequests.RandomUsers, null, "inc=id,gender,name,email", "results=10");

Contributions

This Project is open for Contributions. Please file a Issue before you start working on something If you have any questions or looking for a good first issue, feel free to raise a issue aswell

finalrest's People

Contributors

bernhardpollerspoeck avatar bpoller2810 avatar

Stargazers

Abdella Solomon avatar  avatar

Watchers

 avatar

finalrest's Issues

Readme needs rework

The current Readme is just a stub and needs propper rework to fully present the project

Documentation is missing

All public types, methods and properties defined in FinalRest.core should be propperly documented

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.