Giter Site home page Giter Site logo

muskanmahajan37 / restclient.net Goto Github PK

View Code? Open in Web Editor NEW

This project forked from magicianred/restclient.net

0.0 0.0 0.0 8.72 MB

.NET REST Client Framework for all platforms

License: MIT License

Batchfile 0.09% C# 97.38% CSS 0.90% JavaScript 0.05% HTML 1.58%

restclient.net's Introduction

diagram

.NET REST Client Framework for all platforms

NuGet: RestClient.Net

The best .NET REST Client with task-based async, strong types and dependency injection on all platforms. Consume your ASP .NET Core Web APIs or consume RESTful APIs over the internet in C#, F# or Visual Basic. Designed for functional style programming with F# in mind.

.NET

.NET REST Client Framework for all platforms

The best .NET REST Client with task-based async, strong types, and dependency injection on all platforms. Consume your ASP .NET Core Web APIs or consume RESTful APIs over the internet in C# or Visual Basic.

This page represents documentation for the alpha release. Please include pre-release when adding the NuGet packages. See the main branch for 4.x. There will be some breaking changes until the actual release.

Why You Should Use It

  • Treats Urls as first-class citizens with Urls. URLs are immutable records and have a fluent API for construction.
  • Designed for Dependency Injection. Mock your REST calls and add RestClient.Net to your IoC container with one line of code
  • Async friendly. All operations use async, await keywords
  • Automatic request/response body serialization to/from strong types (JSON, Binary, SOAP, Google Protocol Buffers)
  • Install from NuGet on any platform from .NET Framework 4.5 up to .NET 5. Supports Xamarin (Mono, iOS, Android), UWP, WebAssembly and Unity with .NET Standard 2.0
  • Supports GET, POST, PUT, PATCH, DELETE with ability and custom methods
  • Tight code (around 350 lines) means you can make a change if you need to

Examples

For a full set of examples see these unit tests.

POST an Object and get Response

using var client =
    //Build the Url from the host name
    new Client("jsonplaceholder.typicode.com".ToHttpsUriFromHost());

UserPost userPost = await client.PostAsync<UserPost, UserPost>(
    //POST the UserPost to the server
    new UserPost { title = "Title" }, "posts"
    );

Dependency Injection (RestClient.Net.DependencyInjection NuGet Package)

Wiring it up

var serviceCollection = new ServiceCollection()
    //Add a service which has an IClient dependency
    .AddSingleton<IGetString, GetString1>()
    //Add RestClient.Net with a default Base Url of http://www.test.com
    .AddRestClient((o) => o.BaseUrl = "http://www.test.com".ToAbsoluteUrl());

//Use HttpClient dependency injection
_ = serviceCollection.AddHttpClient();

Getting a Global IClient in a Service

public class GetString1 : IGetString
{
    public IClient Client { get; }

    public GetString1(IClient client) => Client = client;

    public async Task<string> GetStringAsync() => await Client.GetAsync<string>();
}

Getting a IClient Using a Factory

public class GetString2 : IGetString
{
    public IClient Client { get; }

    public GetString2(CreateClient createClient)
    {
        //Use the options to set the BaseUrl or other properties on the Client
        Client = createClient("test", (o) => { o.BaseUrl = o.BaseUrl with { Host = "www.test.com" }; });
    }

    public async Task<string> GetStringAsync() => await Client.GetAsync<string>();
}

Make Call and Construct Client

//This constructs an AbsoluteUrl from the string, makes the GET call and deserializes the JSON to a strongly typed list
//The response also contains a Client with the base of the Url that you can reuse
//Note: not available on .NET 4.5

var response = await "https://restcountries.eu/rest/v2"
    .ToAbsoluteUrl()
    .GetAsync<List<RestCountry>>();

Query Github Issues with GraphQL (You must authorize GraphQL Github App)

using RestClient.Net.Abstractions.Extensions;
using System.Collections.Generic;
using System.Threading.Tasks;
using Urls;

namespace RestClient.Net
{
    public static class GitHubGraphQLMethods
    {
        public static async Task<T> GetIssues<T>(string repo, string accessToken)
        => (await "https://api.github.com/graphql"
            .ToAbsoluteUrl()
            .PostAsync<QueryResponse<T>, QueryRequest>(
                new QueryRequest("{ search(query: \"repo:" + repo + "\", type: ISSUE, first: 100) {nodes {... on Issue { number title body } } }}")
                , HeadersExtensions.FromBearerToken(accessToken)
            .Append("User-Agent", "RestClient.Net"))).Response.Body.data.search;
    }

    public record QueryRequest(string query);
    public record Issue(int? number, string title, string body);
    public record Issues(List<Issue> nodes);
    public record Data<T>(T search);
    public record QueryResponse<T>(Data<T> data);

}

Donate

Coin Address
Bitcoin 33LrG1p81kdzNUHoCnsYGj6EHRprTKWu3U
Ethereum 0x7ba0ea9975ac0efb5319886a287dcf5eecd3038e

restclient.net's People

Contributors

acecoderlaura avatar mackiovello avatar melbournedeveloper avatar silic0ns0ldier avatar www 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.