How to effectively connect the api interface

1. Background

In normal work, a scenario that is often encountered is: Company A wants to connect to the API method of Company B. At this time, Company A must read the interface document of Company B, and find the API that it needs to interface with from the interface document. And according to the requirements of the interface document, complete the coding work, and finally complete the docking work.

This article is from the perspective of company A to connect with the API interface of company B.

2. Understand the basic agreement of the interface of company B

Under normal circumstances, Company B will provide the following similar agreements to meet the basic docking requirements, and will provide two sets of information about the test environment and the official environment.

appkey: Company A merchant platform id

appsecret: Company A merchant platform secret

3. Request and corresponding format instructions

In general, Company B will provide the request and the corresponding basic format instructions.

For example:

1. Request method

post

2. Request message format

application/json

3. Response message format

application/json

4. Request public parameters

For example, Company B has the following requirements

All interfaces need to pass the following parameters in Http Header mode;

Of course, the parameters offered by different companies vary and vary from company to company.

5. Description of response/callback parameters

For example: All APIs of company B have responses, and some APIs also have callback responses. Whether it is a response or a callback response, their parameter formats are the same.

6. Instructions for asynchronous callbacks

For example: Company B explains the asynchronous callback as follows:

Asynchronous callback:

Some specific interfaces need to return results asynchronously, so Merchant A needs to provide a callback address, encode it in base64, and configure it in the callback_url in the Http request Header.

Response mechanism:

The response mechanism means that when Merchant A receives the data notification from Company B, it must write back the success string, which is not case-sensitive. When Company B receives the "success", it will consider that Merchant A has received the notification; otherwise, it will continue to repeat the request Call back the interface 3 times, the time interval is 1s, 5s, 30s. If the access fails for 4 times, it will continue to poll for callbacks at intervals of 3 hours.

Callback decryption:

The callback is encrypted with aes and needs to be decrypted before use. In order to avoid callback failure due to network fluctuations, if you have not received a callback for a long time, please actively inquire.

7. Description and example of request body encryption

The data is encrypted with AES, and the encrypted value is used as the value of data.

Example:

Before encryption:

After encryption:

8. Callback decryption instructions and examples

The data is decrypted by AES, the data value part is decrypted, and the decrypted is a json string

Before decryption:

After decryption:

9. Generally, company B will also explain the code value.

4. Determine which APIs to connect to

Under normal circumstances, company B will provide the necessary APIs for a certain project, and we often only need to interface with a small number of API interfaces. Therefore, we must first confirm which API methods to interface with. We only need to connect according to the requirements of the API.

5. According to the API documentation, write some basic tool classes

There are two types of tools. For a category of tools, Company B will provide a DEMO, which we can use. The other category needs to be written by yourself according to the API requirements.

6. According to the API documentation, write the necessary DTO

For each API, it mainly includes request DTO, response DTO, and callback response DTO. This requires tracking API requirements and writing DTOs that meet the requirements.

Of course, some DTOs can be abstracted into a class. For example, the general response DTO and the callback response DTO are the same. At this time, it can be abstracted into a DTO.

7. For each API method, docking

After completing the above preparations, you can connect the interfaces one by one. The following methods can be used for docking one by one.

Guess you like

Origin blog.csdn.net/Api_k/article/details/130490812