Four types of API

The API is also a middleware that provides data sharing for a variety of different platforms. APIs can be divided into four types based on data sharing performance between different software applications on a single or distributed platform:

Remote Procedure Call (RPC): Communicates between programs through processes (or tasks) that act on the shared data buffer.

Standard Query Language (SQL): A standard query language for accessing data. Data sharing between applications is achieved through a common database.

File Transfer: File Transfer enables data sharing between applications by sending formatted files.

Information Delivery: Refers to small formatted information between loosely coupled or tightly coupled applications, enabling data sharing through direct communication between programs.

The standards currently applied to APIs include the ANSI standard SQL API. There are also some other standards that are still being developed. The API can be applied to all computer platforms and operating systems. These APIs connect data in different formats (such as shared data buffers, database structures, file frames). Each data format requires proper data communication with different data commands and parameters, but it also produces different types of errors. Therefore, in addition to the knowledge required to perform data sharing tasks, these types of APIs must address many network parameter issues and possible error conditions, that is, each application must be aware of its strong performance to support inter-program communication. Conversely, since this API only processes one information format, the information delivery API in this case only provides a small subset of commands, network parameters, and error conditions. Because of this, the delivery of APIs greatly reduces system complexity, so when applications need to share data across multiple platforms, the use of information delivery API types is an ideal choice.

The API is distinct from a graphical user interface (GUI) or command interface: the API interface is an operating system or program interface, and the latter two are direct user interfaces.

Sometimes companies use the API as their public open system. That is to say, the company develops its own system interface standard. When it is necessary to perform operations such as system integration, customization, and program application, all members of the company can call the source code through the interface standard. This interface standard is called an open API. .

API design background

The Internet is currently flooded with articles on how to design RESTful APIs (for convenience, "RESTful API" abbreviated as "API" below), but there is no "universal" design standard: how to authenticate? What is the API format? Should your API be added to the version information? When you start writing an app, especially when the backend model part is already written, you have to design and implement the public API part of your app. Because once released, the API released will be difficult to change.

When designing the API for SupportedFu, I tried to solve the above mentioned problem from a practical point of view. I hope that I can design an API that is easy to use, easy to deploy, and flexible enough.

Basic requirements for API design

Many of the online views on API design are very "academic", they may have a more theoretical basis, but sometimes they are derailed from the real world (so I am a liberal). So the goal of this article is to give the API design best practices for current web applications from a practical point of view (of course, I think it's the best ~), and I don't follow the standards if I don't think it's appropriate. Of course, as a basis for design, several essential principles must be followed:

Follow the standards when the standards are reasonable.

The API should be programmer friendly and easy to type in the browser address bar.

The API should be simple, intuitive, and easy to use while being elegant.

The API should be flexible enough to support the upper ui.

The API design weighs the above principles.

It should be emphasized that the API is the programmer's UI. Like other UIs, you must carefully consider its user experience!

Use RESTful URLs and acTIon.

Although I said earlier, there is no one-size-fits-all API design standard. But there is indeed a universal recognition and observance: RESTfu design principles. It was proposed by Roy Felding (in Chapter 5 of his "Web-based Software Architecture" paper). The core principle of REST is to split your API into logical resources. These resources are manipulated via http (GET, POST, PUT, DELETE).

So how do I split up these resources?

Obviously from the perspective of API users, "resources" should be a noun. Even if your internal data model and resources already have a good correspondence, you still don't need to expose them one-to-one when designing the API. The key here is to hide internal resources and expose the necessary external resources.

In SupportFu, the resources are TIcket, user, and group.

Once you have defined the resources to expose, you can define the operations allowed on the resource and the correspondence between these operations and your API:

GET /TIckets # Get TIcket List

GET /tickets/12 # View a specific ticket

POST /tickets # Create a new ticket

PUT /tickets/12 # update ticket 12.

DELETE /tickets/12 #去除ticek 12

It can be seen that the benefit of using REST is that you can take advantage of the powerful implementation of http for CURD functionality of resources. And here you only need one endpoint: /tickets, there are no other naming rules and url rules, cool!

The singular plural of this endpoint

One rule that can be followed is that although it seems that using a complex number to describe a resource instance looks awkward, unifying all endpoints, using plurals makes your URL more regular. This makes API users easier to understand and easier for developers to implement.

How to deal with associations? There are also related descriptions on how to deal with the management REST principles between resources:

GET /tickets/12/messages- Retrieves list of messages for ticket #12

GET /tickets/12/messages/5- Retrieves message #5 for ticket #12

POST /tickets/12/messages- Creates a new message in ticket #12

PUT /tickets/12/messages/5- Updates message #5 for ticket #12

PATCH /tickets/12/messages/5- Partially updates message #5 for ticket #12

DELETE /tickets/12/messages/5- Deletes message #5 for ticket #12

Among them, if this association and resources are independent, then we can save the endpoint of the corresponding resource in the output representation of the resource. The API user can then find the relevant resources by clicking on the link. If the association and resources are closely linked. The output of the resource indicates that the corresponding resource information should be saved directly. (For example, if the message resource exists independently, then GET /tickets/12/messages will return the link of the corresponding message; if the message does not exist independently, and the ticket attachment exists, then the above API call returns directly. Message information)

Non-compliant with CURD

Here are some solutions to this confusing problem:

Refactor your behavior action. When your behavior does not require parameters, you can map active to the activated resource (update using patch).

Treated as sub-resources. For example: on github, add a star operation to a gists: PUT /gists/:id/star and cancel the star operation: DELETE /gists/:id/star.

Sometimes the action is really not difficult to correspond to a resource such as search. Then do it. I don't think API users will have much opinion about the /search url (after all, he is easy to understand). Just pay attention to writing clearly in the document.

Aluminum Alloy Housing Integrator

Aluminum Alloy Housing Integrator,Metal Case Integrator,Three Phase Metal Case Integrator,Three Phase Aluminum Alloy Housing Integrator

Zibo Tongyue Electronics Co., Ltd , https://www.tongyueelectron.com