Exploring refit, an automatic Type-safe REST library for. NET Standard

Source: Internet
Author: User

On Scott Hanselman's blog, I saw a good thing, "exploring refit, an automatic Type-safe REST library for. NET," and he recommended one. NET Standard 1.4 for automatic type-safe rest library refit. Refit similar to Java retrofit, is a restful architecture of. NET client implementations, based on attributes that provide the data returned by the rest API into (Plain ordinary C # object, simple C # objects), POCO to JSON, Network requests (post,get,put,delete, etc.) encapsulation, the internal encapsulation uses httpclient, the former focuses on the interface encapsulation, the latter focus on the efficient network request, the two Division of Labor and Cooperation. Our application through the refit request network, actually uses the refit interface layer encapsulates the request parameter, the Header, the URL and so on the information, then HttpClient completes the subsequent request operation, after the service side returns the data, the HttpClient original result gives refit , The latter is the process of parsing the results based on the user's needs.

For example:

public interface IGitHubApi{    [Get("/users/{user}")]    Task<User> GetUser(string user);}

Define one of the rest API interfaces above. The interface defines a function GetUser that accesses the server's path through an HTTP GET request /users/{user} and encapsulates the returned result as a user POCO object.

Where the value in the URL path is the value of the parameter in the {user} getuser function user .

Then through RestService  the class to generate an IGitHubApi  interface implementation, using HttpClient call;

var gitHubApi = RestService.For<IGitHubApi>(https://api.github.com);var octocat = await gitHubApi.GetUser("octocat");

As can be seen from the above example, refit uses attributes to declare HTTP requests

    • Support for URL parameter substitution 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 documentation

attributes on function and function arguments declare the request method

1. Request method

Each function must have an HTTP attribute to indicate how the request is requested and the URL path of the request. There are 5 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")]

URL parameters can also be specified in the URL path:

[Get("/users/list?sort=desc")]
2. URL Processing

The requested URL can be dynamically updated based on the function parameters. A replaceable chunk is a { string that is used and } enclosed, and the function parameter must be @ AliasAs特性 marked, and the parameter of the attribute is the same string

[Get("/group/{id}/users")]//注意 字符串idTask<List<User>> GroupList([AliasAs("id")] int groupId); //注意 AliasAs特性的参数要和前面的字符串一样 id
Query parameters are also supported
[Get("/group/{id}/users")]Task<List<User>> GroupList([AliasAs("id")] int groupId, [AliasAs("sort")] string sortOrder);GroupList(4, "desc");>>> "/group/4/users?sort=desc"
3. Requesting body (Request body)

by [ Body]特性 You can declare an object to be sent to the server as a request body.]

[Post("/users/new")]Task CreateUser([Body] User user);
The object will be converted to a  string using the corresponding converter, or the byte stream is committed to the server.
4, form encoded and MULTIPART forms and MULTIPART

Functions can also be annotated to send form data and multipart data

5. Server results converted to C # objects

RestService the converters used convert HTTP request results (by default, JSON) to C # objects, and C # objects are specified by the function return value

6. Add the request header

We can add the request header through [Headers] to support the dynamic request header.

refitis very powerful, this article through a wealth of examples and the source of the mining, to show you refit's own powerful features and extensibility

Exploring refit, an automatic Type-safe REST library for. NET Standard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.