API request function specification management method, terminal equipment and medium
By building an API request instance generator and a request function configuration file, we solve the problems of cumbersome writing of API request functions and confusing error handling logic in web application development, achieve standardization and flexible expansion of API request functions, and improve code quality and development efficiency. .
Patent Information
- Application Number
- CN202410572748.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-10
- Publication Date
- 2024-08-23
AI Technical Summary
In web application development, there are problems such as duplicate code, lack of unified specifications, difficulty in flexible expansion, and confusing error handling logic when writing front-end and back-end API request functions.
By building an API request instance generator and a request function configuration file, qualify and declare the request function type, generate a request instance, and execute the corresponding function when the response is successful and failed, and record the request configuration information in the form of strings or objects to support batch generation and flexible expansion.
Standardizes the definition of API request functions, reduces duplicate code, improves code readability and maintainability, supports flexible expansion and unified error handling, and improves business development efficiency.
Smart Images

Figure CN118535136A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Web applications, and in particular to an API request function specification management method, terminal equipment and medium. Background Art
[0002] In Web application development, collaborative development between the front-end and back-end is required. The front-end is responsible for page display and interaction, and the back-end provides data and operation interfaces. After the interface is set up on the back-end, the front-end needs to encapsulate each interface into an independent function according to the interface document. Usually, we need to manually define the input parameters and return value types of the function. When multiple people collaborate, the codes written by each person often cannot be guaranteed to follow unified specifications, and there will be a lot of duplicate code, and readability and maintainability cannot be guaranteed.
[0003] In addition, after the front end initiates an interface request, the response data received from the back end will contain the status identifier and error code of the request. For the processing of conventional error codes, the front end usually adopts a unified processing logic, but in some scenarios, customized processing logic is required for specific error codes. In previous practices, the API of the request library (such as Axios) is usually used to process the unified logic, and then the specific error logic is processed in the business. However, with the iteration of the business, the general and specific error handling logic has gradually diversified, and the processing logic in the code has gradually expanded, resulting in a decline in the overall code quality.
[0004] In summary, the existing technical solutions have the following deficiencies:
[0005] (1) Manually create API request functions. Each API request function calls different methods through the API request instance to initiate a request, resulting in a lot of duplicate code.
[0006] (2) The parameters and return values of each API request must be processed separately, without unified standards;
[0007] (3) API request instances are highly customized and cannot be flexibly expanded;
[0008] (4) There is no standardized solution for handling API errors, which is heavily coupled with the business and results in confusing code. Summary of the invention
[0009] In order to solve the above problems, the present invention proposes an API request function specification management method, terminal equipment and medium.
[0010] The specific plan is as follows:
[0011] A method for managing API request function specifications includes the following steps:
[0012] Construct an API request instance generator, limit the types of request functions supported by the request instances generated by the API request instance generator, and declare the types of request functions supported by the request instances generated by the API request instance generator;
[0013] Generate an API request instance through the API request instance generator;
[0014] Build a request function configuration file, which records the type, request data fields, and response data fields corresponding to each request function;
[0015] Build an API request function generator. Based on the request function configuration file, use the API request function generator to generate all request functions supported by the request instance.
[0016] The API makes a request through the generated request function, and executes the function that needs to be executed corresponding to the type declaration of the request in the API request instance generator when the response is successful or failed.
[0017] Furthermore, the type declaration of the request function includes a request header, a function to be executed when the request response is successful, and a function to be executed corresponding to each error code contained in the response when the request response fails.
[0018] Furthermore, when the request response fails, the error code recorded in the type declaration is checked from the response to see if there is an identical error code. If so, the function that needs to be executed corresponding to the error code is executed.
[0019] Furthermore, the request function configuration file records the configuration information of each request function in a string format, and the string format includes three fields: request method, path, and parameters in the path.
[0020] Furthermore, the request function configuration file records the configuration information of each request function in the form of an object, and the object includes a request method, a path, and parameters in the path.
[0021] An API request function specification management terminal device includes a processor, a memory, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the method described above in the embodiment of the present invention are implemented.
[0022] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method described above in an embodiment of the present invention are implemented.
[0023] The present invention adopts the above technical solution to solve the problems of the prior art, such as cumbersome front-end API code writing, lack of specifications, large amounts of duplicate code, and confusing interface error processing logic. While simplifying the interface definition, it provides complete data type prompts for the business logic, thereby standardizing API code writing and improving the readability and maintainability of the overall code. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 Shown is a flow chart of a method according to a first embodiment of the present invention. DETAILED DESCRIPTION
[0025] To further illustrate various embodiments, the present invention provides drawings. These drawings are part of the disclosure of the present invention, which are mainly used to illustrate the embodiments and can be used in conjunction with the relevant descriptions in the specification to explain the operating principles of the embodiments. With reference to these contents, ordinary technicians in this field should be able to understand other possible implementations and advantages of the present invention.
[0026] The present invention will now be further described with reference to the accompanying drawings and specific implementation methods.
[0027] Embodiment 1:
[0028] The embodiment of the present invention provides a method for managing API request function specifications, such as Figure 1 As shown, the method comprises the following steps:
[0029] S1: Construct an API request instance generator, limit the types of request functions supported by the request instances generated by the API request instance generator, and declare the types of request functions supported by the request instances generated by the API request instance generator.
[0030] In this embodiment, the type declaration of the request function includes a request header, a function to be executed when the request response is successful, and a function to be executed corresponding to each error code contained in the response when the request response fails.
[0031] The API request instance generator is used to generate request instances corresponding to each API. One API can correspond to one request instance, or multiple APIs can correspond to one request instance.
[0032] The request header is the metadata used to describe an HTTP request in the HTTP protocol. One request corresponds to one request header. The function of the request header is to pass some additional information to control or explain the request method, expected response and identity authentication, etc. The specific content of the request header can be customized and extended according to actual needs.
[0033] After the request is sent, the backend response may be successful or failed, and there are different error codes when it fails. In order to enable the API to automatically perform corresponding operations based on the response results, in this embodiment, corresponding functions that need to be executed are set for successful response and failed response, and different error codes correspond to different functions that need to be executed.
[0034] After receiving the incoming type declaration and supported calling function types, the API request instance generator generates a request instance through an existing third-party library (such as axios, etc.).
[0035] S2: Generate an API request instance through the API request instance generator.
[0036] S3: Build a request function configuration file, which records the type, request data fields, and response data fields corresponding to each request function.
[0037] The request function configuration file is used to batch configure the relevant information of each request function. In this embodiment, the configuration information of each request function is recorded in the request function configuration file in a string format, and the string format includes three fields: request method, path, and parameters in the path, such as getUser:'GET api / user / :id', getUser is the function name, GET is the request method, api / user is the requested path, and id is the parameter carried in the path; createUser:'POST api / user', createUser is the function name, POST is the request method, api / user is the requested path, and the path does not carry parameters.
[0038] The types of request functions include GET and POST. The fields of request data are the parameters carried when the request is initiated, and the fields of response data are the parameters carried in the response.
[0039] S4: Build an API request function generator. Based on the request function configuration file, generate all request functions supported by the request instance through the API request function generator.
[0040] The API request function generator is used to generate the request function recorded in the request function configuration file based on the request function configuration file. Since each request instance supports different types of request functions, in this embodiment, it is only necessary to generate the request functions supported by the request instance in combination.
[0041] In this embodiment, the request functions in string format (including GET and POST) generated in batches by the API request function generator are as follows:
[0042]
[0043] The above method can realize batch generation of API request functions, greatly reducing the workload.
[0044] Furthermore, in order to be applicable to more complex interface requests, this embodiment reserves space for custom extensions, that is, describing the API request function in a custom manner, such as describing it in the form of the following object:
[0045]
[0046] The method field indicates the request method, the url field indicates the request path, and the params field indicates the parameters carried in the path.
[0047] S5: The API makes a request through the generated request function, and executes the function that needs to be executed corresponding to the type declaration of the request in the API request instance generator when the response is successful or failed.
[0048] After the API sends the request to the backend, the backend will reply with a corresponding response. According to the different response types, combined with the record in the type declaration corresponding to the request in step S1, the corresponding function to be executed is executed.
[0049] The embodiments of the present invention have the following beneficial effects:
[0050] (1) The definitions of API request instances and API request functions are standardized, making the code more concise and clear;
[0051] (2) Supports the use of strings to define API request functions, reducing duplicate code and making coding simpler;
[0052] (3) The batch-generated API request function types are complete and support overwriting common parameters in API request instances, which can cover most usage scenarios and greatly improve business development efficiency;
[0053] (4) API request instances can be flexibly expanded, and common logic can be split and shared across businesses without any mental burden;
[0054] (5) Standardize API interface error handling to reduce coupling with business, while also supporting customized processing for special business needs.
[0055] Embodiment 2:
[0056] The present invention also provides an API request function specification management terminal device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps in the above method embodiment of embodiment 1 of the present invention are implemented.
[0057] Further, as an executable solution, the API request function specification management terminal device can be a computing device such as a desktop computer, a notebook, a PDA, and a cloud server. The API request function specification management terminal device may include, but is not limited to, a processor and a memory. Those skilled in the art can understand that the composition structure of the above-mentioned API request function specification management terminal device is only an example of the API request function specification management terminal device, and does not constitute a limitation on the API request function specification management terminal device. It may include more or less components than the above, or a combination of certain components, or different components. For example, the API request function specification management terminal device may also include input and output devices, network access devices, buses, etc., and the embodiments of the present invention do not limit this.
[0058] Further, as an executable solution, the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, etc. The general-purpose processor may be a microprocessor or any conventional processor, etc. The processor is the control center of the API request function specification management terminal device, and uses various interfaces and lines to connect various parts of the entire API request function specification management terminal device.
[0059] The memory can be used to store the computer program and / or module, and the processor implements the API request function specification management of various functions of the terminal device by running or executing the computer program and / or module stored in the memory, and calling the data stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system and at least one application required for a function; the data storage area can store data created according to the use of the mobile phone, etc. In addition, the memory can include a high-speed random access memory, and can also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0060] The present invention also provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the above method in the embodiment of the present invention are implemented.
[0061] If the module / unit integrated in the API request function specification management terminal device is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present invention implements all or part of the process in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory) and software distribution medium, etc.
[0062] Although the present invention has been specifically shown and described in conjunction with the preferred embodiments, it should be understood by those skilled in the art that various changes may be made to the present invention in form and details without departing from the spirit and scope of the present invention as defined by the appended claims, all of which are within the scope of protection of the present invention.
Claims
1. A method for managing API request function specifications, characterized in that: The following steps are involved: Construct an API request instance generator, limit the types of request functions supported by the request instances generated by the API request instance generator, and declare the types of request functions supported by the request instances generated by the API request instance generator; Generate an API request instance through the API request instance generator; Build a request function configuration file, which records the type, request data fields, and response data fields corresponding to each request function; Build an API request function generator. Based on the request function configuration file, use the API request function generator to generate all request functions supported by the request instance. The API makes a request through the generated request function, and executes the function that needs to be executed corresponding to the type declaration of the request in the API request instance generator when the response is successful or failed.
2. The API request function specification management method according to claim 1, characterized in that: The type declaration of the request function includes the request header, the function that needs to be executed when the request response is successful, and the function that needs to be executed corresponding to each error code contained in the response when the request response fails.
3. The API request function specification management method according to claim 1, characterized in that: When the request response fails, the error code recorded in the type declaration is checked from the response to see if there is the same error code. If so, the function that needs to be executed corresponding to the error code is executed.
4. The API request function specification management method according to claim 1, characterized in that: The request function configuration file records the configuration information of each request function in a string format, which includes three fields: request method, path, and parameters in the path.
5. The API request function specification management method according to claim 1, characterized in that: The request function configuration file records the configuration information of each request function in the form of an object, which includes the request method, path, and parameters in the path.
6. An API request function specification management terminal device, characterized in that: The method comprises a processor, a memory and a computer program stored in the memory and running on the processor, wherein the processor implements the steps of any one of the methods of claims 1 to 5 when executing the computer program.
7. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.
