In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you what the automatic type-safe REST .NET standard library refit is like, which is concise and easy to understand. I hope you can learn something through the detailed introduction of this article.
Seeing a good thing on the SCOTT HANSELMAN blog, "Exploring refit, an automatic type-safe REST library for .NET Standard", he recommended refit, an automatic type-safe REST library based on .NET standard 1.4. Refit is similar to Java's Retrofit, is a set of RESTful architecture .NET client implementation, based on features, provides the data returned by REST API into (Plain Ordinary C # Object, simple C # object), POCO to JSON, network request (POST,GET,PUT,DELETE, etc.) encapsulation, internal encapsulation uses HttpClient, the former focuses on interface encapsulation, the latter focuses on the efficiency of network requests, the two work together. Our application requests the network through refit, in fact, it uses refit interface layer to encapsulate request parameters, Header, Url and other information, and then HttpClient completes the subsequent request operations. After the server returns data, HttpClient gives the original results to refit, which parses the results according to the needs of users.
For example:
Public interface IGitHubApi {[Get ("/ users/ {user}")] Task GetUser (string user);}
Define a REST API interface above. This interface defines a function GetUser that accesses the server's / users/ {user} path through a HTTP GET request and encapsulates the returned result as a User POCO object.
The value of {user} in the URL path is the value of the parameter user in the GetUser function.
Then generate an implementation of the IGitHubApi interface through the RestService class, using the HttpClient call
Var gitHubApi = RestService.For (https://api.github.com);var octocat = await gitHubApi.GetUser ("octocat")
As you can see from the example above, refit uses features to declare HTTP requests
Support for URL parameter replacement and query parameters
The returned result is converted to a C# object (the returned result can be JSON)
Support for Multipart requests and file uploads
Specific use of documents
Features on functions and function parameters declare the way the request is made
1. Request method
Each function must have the HTTP feature to indicate the method of the request and the URL path of the request. There are five HTTP annotations in the class library: GET, POST, PUT,DELETE and HEAD. The parameter in the annotation is the relative URL path of the request.
[Get ("/ users/list")]
You can also specify the URL parameter in the URL path:
[Get ("/ users/list?sort=desc")] 2. URL processing
The requested URL can be dynamically updated according to the function parameters. A replaceable block is a string surrounded by {and}, and the function argument must be marked with the @ AliasAs attribute, and the parameter of the property is the same string.
[Get ("/ group/ {id} / users")] / / Note the string idTask GroupList ([AliasAs ("id")] int groupId); / / Note that the parameters of the AliasAs feature should be the same as the previous string. Id also supports query parameters [Get ("/ group/ {id} / users")] Task GroupList ([AliasAs ("id")] int groupId, [AliasAs ("sort")] string sortOrder); GroupList (4, "desc") "/ group/4/users?sort=desc" 3, request body (Request Body)
The [Body] feature allows you to declare that an object is sent to the server as the body of the request.
[Post ("/ users/new")] Task CreateUser ([Body] User user); the object is converted to a string or byte stream submitted to the server by RestService using the corresponding converter. 4. FORM ENCODED AND MULTIPART forms and Multipart
Functions can also be annotated to send form data and multipart data
5. The server result is converted to a C# object
Use RestService's converter to convert the HTTP request result (default is JSON) into a C # object, which is specified by the function return value
6. Add request headers
We can add request headers through [Headers] to support dynamic request headers.
Refit is very powerful. Through rich examples and mining the source code, the editor shows the powerful function and expansibility of refit itself.
The above is what the automatic type-safe REST .NET standard library refit looks like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.