Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

.NET REST Client Framework for all platforms

License

NotificationsYou must be signed in to change notification settings

MelbourneDeveloper/RestClient.Net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

diagram

.NET REST Client Framework for all platforms

buildandtest

The best .NET REST API 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. It's designed for functional-style programming with F# in mind.

NuGet:RestClient.Net

.NET

5.0 Release

5.0 brings a Fluent API, immutable types, lots of useful extension methods, and thorough unit testing. Check outthe tests for code examples.

Why You Should Use It

  • Treats Urls as first-class citizens withUrls. URLs are immutablerecords 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)
    • The library is decoupled from Newtsonsoft so that you can use any serialization method or version of Newtonsoft. This means that you can use it with any version of Azure Functions.
  • 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 and complete test coverage means you can make a change if you need todiagram
  • Fluent API for construction, non-destructive mutation, and Url construction
  • Uses.NET Core Logging -ILogger to allow logging of all aspects of HTTP calls

Examples

For a complete set of examples, see theseunit tests.

POST an Object and get Response

usingvarclient=//Build the Url from the host namenewClient("jsonplaceholder.typicode.com".ToHttpsUriFromHost());UserPostuserPost=awaitclient.PostAsync<UserPost,UserPost>(//POST the UserPost to the servernewUserPost{title="Title"},"posts");

Dependency Injection (RestClient.Net.DependencyInjection NuGet Package)

Wiring it up

varserviceCollection=newServiceCollection()//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

publicclassGetString1:IGetString{publicIClientClient{get;}publicGetString1(IClientclient)=>Client=client;publicasyncTask<string>GetStringAsync()=>awaitClient.GetAsync<string>();}

Getting a IClient Using a Factory

publicclassGetString2:IGetString{publicIClientClient{get;}publicGetString2(CreateClientcreateClient){//Use the options to set the BaseUrl or other properties on the ClientClient=createClient("test",(o)=>{o.BaseUrl=o.BaseUrlwith{Host="www.test.com"};});}publicasyncTask<string>GetStringAsync()=>awaitClient.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.5varresponse=await"https://restcountries.eu/rest/v2".ToAbsoluteUrl().GetAsync<List<RestCountry>>();

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

usingRestClient.Net.Abstractions.Extensions;usingSystem.Collections.Generic;usingSystem.Threading.Tasks;usingUrls;namespaceRestClient.Net{publicstaticclassGitHubGraphQLMethods{publicstaticasyncTask<T>GetIssues<T>(stringrepo,stringaccessToken)=>(await"https://api.github.com/graphql".ToAbsoluteUrl().PostAsync<QueryResponse<T>,QueryRequest>(newQueryRequest("{ 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;}publicrecordQueryRequest(stringquery);publicrecordIssue(int?number,stringtitle,stringbody);publicrecordIssues(List<Issue>nodes);publicrecordData<T>(Tsearch);publicrecordQueryResponse<T>(Data<T>data);}

Url Construction with F#

[<TestMethod>]memberthis.TestComposition()=leturi="host.com".ToHttpUrlFromHost(5000)        .AddQueryParameter("fieldname1","field<>Value1")        .WithCredentials("username","password")        .AddQueryParameter("FieldName2","field<>Value2")        .WithFragment("frag")        .WithPath("pathpart1","pathpart2")    Assert.AreEqual("http://username:password@host.com:5000/pathpart1/pathpart2?fieldname1=field%3C%3EValue1&FieldName2=field%3C%3EValue2#frag",uri.ToString());

About

.NET REST Client Framework for all platforms

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    Languages


    [8]ページ先頭

    ©2009-2025 Movatter.jp