Method, device and equipment for automatic conversion of API result code
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHINA UNITECHS
- Filing Date
- 2022-12-13
- Publication Date
- 2026-07-03
Smart Images

Figure CN116346912B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention generally relate to the field of distributed system technology of computer software, and particularly to a method, apparatus and device for automatic conversion of API result codes. Background Technology
[0002] Control API delivery is one of the core capabilities of the acquisition and control platform. Clients call the acquisition and control API to deliver the message. The API, through its internal logic, re-encapsulates the request message and forwards it to the corresponding acquisition or orchestration system. Once acquisition is complete, it returns the acquisition execution result. The control API then performs logical transformation on the returned result and returns the message to the client. Because the control API connects to a large number of clients and servers belonging to different platforms and vendors, their defined request message result codes and descriptions differ, leading to errors in result parsing. As middleware, the control API needs to perform unified encoding conversion to achieve a unified protocol and perfect integration of requests and returned results. However, considering the massive number of actual API delivery scenarios, relying solely on programmatic enumeration for conversion is impractical. First, it's impossible to completely cover all result codes and descriptions. Second, as the acquisition and control platform is used, more result codes and descriptions will emerge, requiring program modifications for each addition. This approach is extremely time-consuming and manpower-intensive, including upgrades and regression testing, and suffers from incomplete scenario coverage.
[0003] For example, the patent "A Method for Protocol Conversion in an API Gateway (Application No.: CN201811051226.9)" states: A. The terminal APP initiates an access request to the API gateway; B. The API gateway parses the request information; C. Based on the parsing result in step B, it determines whether the access request needs conversion. If so, proceed to step D; otherwise, proceed to step H; D. Convert the access request and proceed to step E; E. Route the converted request information to the backend microservice and proceed to step F; F. The backend microservice processes the access request and returns the result to the API gateway, proceeding to step G; G. Convert the received result information and proceed to step J; H. Route the request information to the backend microservice; I. The backend microservice processes the access request and returns the result to the API gateway, proceeding to step J; J. The result information is returned to the terminal APP. This solution allows the API gateway to adapt to various microservice backends, which is beneficial for realizing the microservice integration of heterogeneous and complex systems. However, it does not solve the problem that when the API execution is abnormal, the error coding and error description within the control cannot meet the client's needs, and the control API's conversion of result codes and result descriptions lacks scalability. Summary of the Invention
[0004] To address the above issues, this invention enables automatic conversion between error codes and descriptions by customizing settings such as whether to enable error code conversion, configuring dictionary table data, and whether to enable external error code conversion. Error code data is added to memory at project startup, eliminating the need to query the database for error code data with each API call, thus reducing database interaction, lowering database pressure, and improving program execution efficiency. This approach does not impact API deployment performance in high-concurrency scenarios. Furthermore, a timed refresh function is provided with configurable frequency, and a manual refresh interface is available for manual data refresh.
[0005] According to embodiments of the present invention, a method, apparatus, and device for automatic conversion of API result codes are provided.
[0006] In a first aspect of the present invention, a method for automatic conversion of API result codes is provided. The method includes:
[0007] S01: By implementing the CommandLineRunner interface, a data refresh is performed when the project starts, converting the result code into data stored in memory;
[0008] S02: Define static variables to store the API return result description key value and the API return result encoding key value in memory;
[0009] S03: Periodically read or manually refresh memory data to update the pre-stored result code data in memory;
[0010] S04: Before the API returns the final result, determine whether error code conversion is enabled. If not, no conversion is performed; if enabled, perform regular expression matching on the result code and convert it into a standard result code and result description before returning it.
[0011] Furthermore, the result code mentioned in S01 includes internal result code and external result code. The internal result code conversion data is denoted as errCodeList and stored in a List, and the external result code conversion data is denoted as errCodeMapList and stored in a List.
[0012] Furthermore, the API return result description key value mentioned in S02 is denoted as resultDescMap and stored in Map, and the API return result encoding key value is denoted as resultCodeMap and stored in Map.
[0013] Furthermore, the steps for refreshing memory data as described in S03 are as follows:
[0014] S031: Clear preloaded internal result code conversion data;
[0015] S032: Clear preloaded external result code conversion data;
[0016] S033: Call the database query method, obtain the internal result code conversion data, and update errCodeList;
[0017] S034: Call the database query method, obtain external result code conversion data, and update errCodeMapList.
[0018] Furthermore, the method for determining whether error code conversion is enabled as described in S04 is as follows: define the environment variable CCFAPI_ERRORCODE_CONVERT, with the specific value being customizable. When the value is Y, it indicates that error code conversion is enabled; otherwise, it is not enabled.
[0019] Furthermore, the error code conversion steps described in S04 are as follows:
[0020] S041: Sets whether to search for the result code flag, denoted as codeFlag, with a default value of true; sets whether to search for the result description flag, denoted as descFlag, with a default value of true;
[0021] S042: Iterate through the raw API return messages. If the key value being iterated over exists in resultCodeMap and codeFlag is true, mark it as resultCodeKey and codeFlag as false. If the key value being iterated over exists in resultDescMap and descFlag is true, mark it as resultDescKey, mark the return result description as resultDesc, and descFlag as false. When both codeFlag and descFlag are false, it means that the result code and result description have been found, and exit the current loop.
[0022] S043: Extract the numeric part of the result description using regular expressions; if it exists, record it as code.
[0023] S044: Traverse errCodeList, use the associated error code r_error_code as the key, store the target error description in a List, and store it as the value in a Map, denoted as codeMap;
[0024] S045: If the key of codeMap contains code, it means that the result code has been matched by r_error_code. Sort the values in ascending order according to priority, take the first value to get the target result code and target result description. If a custom English result description is defined, read the corresponding English description; reassign the returned result.
[0025] S046: If no match is found, sort errCodeList in ascending order by error_prior, prioritizing higher-priority matches. Traverse the sorted errCodeList, obtain the target error description regular expression error_msg_reg, and perform regular expression matching. If a match is found, the target error code has been found. If a custom English result description is defined, read the corresponding English description, end the loop, and reassign the returned result.
[0026] Furthermore, the error code conversion described in S04 also includes external error code conversion, defining the environment variable PUB_CKAPI_ERRCODE_OUT, the specific value of which supports custom configuration. When it is Y, it means that error code conversion is enabled; otherwise, it is not enabled.
[0027] Furthermore, the error code conversion steps are as follows:
[0028] Iterate through errCodeMapList, take the error code error_code plus specialty as the key, and the target error code and description to be output as the value, and store them in a Map data structure, denoted as refMap;
[0029] Determine whether refMap contains the target error code matched when matching the target error description regular expression error_msg_reg. If it does, retrieve the corresponding external error code and description, and reassign the value to the returned result.
[0030] In a second aspect of the invention, an apparatus for automatic conversion of API result codes is provided. The apparatus includes:
[0031] The conversion module is used to perform a data refresh when the project starts by implementing the CommandLineRunner interface, converting the result code into data stored in memory.
[0032] Definition module: Used to define static variables and store the API return result description key value and the API return result encoding key value in memory;
[0033] Refresh module: Used to periodically read or manually refresh memory data to update the pre-stored result code data in memory;
[0034] Error code start-up judgment module: It is used to determine whether error code conversion is enabled before the control API returns the final result. If it is not enabled, no conversion is performed; if it is enabled, regular expression matching of the result code is performed to convert it into a standard result code and result description before returning it.
[0035] In a third aspect of the invention, an electronic device is provided. The electronic device includes a memory and a processor, the memory storing a computer program, the processor executing the program to implement the method according to a first aspect of the invention.
[0036] In a fourth aspect of the invention, a computer-readable storage medium is provided having a computer program stored thereon, which, when executed by a processor, implements the method according to a first aspect of the invention.
[0037] The above-mentioned English abbreviations are explained as follows:
[0038] API: Application Programming Interface
[0039] List: A data structure in Java programs
[0040] Map: A data structure in Java programs
[0041] This invention enables automatic conversion between error codes and descriptions by customizing settings such as whether to enable error code conversion, configuring dictionary table data, and whether to enable external error code conversion. Error code data is added to memory at project startup, eliminating the need to query the database for error code data with each API call, thus reducing database interaction, lowering database pressure, and improving program execution efficiency. It also does not impact API deployment performance in high-concurrency scenarios. Furthermore, it provides a timed refresh function with configurable frequency and a manual refresh interface for manual data refresh.
[0042] It should be understood that the description in the Summary of the Invention is not intended to limit the key or essential features of the embodiments of the present invention, nor is it intended to restrict the scope of the invention. Other features of the invention will become readily apparent from the following description. Attached Figure Description
[0043] The above and other features, advantages, and aspects of the various embodiments of the present invention will become more apparent from the accompanying drawings and the following detailed description. Wherein:
[0044] Figure 1 A flowchart of a method for automatic conversion of API result codes according to an embodiment of the present invention is shown;
[0045] Figure 2A block diagram of an apparatus for automatic conversion of API result codes according to an embodiment of the present invention is shown;
[0046] Figure 3 A schematic diagram of a device for home broadband service diagnostics based on a novel network according to an embodiment of the present invention is shown. Detailed Implementation
[0047] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0048] According to embodiments of the present invention, a method, apparatus, and device for automatic conversion of API result codes are proposed. Automatic conversion of error codes and descriptions is achieved through customizable configurations such as whether to enable error code conversion, configuring dictionary table data, and whether to enable external error code conversion. Error code data is added to memory at project startup, eliminating the need to query the database for error code data each time the control API is issued, reducing database interaction, lowering database pressure, and improving program execution efficiency. It does not impact API issuance in high-concurrency scenarios. A timed refresh function is provided, with configurable timing frequency, and a manual refresh interface is provided for manual data refresh.
[0049] The principles and spirit of the present invention will be explained in detail below with reference to several representative embodiments.
[0050] Figure 1 This is a schematic flowchart of a method for automatic conversion of API result codes according to an embodiment of the present invention. The method includes:
[0051] S01: By implementing the CommandLineRunner interface, a data refresh is performed when the project starts, converting the result code into data stored in memory;
[0052] S02: Define static variables to store the API return result description key value and the API return result encoding key value in memory;
[0053] S03: Periodically read or manually refresh memory data to update the pre-stored result code data in memory;
[0054] S04: Before the API returns the final result, determine whether error code conversion is enabled. If not, no conversion is performed; if enabled, perform regular expression matching on the result code and convert it into a standard result code and result description before returning it.
[0055] It should be noted that although the operation of the method of the present invention has been described in a specific order in the above embodiments and figures, this does not require or imply that the operations must be performed in that specific order, or that all the operations shown must be performed to achieve the desired result. Additionally or alternatively, certain steps may be omitted, multiple steps may be combined into one step, and / or one step may be broken down into multiple steps.
[0056] To provide a clearer explanation of the above method for automatic conversion of API result codes, a specific embodiment will be used for illustration below. However, it is worth noting that this embodiment is only for better illustrating the present invention and does not constitute an improper limitation of the present invention.
[0057] The following concrete example will further illustrate the method of automatic conversion of API result codes:
[0058] The system is configured with the original result code and result description key values as reusltId and reusltDesc.
[0059] HTTP request, sending API JSON formatted parameters:
[0060] {
[0061] "DeviceIP": "10.48.138.3"
[0062] }
[0063] The API parses the request, calls the corresponding backend data collection service, and receives the following raw response message:
[0064] {
[0065] "reusltId": "0001",
[0066] "reusltData": {
[0067] "rawResult": "{\"dis version\":\"fake device output\"}",
[0068] "dis version": "fake device output"
[0069] },
[0070] "reusltDesc": "The dis command failed to execute."
[0071] }
[0072] Enable the error code conversion switch and configure the corresponding conversion rules in the table, as shown in Table 1:
[0073] Table 1
[0074] error_code r_error_code error_msg_reg error_msg_en error_msg_zh error_prior 000000009999 [NULL] .* system error System error 1000
[0075] The original error codes and error descriptions are converted according to the conversion rules, and the result is returned:
[0076] {
[0077] "reusltId": "000000009999",
[0078] "reusltData": {
[0079] "rawResult": "{\"dis version\":\"fake device output\"}",
[0080] "dis version": "fake device output"
[0081] },
[0082] "reusltDesc": "System Error"
[0083] }
[0084] Furthermore, if the error description is configured to be returned in English, then the reusltDesc in the above result will be systemerror:
[0085] {
[0086] "reusltId": "000000009999",
[0087] "reusltData": {
[0088] "rawResult": "{\"dis version\":\"fake device output\"}",
[0089] "dis version": "fake device output"
[0090] },
[0091] "reusltDesc": "system error"
[0092] }
[0093] Furthermore, if the external error code conversion switch is enabled, the corresponding external conversion rules are configured in the table, as shown in Table 2:
[0094] Table 2
[0095] error_code o_error_code error_msg specialty 000000009999 6666 SYSTEM EXCEPTION1 IPF 000000009999 6667 SYSTEM EXCEPTION2 IPM 000200000002 6668 END error! IPF
[0096] The system will then match the corresponding data in the table based on the major of the currently issued API and the error code 000000009999. For example, if the major of the current API is IPM, the final result after external error code conversion will be:
[0097] {
[0098] "reusltId": "6667",
[0099] "reusltData": {
[0100] "rawResult": "{\"dis version\":\"fake device output\"}",
[0101] "dis version": "fake device output"
[0102] },
[0103] "reusltDesc": "SYSTEM EXCEPTION2"
[0104] }
[0105] Furthermore, the error code transformation data is loaded into memory when the project starts. However, the error code data may change during project execution, such as deleting or adding a new error code transformation. In this case, it is necessary to dynamically refresh the error code data loaded in memory. Because this dictionary-type data does not change particularly frequently and is maintained manually, the data refresh can be triggered passively or actively through scheduled tasks or APIs. When a data refresh task is triggered, the execution logic is as follows:
[0106] 1. Clear the preloaded internal result code conversion data;
[0107] 2. Clear the preloaded external result code conversion data;
[0108] 3. Call the database query method to obtain the internal result code conversion data and update errCodeList;
[0109] 4. Call the database query method to obtain external result code conversion data and update errCodeMapList.
[0110] Furthermore, the unified error code table dc_ccf_errorcode of the data acquisition and control platform is shown in Table 2:
[0111] Table 2
[0112] Fields type name illustrate error_code varchar(20) Error code Primary key, error code r_error_code varchar(20) Related error codes The associated error code; if this error code is matched, then it is this error. error_msg_reg varchar(200) Error Regular Expression If the error message matches this regular expression, then it is considered an error. error_msg_en varchar(1000) English error description Cannot be empty error_msg_zh varchar(1000) Chinese error description error_prior int(11) Priority Error priority: the smaller the value, the higher the priority.
[0113] Furthermore, the error code mapping table (external) dc_ccf_errormap is shown in Table 4. This is generally project-specific data; if there is no project-specific data, this table does not need to contain any data.
[0114] Table 4
[0115] Fields type name illustrate error_code varchar(20) Error code Primary key, error code o_error_code varchar(20) External error codes Error codes output specialty varchar(50) major Different majors may output different error codes. error_msg varchar(1000) External error messages
[0116] Based on the same inventive concept, this invention also proposes a device for automatic conversion of API result codes. The implementation of this device can be found in the implementation of the method described above; repeated details will not be repeated. Figure 2 As shown, the device 100 includes:
[0117] Conversion module 101: This module implements the CommandLineRunner interface to perform a data refresh when the project starts, converting the result code into data stored in memory.
[0118] Definition Module 102: Used to define static variables and store the API return result description key value and the API return result encoding key value in memory;
[0119] Refresh module 103: Used to periodically read or manually refresh memory data to update the pre-stored result code data in memory;
[0120] Error code start judgment module 104: It is used to determine whether error code conversion is enabled before the control API returns the final result. If it is not enabled, no conversion is performed; if it is enabled, regular expression matching of the result code is performed to convert it into a standard result code and result description before returning it.
[0121] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working process of the described module can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0122] like Figure 3As shown, the device includes a central processing unit (CPU), which can perform various appropriate actions and processes based on computer program instructions stored in read-only memory (ROM) or loaded from storage units into random access memory (RAM). The RAM can also store various programs and data required for device operation. The CPU, ROM, and RAM are interconnected via a bus. Input / output (I / O) interfaces are also connected to the bus.
[0123] Multiple components in the device are connected to the I / O interface, including: input units such as keyboards and mice; output units such as various types of displays and speakers; storage units such as disks and optical discs; and communication units such as network interface cards (NICs), modems, and wireless transceivers. The communication unit allows the device to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.
[0124] The processing unit executes the various methods and processes described above, such as methods S01-S04. For example, in some embodiments, methods S01-S04 may be implemented as computer software programs tangibly contained in a machine-readable medium, such as a storage unit. In some embodiments, part or all of the computer program may be loaded and / or installed on the device via ROM and / or a communication unit. When the computer program is loaded into RAM and executed by the CPU, one or more steps of methods S01-S04 described above may be performed. Alternatively, in other embodiments, the CPU may be configured to execute methods S01-S04 by any other suitable means (e.g., by means of firmware).
[0125] The functions described above in this document can be performed at least in part by one or more hardware logic components. For example, exemplary types of hardware logic components that can be used, without limitation, include: field programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), payload programmable logic devices (CPLDs), and so on.
[0126] The program code used to implement the methods of the present invention can be written in any combination of one or more programming languages. This program code can be provided to a processor or controller of a general-purpose computer, special-purpose computer, or other programmable data processing device, such that when executed by the processor or controller, the program code causes the functions / operations specified in the flowcharts and / or block diagrams to be implemented. The program code can be executed entirely on the machine, partially on the machine, as a standalone software package partially on the machine and partially on a remote machine, or entirely on a remote machine or server.
[0127] In the context of this invention, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. Machine-readable media can include, but are not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0128] Furthermore, although the operations are described in a specific order, this should be understood as requiring that such operations be performed in the specific order shown or in sequential order, or requiring that all illustrated operations be performed to achieve the desired result. In certain environments, multitasking and parallel processing may be advantageous. Similarly, although several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of the invention. Certain features described in the context of individual embodiments may also be implemented in combination in a single implementation. Conversely, various features described in the context of a single implementation may also be implemented individually or in any suitable sub-combination in multiple implementations.
[0129] Although the subject matter has been described using language specific to structural features and / or methodological logic, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. Rather, the specific features and actions described above are merely illustrative examples of implementing the claims.
Claims
1. A method for automatic conversion of API result codes, characterized in that, The method includes: S01: By implementing the CommandLineRunner interface, a data refresh is performed when the project starts, converting the result code into data stored in memory. The result code includes internal result code and external result code. The converted data of internal result code is recorded as errCodeList and stored in List, and the converted data of external result code is recorded as errCodeMapList and stored in List. S02: Define static variables to store the API return result description key value and the API return result encoding key value in memory. The API return result description key value is denoted as resultDescMap and stored in Map, and the API return result encoding key value is denoted as resultCodeMap and stored in Map. S03: Periodically read or manually refresh memory data to update the pre-stored result code data in memory; S04: Before the API returns the final result, determine whether error code conversion is enabled. If not, no conversion is performed; if enabled, perform regular expression matching on the result code, convert it into a standard result code and result description, and return it. The method for determining whether error code conversion is enabled is as follows: Define an environment variable CCFAPI_ERRORCODE_CONVERT, with a customizable value. When the value is Y, it indicates that error code conversion is enabled; otherwise, it is disabled. The error code conversion steps are as follows: S041: Sets whether to search for the result code flag, denoted as codeFlag, with a default value of true; sets whether to search for the result description flag, denoted as descFlag, with a default value of true; S042: Iterate through the raw API return messages. If the key value being iterated over exists in resultCodeMap and codeFlag is true, mark it as resultCodeKey and codeFlag as false. If the key value being iterated over exists in resultDescMap and descFlag is true, mark it as resultDescKey, mark the return result description as resultDesc, and descFlag as false. When both codeFlag and descFlag are false, it means that the result code and result description have been found, and exit the current loop. S043: Extract the numeric part of the result description using regular expressions; if it exists, record it as code. S044: Traverse errCodeList, use the associated error code r_error_code as the key, store the target error description in a List, and store it in a Map as the value, denoted as codeMap; S045: If the key of codeMap contains code, it means that the result code has been matched by r_error_code. Sort the values in ascending order according to priority, take the first value to get the target result code and target result description. If a custom English result description is defined, read the corresponding English description; reassign the returned result. S046: If no match is found, sort errCodeList in ascending order by error_prior, prioritizing higher-priority matches. Traverse the sorted errCodeList, obtain the target error description regular expression error_msg_reg, and perform regular expression matching. If a match is found, the target error code has been found. If a custom English result description is defined, read the corresponding English description, end the loop, and reassign the returned result.
2. The method for automatic conversion of API result codes according to claim 1, characterized in that, The steps for refreshing memory data as described in S03 are as follows: S031: Clear preloaded internal result code conversion data; S032: Clear preloaded external result code conversion data; S033: Call the database query method, obtain the internal result code conversion data, and update errCodeList; S034: Call the database query method, obtain external result code conversion data, and update errCodeMapList.
3. The method for automatic conversion of API result codes according to claim 1, characterized in that, The error code conversion described in S04 also includes external error code conversion, defining the environment variable PUB_CKAPI_ERRCODE_OUT. The specific value can be customized. When it is Y, it means that error code conversion is enabled; otherwise, it is not enabled.
4. The method for automatic conversion of API result codes according to claim 3, characterized in that, The error code conversion steps are as follows: Iterate through errCodeMapList, take the error code error_code plus specialty as the key, and the target error code and description to be output as the value, and store them in a Map data structure, denoted as refMap; Determine whether refMap contains the target error code matched when matching the target error description regular expression error_msg_reg. If it does, retrieve the corresponding external error code and description, and reassign the value to the returned result.
5. An apparatus for automatically converting API result codes, characterized in that, The device implements the method as described in any one of claims 1 to 4, comprising: The conversion module is used to perform a data refresh when the project starts by implementing the CommandLineRunner interface, converting the result code into data stored in memory. Definition module: Used to define static variables and store the API return result description key value and the API return result encoding key value in memory; Refresh module: Used to periodically read or manually refresh memory data to update the pre-stored result code data in memory; Error code start-up judgment module: It is used to determine whether error code conversion is enabled before the control API returns the final result. If it is not enabled, no conversion is performed; if it is enabled, regular expression matching of the result code is performed to convert it into a standard result code and result description before returning it.
6. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the program, it implements the method as described in any one of claims 1 to 4.
7. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Method for implementing protocol conversion by API gateway
CN108989356A
Data processing method, and error code dynamic compatibility method, apparatus and system
CN107038041A