GithubHelp home page GithubHelp logo

jbogatchenko / dalsoft.restclient Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dalsoft/dalsoft.restclient

0.0 1.0 0.0 679 KB

The dynamic C# REST Client - the only REST Client you will ever need to create awesome code

Home Page: https://restclient.dalsoft.io

License: MIT License

C# 100.00%

dalsoft.restclient's Introduction

RestClient

Help and chat on Gitter

For everything you need to know, please head over to https://restclient.dalsoft.io

RestClient is a very lightweight wrapper around System.Net.HttpClient that uses the dynamic features of .NET 4 to provide a fluent way of accessing RESTFul API's, making it trivial to create REST requests using a lot less code .

Originally created to remove the boilerplate code involved in making REST requests using code that is testable. I know there are a couple of REST clients out there but I wanted the syntax to look a particular way with minimal fuss.

RestClient is biased towards posting and returning JSON - if you don't provide Accept and Content-Type headers then they are set to application/json by default See Working with non JSON content.

Supported Platforms

RestClient targets .NET Standard 2.0 therefore supports Windows, Linux, Mac and Xamarin (iOS, Android and UWP).

If you need to target .NET Standard 1.4 use version 3.2.2.

Getting Started

Install via .NET CLI

> dotnet add package DalSoft.RestClient

Install via NuGet

PM> Install-Package DalSoft.RestClient

Example call a REST API in two lines of code

You start by new'ing up the RestClient and passing in the base uri for your RESTful API.

Then simply chain members that would make up the resource you want to access - ending with the HTTP method you want to use.

For example if your wanted to perform a GET on https://jsonplaceholder.typicode.com/users/1 you would do the following:

dynamic client = new RestClient("https://jsonplaceholder.typicode.com");

var user = await client.Users(1).Get();
   
Console.WriteLine(user.name);

Note all HTTP methods are async

New in Version 3.3.0 IHttpClientFactory Goodness!

In version 3.3.0 we added support for .NET Core's 2.1 IHttpClientFactory.

First register your RestClient as a service in Startup.cs.

public class Startup
{
   public void ConfigureServices(IServiceCollection services)
   {
      services.AddRestClient("https://api.github.com", new Headers(new { UserAgent = "MyClient" }));
   }
}

Then inject IRestClientFactory into your controller, which is how we support IHttpClientFactory.

public class GitHubController : Controller
{
   private readonly IRestClientFactory _restClientFactory;
        
   public GitHubController(IRestClientFactory restClientFactory)
   {
      _restClientFactory = restClientFactory;
   }

   [Route("github/users/dalsoft"), HttpGet]
   public async Task<List<Repository>> CreateClient()
   {
      dynamic restClient = _restClientFactory.CreateClient();
            
      var repositories = await restClient.users.dalsoft.repos.Get();
            
      return repositories;
   }
}

See IHttpClientFactory for more examples and details on our IHttpClientFactory support.

New in Version 3.0 Pipeline Awesomeness!

var config = new Config() //Build a pipeline using extension methods
                    .UseHttpClientHandler(new HttpClientHandler())
                    .UseHandler((request, token, next) => next(request, token))
                    .UseHandler(new UnitTestHandler())
                    .UseUnitTestHandler(request => { })
                    .UseUnitTestHandler(request => new HttpResponseMessage());

dynamic restClient = new RestClient("http://headers.jsontest.com/", config);

See Pipeline Docs

Standing on the Shoulders of Giants

DalSoft.RestClient is built using the following great open source projects:

DalSoft.RestClient is inspired by and gives credit to:

dalsoft.restclient's People

Contributors

dalsoft avatar joakimjm avatar

Watchers

janettab avatar

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.