API definition acquisition method, device and storage medium
By parsing the communication messages based on the API protocol and API definition, the API definition is automatically generated, which solves the problem that the API cannot be quickly imported in the existing technology, and realizes the automatic acquisition and efficient import of the API definition.
Patent Information
- Application Number
- CN202211593648.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-13
- Publication Date
- 2025-01-24
- Estimated Expiration
- 2042-12-13
AI Technical Summary
The lack of automated API definition tools in the existing technology has led to the inability to quickly import APIs. Especially when transforming the API of existing businesses, it requires a lot of manpower and time to manually define them.
By collecting communication messages based on API protocols and API definitions, the position, data type and encoding method of API definition in communication messages are determined, and the communication packets are analyzed based on this information to automatically generate API definition.
It realizes automatic acquisition of API definitions, improves API import efficiency, and solves the problem that APIs cannot be imported quickly due to the lack of automated definition tools in the existing technology.
Smart Images

Figure CN115941820B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of network communications, and in particular to a method, device and storage medium for obtaining API definitions. Background Art
[0002] The API in this document, namely the Application Programming Interface, refers to open API or OpenAPI, also known as interface. Software service providers can encapsulate their services into a series of APIs and open them to third-party developers for calling. API is widely used in the fields of mobile Internet, Internet of Things, e-commerce, social media, mobile payment, online car-hailing, banking, telecommunications, e-government, etc. Service operators provide APIs for third-party calls, and can also be called by their own official apps and mini-programs. When opening API services, service providers need to provide API documents and SDKs to third-party developers so that developers know the purpose and calling methods of these APIs. API documents and SDKs can be automatically generated from API definitions. Service providers also need to store API definitions in the API gateway system so that the API gateway can perform traffic and security control and implement service scheduling. The API definition includes the name, version, request parameter list, return value list of the API. The request parameters or return values include variable names, data types and other information. To define an API, the existing technology is mainly implemented by handwriting complex configuration files, which is time-consuming and labor-intensive. Some manufacturers have also implemented the definition of APIs through web forms, but it still requires considerable manual operations to fill in the relevant forms.
[0003] A business system often has a large number of APIs. Manually defining APIs would take up a lot of manpower and time, which is inefficient. Therefore, there is an urgent need for a technical solution to meet the need for automated definition of APIs. This need is particularly urgent when transforming existing businesses into APIs, because the code that actually executes the API business logic has been running stably for many years, but due to the lack of API definition information, the API gateway cannot be launched to ensure security and control, and API documents and SDKs cannot be automatically generated. To address this issue, some service providers in the industry already support the definition of APIs through API management software such as swagger and postman, but these API management tools only support a limited number of API protocols, and many companies' APIs are not supported by these tools. Even with these tools, you still have to write code manually to describe the API definition, which still cannot solve the need for fast import of existing APIs. Summary of the invention
[0004] In this embodiment, an API definition acquisition method, device and storage medium are provided to solve the problem in the related art that there is a lack of an automated API definition tool, resulting in an inability to quickly import an API.
[0005] In a first aspect, a method for obtaining an API definition is provided in this embodiment, and the method includes:
[0006] Collecting communication messages, wherein the communication messages are obtained by assembling request data and return data of the called API based on the API protocol and API definition;
[0007] According to the API protocol, determine the location, data type and encoding method of the API definition in the communication message;
[0008] The communication message is parsed based on the position, the data type and the encoding method to obtain the API definition.
[0009] In some embodiments, the API definition includes an API name, a version, and variable names and variable data types of request parameters and response parameters; and parsing the communication message based on the position, the data type, and the encoding method to obtain the API definition includes:
[0010] Based on the encoding method, decoding the communication message;
[0011] The parsing result of matching the position and the data type with the position and the data type corresponding to the name and the version determined by the API protocol is used as the name and the version of the API;
[0012] The parsing result that matches the position and the data type with the position and data type corresponding to the variable name and variable data type determined by the API protocol is used as the variable name and variable data type of the API request parameter and response parameter.
[0013] In some of the embodiments, parsing the communication message based on the location, the data type, and the encoding method to obtain the API definition further includes:
[0014] When the names and versions of the APIs in multiple communication messages are the same, the variable names and variable data types of the request parameters of the multiple communication messages are merged, and the variable names and variable data types of the response parameters of the multiple communication messages are merged to obtain the API definition.
[0015] In some embodiments, before determining the location, data type and encoding method of the API definition in the communication message according to the API protocol, the method further includes:
[0016] Set the token corresponding to the product, which is pre-bound to the API protocol;
[0017] Binding the collected communication message to the token;
[0018] Based on the binding relationship between the communication message and the token, and the corresponding relationship between the token and the API protocol, the API protocol corresponding to the communication message is obtained.
[0019] In some of the embodiments, after parsing the communication message based on the location, the data type and the encoding method to obtain the API definition, the method further includes:
[0020] Determining a structured storage format defined by the API;
[0021] The API definition is stored in a configuration file or a database based on the structured storage format.
[0022] In some embodiments, before determining the location, data type and encoding method of the API definition in the communication message according to the API protocol, the method further includes:
[0023] Collecting historical messages, wherein the API protocol corresponding to the historical messages belongs to a predetermined API protocol list;
[0024] Based on the historical messages, the machine learning model is trained with the corresponding API protocol as a label to obtain an API protocol parsing model;
[0025] The communication message is input into the API protocol parsing model to obtain the corresponding API protocol.
[0026] In some embodiments, the collecting communication messages includes:
[0027] Install a browser plug-in for collecting communication messages, and set the token in the browser plug-in;
[0028] When the browser corresponding to the browser plug-in calls the API, monitoring the communication message corresponding to the API based on the browser plug-in;
[0029] When the communication message meets the preset condition, the communication message and the token are collected.
[0030] In some of the embodiments, the collecting communication messages further includes:
[0031] Setting the token in the API server or reverse proxy server running the product;
[0032] When the client calls the API, the API server or the reverse proxy server monitors the communication message corresponding to the API;
[0033] When the communication message meets the preset condition, the communication message and the token are collected.
[0034] In a second aspect, in this embodiment, a device for obtaining API definition is provided, the device comprising:
[0035] A collection module, used to collect communication messages, wherein the communication messages are obtained by assembling the request data and return data of the called API based on the API protocol and API definition;
[0036] A determination module, used to determine the location, data type and encoding method of the API definition in the communication message according to the API protocol;
[0037] A parsing module is used to parse the communication message based on the position, the data type and the encoding method to obtain the API definition.
[0038] According to a third aspect, a computer-readable storage medium is provided in this embodiment, on which a computer program is stored. When the computer program is executed by a processor, the steps of the API definition acquisition method described in the first aspect are implemented.
[0039] Compared with the related art, the API definition acquisition method provided in this embodiment collects communication messages, and the communication messages are assembled to obtain the request data and return data of the called API based on the API protocol and API definition, thereby providing a data source for obtaining the API definition; according to the API protocol, the position, data type and encoding method of the API definition in the communication message are determined, so as to obtain necessary information for the subsequent parsing of the API definition from the communication message; the communication message is parsed based on the position, data type and encoding method to obtain the API definition, that is, the relevant information is extracted by parsing the communication message and the API definition is automatically generated. The API definition provides a common structured model to define the business-level parameters of APIs that comply with different API protocols, thereby solving the problem of the lack of API automation definition tools in the related art, which makes it impossible to quickly import the API.
[0040] Details of one or more embodiments of the present application are set forth in the following drawings and description to make other features, objects, and advantages of the present application more readily apparent. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0042] Figure 1 is a schematic diagram of an application environment of an API definition acquisition method in some embodiments of the present application;
[0043] Figure 2 is a flowchart of a method for obtaining API definitions in some embodiments of the present application;
[0044] Figure 3 is a flow chart of communication message parsing in some embodiments of the present application;
[0045] Figure 4 is a flowchart of obtaining an API protocol corresponding to a communication message in some embodiments of the present application;
[0046] Figure 5 is a flowchart of structured storage of API definitions in some embodiments of the present application;
[0047] Figure 6 is a flowchart of API protocol parsing by machine learning in some embodiments of the present application;
[0048] Figure 7 is a flow chart of collecting communication messages in some embodiments of the present application;
[0049] Figure 8 is a flow chart of collecting communication messages in other embodiments of the present application;
[0050] Fig. 9 is a flowchart of a method for obtaining API definitions in some preferred embodiments of the present application;
[0051] Fig.10 It is a structural block diagram of an API definition acquisition device in some embodiments of the present application. DETAILED DESCRIPTION
[0052] In order to more clearly understand the purpose, technical solutions and advantages of the present application, the present application is described and illustrated below in conjunction with the accompanying drawings and embodiments.
[0053] Unless otherwise defined, the technical terms or scientific terms involved in this application shall have the general meaning understood by people with ordinary skills in the technical field to which this application belongs. The words "one", "a", "a", "the", "these" and the like in this application do not represent quantitative restrictions, and they can be singular or plural. The terms "include", "comprise", "have" and any variants thereof involved in this application are intended to cover non-exclusive inclusions; for example, a process, method and system, product or device comprising a series of steps or modules (units) is not limited to the listed steps or modules (units), but may include unlisted steps or modules (units), or may include other steps or modules (units) inherent to these processes, methods, products or devices. The words "connect", "connected", "coupled" and the like involved in this application are not limited to physical or mechanical connections, but may include electrical connections, whether directly or indirectly. The "multiple" involved in this application refers to two or more. "And / or" describes the association relationship of associated objects, indicating that there can be three relationships. For example, "A and / or B" can mean: A exists alone, A and B exist at the same time, and B exists alone. Usually, the character " / " indicates that the objects associated with each other are in an "or" relationship. The terms "first", "second", "third", etc. involved in this application are only used to distinguish similar objects and do not represent a specific ordering of the objects.
[0054] Figure 1 Schematic diagram of the application environment of the API definition acquisition method in some embodiments of the present application. Figure 1 As shown, the method embodiment provided in this embodiment can be executed in a computer, a server or a similar computing device as shown in 104, for example, running on the computer 104. Figure 1 The API calling system is a client-server architecture, including an API server 105, a client 101 and a client 102, and a reverse proxy server 103 between the client 101 and the API server 105. In some embodiments, the reverse proxy server 103 may not exist. In some embodiments, only one of the client 101 and the client 102 may be retained. Among them, the client 101 includes an application APP, and the client 102 includes a browser plug-in. The computer 104 can collect communication messages through the reverse proxy server 103 or the browser plug-in 102, and obtain the API definition using the API definition acquisition method of this embodiment. In the absence of the reverse proxy server 103, the communication messages can also be collected directly through the API server 105. The reverse proxy server 104 can deploy security protection measures such as firewalls. It can be understood by ordinary technicians in this field that Figure 1The structure shown is for illustration only and does not limit the structure of the above-mentioned servers or clients. For example, the client 101, the client 102, the reverse proxy server 103, and the API server 105 may also include Figure 1 More or fewer components as shown, or with Figure 1 Different configurations shown.
[0055] The computer 104 may include one or more processors and a memory for storing data, wherein the processor may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA. The memory may be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the API definition acquisition method in this embodiment. The processor executes various functional applications and data processing by running the computer program stored in the memory, that is, implementing the above method. The memory may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include a memory remotely arranged relative to the processor.
[0056] In this embodiment, a method for obtaining API definition is provided. Figure 2 Flowchart of the API definition acquisition method in some embodiments of the present application. Figure 2 As shown, the process includes the following steps:
[0057] Step S201 , collecting communication messages, wherein the communication messages are obtained by assembling the request data and return data of the called API based on the API protocol and API definition.
[0058] When business interactions are carried out between the client and the server, the relevant information of the API to be called can be transmitted through communication messages. For example, when a user operates an application APP on a mobile terminal, it can be regarded as sending an API call request to the server and requesting to obtain the execution result corresponding to the operation. Therefore, the relevant information of the API may include the request data and return data of the called API, including the name and version of the called API as the identifier of the called API; and the variable names, variable data types, etc. of the request parameters and response parameters required for the execution of the API. By collecting the communication message, the above information of the called API can be collected. The collection method here can be a browser plug-in or a reverse proxy, etc. This embodiment does not limit the collection method.
[0059] The following uses a weather query API as an example. For example, the client wants to query the weather in Beijing at 11:00 on February 11, 2015, and sends a query request to the server. The query request is:
[0060]
[0061]
[0062] In the communication messages of the query request and the returned result, the API definition content included in the query request includes:
[0063] API name: weather
[0064] API version: None
[0065] API parameters:
[0066] Parameter name meaning Data Types Example Value constraint citypinyin City Name String beijing Cannot be null
[0067] The returned results include the following API definitions:
[0068] API return value:
[0069] Parameter name meaning Data Types Example Value weather weather String clear temp Temperature Integer 10
[0070] It can be seen that the API definition stipulates the name of the called API, the variable names of the request parameters and response parameters, the data types, the meanings of the fields, and other information. It is used to tell the API caller how to call the API. It mainly stipulates the parameters at the business level and does not care about the technical details of communication, encoding, and transmission.
[0071] The three parameters errNum, errMsg, and retData in the above communication message are system parameters rather than business parameters. Therefore, their variable names and data types are not within the agreed scope of the API definition, but are determined by the API protocol. The API protocol is used to tell the API caller how to assemble API requests and parse API return data. It stipulates the location of the API definition in the request data and return data, the encoding method, the call result (success or failure), etc. The API protocol does not care about the business logic of the API, but only cares about the communication technology details. The API protocol does not determine the API definition. If the API definition already exists, the API protocol can be replaced for transmission. The communication message is obtained by assembling the request data and return data corresponding to the API definition according to the encoding method and message structure agreed upon in the API protocol.
[0072] Step S202: According to the API protocol, determine the location, data type and encoding method of the API definition in the communication message.
[0073] The API protocol specifies the format in which the API definition is presented in the communication message, including the data type, encoding method, and location in the communication message. For example, if an API protocol specifies that the name and version of the called API are in the body of the http message structure, then for APIs that comply with the API protocol, the name and version of the called API can be found in this location. API protocols can be public or customized within an organization or enterprise.
[0074] Step S203: Parse the communication message based on the location, data type and encoding method to obtain an API definition.
[0075] Since the communication message itself is generated by assembling the request data and return data corresponding to the API definition based on the API protocol, the location, data type and encoding method corresponding to the API definition can be obtained from the communication message according to the API protocol, and the message content at the location can be parsed based on the data type and encoding method to obtain the API definition.
[0076] Through the above steps S201 to S203, by collecting communication messages, the communication messages are assembled based on the API protocol and API definition to obtain the request data and return data of the called API, thereby providing a data source for obtaining the API definition; according to the API protocol, the position, data type and encoding method of the API definition in the communication message are determined, so as to obtain necessary information for the subsequent parsing of the API definition from the communication message; the communication message is parsed based on the position, data type and encoding method to obtain the API definition, that is, the relevant information is extracted by parsing the communication message and the API definition is automatically generated. The API definition provides a universal structured model to define the business-level parameters of APIs that comply with different API protocols, thereby solving the problem of the lack of an automated API definition tool in the related art, which results in the inability to quickly import the API.
[0077] In some embodiments, the API definition includes the name and version of the API, as well as the variable names and data types of the request parameters and response parameters. Figure 3 This is a flowchart of parsing communication messages according to location, data type and encoding method in some embodiments of the present application. Figure 3 As shown, the process includes the following steps:
[0078] Step S301, decoding the communication message based on the encoding method.
[0079] The encoding method refers to the data exchange format when transmitting data through the API, which can be JSON, XML, etc. During network transmission, the sender encodes the communication message according to the encoding method. After the receiver receives the encoded communication message, it decodes it according to the encoding method to obtain the corresponding data. The decoding method can be recursive decoding, which is a way to decode nested data. If the data is relatively simple, other methods can also be used to decode the communication message.
[0080] Step S302, the parsing result of matching the position and data type with the position and data type corresponding to the name and version determined by the API protocol is used as the name and version of the API.
[0081] The API protocol specifies the location of the API name and version in the communication message. For example, the HTTP request message can be divided into three parts: request line, header, and body, and the API protocol determines which part of the HTTP request message the name and version of the API definition are located in. For example, the API protocol specifies that the API name is located in the body part and the API version is located in the header part. Then the corresponding API name can be parsed from the body part of the communication message, and the corresponding API version can be parsed from the header part of the communication message.
[0082] The API protocol also specifies the data types of the API name and version in the API definition. For example, the API protocol specifies that the API name is located in the body part, and the variable name is method. In the process of parsing the body part of the communication message, if the variable name of the corresponding position parsing result is method and the data type is a string, it means that the position and data type of the parsing result can match the position and data type of the API name determined by the API protocol, so the parsing result can be used as the API name.
[0083] Step S303, using the parsing result that matches the position and data type with the position and data type corresponding to the variable name and variable data type determined by the API protocol as the variable name and variable data type of the request parameter and response parameter of the API.
[0084] Similar to step S302, according to the API protocol, determine the variable name of the API request parameter and response parameter, the position and data type of the variable data type in the communication message. For example, the API protocol stipulates that the variable name of the API response parameter is data, the variable data type is in the body of the communication message, and the data type of the variable data type is a structure. Then parse the body position of the communication message. If the variable name of the corresponding position parsing result is data and the data type is a structure, the parsing result is used as the response parameter.
[0085] Through the above steps S301 to S303, the communication message is decoded based on the encoding method, so that the receiver of the communication message obtains the same message data as the sender; the parsing result of the position and data type that matches the position and data type corresponding to the name and version determined by the API protocol is used as the name and version of the API, and the parsing result of the API name and version in the API definition is obtained; the parsing result of the position and data type that matches the position and data type corresponding to the variable name and variable data type determined by the API protocol is used as the variable name and variable data type of the request parameter and response parameter of the API, and the parsing result of the variable name and variable data type in the API definition is obtained, and the complete content of the API definition is obtained through the parsing of the communication message, which realizes the automatic acquisition of the API definition and improves the efficiency of API import.
[0086] In some embodiments, a method for parsing multiple communication messages to obtain API definitions is provided. The method includes:
[0087] When the names and versions of the APIs in multiple communication messages are the same, the variable names and variable data types of the request parameters of the multiple communication messages are merged, and the variable names and variable data types of the response parameters of the multiple communication messages are merged to obtain the API definition.
[0088] In actual applications, multiple clients call the same API, but the request parameter variables used may be different, and the response parameters returned may also be different. In this case, if only one communication message is parsed, the obtained API definition may be incomplete. The complete API definition can be obtained by merging the variable name and variable data type.
[0089] When the name and version of the API in multiple communication messages are the same, the uniqueness of the API can be determined. That is, the request parameter variables and response parameter variables in the multiple communication messages all belong to the same API. Therefore, the variable names and variable data types in each communication message used to send the call request can be merged to generate the variable name and variable data type of the request parameter of the API; the variable name and variable data type in each communication message used to send the return data can be merged to generate the variable name and variable data type of the response parameter of the API.
[0090] The API definition acquisition method in this embodiment obtains the API definition by merging the variable names and variable data types of the request parameters of multiple communication messages and merging the variable names and variable data types of the response parameters of multiple communication messages when the names and versions of the APIs in the multiple communication messages are the same. When it is determined that multiple communication messages call the same API, the variable names and variable data types of the request parameters and response parameters used in different communication messages are merged, thereby improving the completeness and correctness of the API definition.
[0091] In some of these embodiments, Figure 4 This is a flowchart of obtaining an API protocol corresponding to a communication message in some embodiments of the present application. Figure 4 As shown, the process includes the following steps:
[0092] Step S401, setting a token corresponding to a product, which is pre-bound to an API protocol.
[0093] The API definition acquisition method of this embodiment can be applied to multi-user, multi-organization, and multi-product scenarios. The product here refers to the application or service that calls the API. Different users, organizations, or products can use different API protocols. Different products using different API protocols can be distinguished by tokens. The binding relationship between the token and the API protocol can be saved in advance.
[0094] Step S402: Bind the collected communication message to the token.
[0095] When executing an API call, the client sets the token corresponding to the product in the communication message and sends it to the server. The token can be obtained when the communication message is collected through a browser plug-in or a reverse proxy server, API server, etc. The collected communication message is bound to the token.
[0096] Step S403: based on the binding relationship between the communication message and the token, and the corresponding relationship between the token and the API protocol, obtain the API protocol corresponding to the communication message.
[0097] Through the above steps S401 to S403, by setting the token corresponding to the product, the product is pre-bound to the API protocol, and the token is used to distinguish different products and corresponding API protocols; by binding the collected communication message with the token, the corresponding relationship between the communication message and the token is obtained; based on the binding relationship between the communication message and the token, and the corresponding relationship between the token and the API protocol, the API protocol corresponding to the communication message is obtained, providing necessary information for subsequent analysis of the communication message.
[0098] In some of these embodiments, Figure 5 Flowchart of structured storage of API definitions in some embodiments of the present application. Figure 5 As shown, the process includes the following steps:
[0099] Step S501, determine the storage format defined by the API.
[0100] After obtaining the API definition, since the API definition of each called API includes the name and version of the API, as well as the variable name and variable data type of the request parameter and response parameter, the API definition can be stored in a structured manner. The storage format of the API definition can be determined according to actual needs.
[0101] Step S502: storing the API definition into a configuration file or a database based on the storage format.
[0102] The database can be a relational database such as MySQL, a document database such as MongoDB, a KV database such as Redis, etc.
[0103] Through the above steps S501 to S502, by determining the storage format of the API definition, the content contained in the API definition is used as the basis for structured storage; by storing the API definition in a configuration file or database based on the storage format, it is convenient to query and read and write the API definition, thereby improving the efficiency of API import.
[0104] In some of these embodiments, Figure 6 This is a flowchart of API protocol parsing by machine learning in some embodiments of the present application. Figure 6 As shown, the process includes the following steps:
[0105] Step S601, collecting historical messages, the API protocols corresponding to the historical messages belong to a predetermined API protocol list.
[0106] According to a predetermined API protocol list, historical messages corresponding to the API protocol are collected for training the machine learning model. The API protocol list can be determined according to actual needs. The collection method can be the same as the method of collecting communication messages in step S201, or it can be collected by other methods. The historical messages are also obtained by assembling the request data and return data of the called API based on the API protocol and API definition.
[0107] Step S602: Based on the historical message, the machine learning model is trained with the corresponding API protocol as a label to obtain an API protocol parsing model.
[0108] The machine learning method in this embodiment can be deep learning or reinforcement learning, etc. The API protocol corresponding to the historical message is obtained according to the configuration file or according to the token, the API protocol is used as a label, and the machine learning model is trained using the historical message as training data to obtain the API protocol parsing model.
[0109] Step S603: input the communication message into the API protocol parsing model to obtain the corresponding API protocol.
[0110] Through the above steps S601 to S603, by collecting historical messages based on the API protocol, the original data for training the machine learning model is obtained; by training the machine learning model based on the historical messages and the API protocol as a label, the API protocol parsing model is obtained, and a method for obtaining the API protocol based on machine learning is provided; the API protocol is obtained by inputting the communication message into the API protocol parsing model, and when the collected communication message is not associated with the corresponding API protocol, the API protocol is obtained by machine learning, which expands the method of obtaining the API protocol.
[0111] In some of these embodiments, Figure 7 This is a flow chart of collecting communication messages in some embodiments of the present application. Figure 7 As shown, the process includes the following steps:
[0112] Step S701: Install a browser plug-in for collecting communication messages, and set a token in the browser plug-in.
[0113] There are many ways to collect network communication messages. The more common methods include obtaining through browser plug-ins or reverse proxy. For Web applications, it is recommended to use the browser plug-in method. For mobile apps or desktop applications, it is recommended to use the reverse proxy method for collection. Of course, the specific collection method depends on the situation. Set a token in the browser plug-in. The token can correspond to the client where the browser is located to distinguish different organizations or products, and the correspondence between the token and the API protocol has been pre-set.
[0114] Step S702: When the browser corresponding to the browser plug-in calls the API, the communication message corresponding to the API is monitored based on the browser plug-in.
[0115] The browser plug-in can obtain the browser's access request to the server. When it is found that the browser calls the API, it monitors the communication message corresponding to the API. The communication message includes request data and return data.
[0116] Step S703: When the communication message meets the preset conditions, the communication message and token are collected.
[0117] The monitored communication messages are analyzed to determine whether they meet preset conditions, which may include: the IP address, host name or domain name of the target Web application; the port number of the target Web application; the URL address prefix; and specific keywords in the URL, etc.
[0118] Through the above steps S701 to S703, by installing a browser plug-in for collecting communication messages and setting a token in the browser plug-in, the token used to distinguish different products or different organizations is matched with the communication message for calling the API; when the browser corresponding to the browser plug-in calls the API, a means of collecting communication messages is provided by monitoring the communication messages corresponding to the API based on the browser plug-in; by collecting the communication message and the token when the communication message meets the preset conditions, the communication message that meets the requirements is obtained for subsequent analysis.
[0119] In some of these embodiments, Figure 8 This is a flow chart of collecting communication messages in some other embodiments of the present application. Figure 8 As shown, the process includes the following steps:
[0120] Step S801, setting a token in the API server or reverse proxy server running the product.
[0121] Message collection can be performed based on the client's browser plug-in, or on the server side based on the API server or reverse proxy server. If it is based on the reverse proxy server, since the reverse proxy server connects the client and the server, it can obtain the request data sent by the client and the return data sent by the server. Set a token in the API server or reverse proxy server. The token can correspond to different clients to distinguish different organizations or products, and the correspondence between the token and the API protocol has been pre-set.
[0122] Step S802: When the client calls the API, the API server or the reverse proxy server monitors the communication message corresponding to the API.
[0123] According to the content of the request data, when it is found that the client calls the API, the communication messages corresponding to the request data and return data of the API are monitored.
[0124] Step S803: When the communication message meets the preset conditions, the communication message and token are collected.
[0125] The monitored communication messages are analyzed to determine whether they meet preset conditions, which may include: the IP address, host name or domain name of the target Web application; the port number of the target Web application; the URL address prefix; and specific keywords in the URL, etc.
[0126] Through the above steps S801 to S803, by setting a token in the API server or reverse proxy server running the product, the token used to distinguish different products or different organizations is matched with the communication message calling the API; when the client calls the API, a means of collecting communication messages is provided by monitoring the communication messages corresponding to the API based on the API server or reverse proxy server; by collecting the communication message and token when the communication message meets the preset conditions, the communication message that meets the requirements is obtained for subsequent analysis.
[0127] The present embodiment is described and illustrated below through a preferred embodiment. The API definition of the present preferred embodiment includes the name and version of the API, and the variable names and variable data types of the request parameters and response parameters.
[0128] Fig. 9 Flowchart of the API definition acquisition method of some preferred embodiments of the present application. Fig. 9 As shown, the process includes the following steps:
[0129] Step S901, obtaining a token corresponding to a product, which is pre-bound to the API protocol;
[0130] Step S902, setting the token in the browser plug-in or the reverse proxy server;
[0131] Step S903, monitoring API communication messages through a browser plug-in or a reverse proxy server;
[0132] Step S904: if the communication message meets the preset conditions, collect the communication message and the corresponding token;
[0133] Step S905, based on the binding relationship between the communication message and the token, and the corresponding relationship between the token and the API protocol, obtain the API protocol corresponding to the communication message;
[0134] Step S906, determining the location, data type and encoding method of the API definition in the communication message according to the API protocol;
[0135] Step S907, decoding the communication message based on the encoding method;
[0136] Step S908, the parsing result of matching the position and data type with the position and data type corresponding to the name and version determined by the API protocol is used as the name and version of the API;
[0137] Step S909, using the parsing result whose position and data type match the position and data type corresponding to the variable name and variable data type determined by the API protocol as the variable name and variable data type of the API request parameter and response parameter;
[0138] Step S910, when the names and versions of the APIs in the multiple communication messages are the same, the variable names and variable data types of the request parameters of the multiple communication messages are merged, and the variable names and variable data types of the response parameters of the multiple communication messages are merged to obtain an API definition;
[0139] Step S911, determining the storage format defined by the API;
[0140] Step S912: storing the API definition into a configuration file or a database based on the storage format.
[0141] Through the above steps S901 to S912, communication messages and corresponding tokens are collected through browser plug-ins or reverse proxy servers, which provides a source for obtaining API definitions and determines the API protocol corresponding to the communication message; the location, data type and encoding method of the API definition in the communication message are determined through the API protocol, and the communication message is parsed based on the location, type and encoding method to obtain the API definition, thereby achieving that different API protocols can obtain the API definition through this method; through structured storage of API definitions, an automated method is provided to extract API definitions that comply with different API protocols from communication messages, thereby solving the problem of lack of automated API definition tools in related technologies, which results in the inability to quickly import APIs.
[0142] It should be noted that the steps shown in the above process or the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0143] In some embodiments, the present application also provides an API definition acquisition device. The API definition acquisition device is used to implement the above embodiments and preferred implementations, and the descriptions that have been made will not be repeated. The terms "module", "unit", "sub-unit", etc. used below can be a combination of software and / or hardware that can implement a predetermined function.
[0144] In some embodiments, Fig.10 is a structural block diagram of the API definition acquisition device of this embodiment, such as Fig.10 As shown, the device comprises:
[0145] The collection module 1001 is used to collect communication messages, which are obtained by assembling the request data and return data of the called API based on the API protocol and API definition;
[0146] A determination module 1002 is used to determine the location, data type and encoding method of the API definition in the communication message according to the API protocol;
[0147] The parsing module 1003 is used to parse the communication message based on the location, data type and encoding method to obtain the API definition.
[0148] The API definition acquisition device provided in this embodiment acquires communication messages through the acquisition module 1001. The communication message is assembled based on the API protocol and the API definition to obtain the request data and the return data of the called API, thereby providing a data source for obtaining the API definition; according to the API protocol, the position, data type and encoding method of the API definition in the communication message are determined through the determination module 1002, so as to obtain necessary information for the subsequent parsing of the API definition from the communication message; the communication message is parsed based on the position, data type and encoding method through the parsing module 1003 to obtain the API definition, that is, the relevant information is extracted by parsing the communication message and the API definition is automatically generated. The API definition provides a universal structured model to define the business-level parameters of APIs that comply with different API protocols, thereby solving the problem of the lack of an automated API definition tool in the related art, which results in the inability to quickly import the API.
[0149] In some embodiments, the API definition includes the name and version of the API, as well as the variable names and variable data types of the request parameters and response parameters; the parsing module includes a decoding submodule, a first parsing submodule and a second parsing submodule; the decoding submodule is used to decode the communication message based on the encoding method; the first parsing submodule is used to use the parsing result that matches the position and data type with the name and version determined by the API protocol as the name and version of the API; the second parsing submodule is used to use the parsing result that matches the position and data type with the variable name and variable data type determined by the API protocol as the variable name and variable data type of the request parameters and response parameters of the API.
[0150] The API definition acquisition device provided in this embodiment decodes the communication message based on the encoding method through the decoding submodule, and converts the communication message byte stream transmitted on the network into data inside the program; through the first parsing submodule, the parsing result of matching the position and data type with the position and data type corresponding to the name and version determined by the API protocol is used as the name and version of the API, and the parsing result of the API name and version in the API definition is obtained; through the second parsing submodule, the parsing result of matching the position and data type with the position and data type corresponding to the variable name and variable data type determined by the API protocol is used as the variable name and variable data type of the request parameter and response parameter of the API, and the parsing result of the variable name and variable data type in the API definition is obtained, and the complete content of the API definition is obtained through the parsing of the communication message, thereby realizing automatic acquisition of the API definition and improving the efficiency of API import.
[0151] In some embodiments, the parsing module also includes a merging sub-module, which is used to merge the variable names and variable data types of the request parameters of the multiple communication messages, and merge the variable names and variable data types of the response parameters of the multiple communication messages when the names and versions of the APIs in the multiple communication messages are the same, so as to obtain the API definition.
[0152] The API definition acquisition device provided in this embodiment obtains the API definition by merging the variable names and variable data types of the request parameters of multiple communication messages and the variable names and variable data types of the response parameters of multiple communication messages when the names and versions of the APIs in the multiple communication messages are the same through the merging sub-module. When it is determined that multiple communication messages call the same API, the variable names and variable data types of the request parameters and response parameters used in different communication messages are merged, thereby improving the completeness and correctness of the API definition.
[0153] In some embodiments, the API definition acquisition device also includes a setting module, a binding module and an acquisition module; the setting module is used to set a token corresponding to the product, which is pre-bound to the API protocol; the binding module is used to bind the collected communication message to the token; the acquisition module is used to obtain the API protocol corresponding to the communication message based on the binding relationship between the communication message and the token, and the correspondence between the token and the API protocol.
[0154] The API definition acquisition device provided in this embodiment sets a token corresponding to a product through a setting module. The product is pre-bound to an API protocol, and the token is used to distinguish different products and corresponding API protocols; the collected communication message is bound to the token through a binding module to obtain a corresponding relationship between the communication message and the token; the API protocol corresponding to the communication message is obtained through an acquisition module based on the binding relationship between the communication message and the token, and the corresponding relationship between the token and the API protocol, thereby providing necessary information for subsequent parsing of the communication message.
[0155] In some embodiments, the API definition acquisition device further includes a second determination module and a storage module, wherein the second determination module is used to determine the storage format of the API definition; and the storage module is used to store the API definition into a configuration file or a database based on the storage format.
[0156] The API definition acquisition device provided in this embodiment determines the storage format of the API definition through the second determination module, and uses the content contained in the API definition as the basis for structured storage; the API definition is stored in a configuration file or database based on the storage format through the storage module, which facilitates the query and read-write import of the API definition and improves the efficiency of API import.
[0157] In some embodiments, the API definition acquisition device also includes a first collection submodule, a training submodule and an input submodule. The first collection submodule is used to collect historical messages based on the API protocol; the training submodule is used to train the machine learning model based on the historical messages and the API protocol as a label to obtain an API protocol parsing model; the input submodule is used to input the communication message into the API protocol parsing model to obtain the API protocol.
[0158] The API definition acquisition device provided in this embodiment collects historical messages based on the API protocol through a first collection submodule to obtain raw data for training a machine learning model; trains the machine learning model based on the historical messages and the API protocol as a label through a training submodule to obtain an API protocol parsing model, and provides a method for obtaining the API protocol based on machine learning; inputs the communication message into the API protocol parsing model through an input submodule to obtain the API protocol, and when the collected communication message is not associated with the corresponding API protocol, the API protocol is obtained through machine learning, thereby expanding the method for obtaining the API protocol.
[0159] In some embodiments, the collection module includes a first setting submodule, a first monitoring submodule and a second collection submodule. The first setting submodule is used to install a browser plug-in for collecting communication messages and set a token in the browser plug-in; the first monitoring submodule is used to listen to the communication message corresponding to the API based on the browser plug-in when the browser corresponding to the browser plug-in calls the API; the second collection submodule is used to collect the communication message and token when the communication message meets the preset conditions.
[0160] The API definition acquisition device provided in this embodiment installs a browser plug-in for collecting communication messages through a first setting submodule, and sets a token in the browser plug-in, so that the token used to distinguish different products or different organizations corresponds to the communication message that calls the API; when the browser corresponding to the browser plug-in calls the API, the first monitoring submodule monitors the communication message corresponding to the API based on the browser plug-in, thereby providing a means for collecting the communication message; and the second collecting submodule collects the communication message and the token when the communication message meets the preset conditions, so as to obtain the communication message that meets the requirements for subsequent analysis.
[0161] In some embodiments, the acquisition module also includes a second setting submodule, a second monitoring submodule and a third acquisition submodule. The second setting submodule is used to set a token in the API server or reverse proxy server running the product; the second monitoring submodule is used to monitor the communication message corresponding to the API based on the API server or reverse proxy server when the client calls the API; the third acquisition submodule is used to collect communication messages and tokens when the communication messages meet preset conditions.
[0162] The API definition acquisition device provided in this embodiment sets a token in the API server or reverse proxy server running the product through the second setting submodule, and corresponds the token used to distinguish different products or different organizations with the communication message calling the API; when the client calls the API, the second monitoring submodule monitors the communication message corresponding to the API based on the API server or reverse proxy server, providing a means of collecting communication messages; through the third collecting submodule, when the communication message meets the preset conditions, the communication message and token are collected to obtain the communication message that meets the requirements for subsequent analysis.
[0163] It should be noted that the above modules can be functional modules or program modules, and can be implemented by software or hardware. For modules implemented by hardware, the above modules can be located in the same processor; or the above modules can be located in different processors in any combination.
[0164] This embodiment further provides a computer device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to execute the computer program to perform the steps in any one of the API definition acquisition method embodiments in the above embodiments.
[0165] Optionally, the computer device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor, and the input / output device is connected to the processor.
[0166] Optionally, in this embodiment, the processor may be configured to perform the following steps through a computer program:
[0167] S1, collecting communication messages, which are obtained by assembling the request data and return data of the called API based on the API protocol and API definition;
[0168] S2, according to the API protocol, determine the location, data type and encoding method of the API definition in the communication message;
[0169] S3, parse the communication message based on the location, data type and encoding method to obtain the API definition.
[0170] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementation modes, and this embodiment will not be described in detail here.
[0171] In addition, in combination with the API definition acquisition method provided in the above embodiments, a storage medium may be provided in this embodiment to implement the method. The storage medium stores a computer program; when the computer program is executed by a processor, any one of the API definition acquisition methods in the above embodiments is implemented.
[0172] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementation modes, and will not be repeated in this embodiment.
[0173] It should be understood that the specific embodiments described herein are only used to explain the application, rather than to limit it. Based on the embodiments provided in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the protection scope of this application.
[0174] Obviously, the drawings are only some examples or embodiments of the present application. For ordinary technicians in the field, the present application can also be applied to other similar situations based on these drawings without creative work. In addition, it is understandable that although the work done in this development process may be complicated and lengthy, for ordinary technicians in the field, certain changes in design, manufacturing or production based on the technical content disclosed in this application are only conventional technical means and should not be regarded as insufficient content disclosed in this application.
[0175] The term "embodiment" in this application refers to a specific feature, structure or characteristic described in conjunction with the embodiment that can be included in at least one embodiment of the present application. The appearance of this phrase in various locations in the specification does not necessarily mean the same embodiment, nor does it mean that it is mutually exclusive with other embodiments and is independent or optional. It is clearly or implicitly understood by those of ordinary skill in the art that the embodiments described in this application can be combined with other embodiments without conflict.
[0176] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of patent protection. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the scope of protection of the present application. Therefore, the scope of protection of the present application shall be subject to the attached claims.
Claims
1. A method for obtaining API definition, characterized in that: The method comprises: Set the token corresponding to the product, which is pre-bound to the API protocol; Collecting communication messages, wherein the communication messages are obtained by assembling request data and return data of the called API based on the API protocol and API definition; Binding the collected communication message to the token; Based on the binding relationship between the communication message and the token, and the corresponding relationship between the token and the API protocol, obtain the API protocol corresponding to the communication message; According to the API protocol, determine the location, data type and encoding method of the API definition in the communication message; The communication message is parsed based on the position, the data type and the encoding method to obtain the API definition.
2. The method according to claim 1, characterized in that The API definition includes the name and version of the API, as well as the variable names and variable data types of the request parameters and response parameters; The parsing of the communication message based on the position, the data type and the encoding method to obtain the API definition includes: Based on the encoding method, decoding the communication message; The parsing result of matching the position and the data type with the position and the data type corresponding to the name and the version determined by the API protocol is used as the name and the version of the API; The parsing result that matches the position and the data type with the position and data type corresponding to the variable name and variable data type determined by the API protocol is used as the variable name and variable data type of the API request parameter and response parameter.
3. The method according to claim 2, characterized in that The parsing of the communication message based on the position, the data type and the encoding method to obtain the API definition further comprises: When the names and versions of the APIs in multiple communication messages are the same, the variable names and variable data types of the request parameters of the multiple communication messages are merged, and the variable names and variable data types of the response parameters of the multiple communication messages are merged to obtain the API definition.
4. The method according to claim 1, characterized in that: After parsing the communication message based on the position, the data type and the encoding method to obtain the API definition, the method further includes: Determining a structured storage format defined by the API; The API definition is stored in a configuration file or a database based on the structured storage format.
5. The method according to claim 1, characterized in that Before determining the position, data type and encoding method of the API definition in the communication message according to the API protocol, the method further includes: Collecting historical messages, wherein the API protocol corresponding to the historical messages belongs to a predetermined API protocol list; Based on the historical messages, the machine learning model is trained with the corresponding API protocol as a label to obtain an API protocol parsing model; The communication message is input into the API protocol parsing model to obtain the corresponding API protocol.
6. The method according to claim 1, characterized in that The communication message collection includes: Install a browser plug-in for collecting communication messages, and set the token in the browser plug-in; When the browser corresponding to the browser plug-in calls the API, monitoring the communication message corresponding to the API based on the browser plug-in; When the communication message meets the preset condition, the communication message and the token are collected.
7. The method according to claim 1, characterized in that The communication message collection also includes: Setting the token in the API server or reverse proxy server running the product; When the client calls the API, the API server or the reverse proxy server monitors the communication message corresponding to the API; When the communication message meets the preset condition, the communication message and the token are collected.
8. An API definition acquisition device, characterized in that: The device comprises: A collection module is used to set a token corresponding to a product, the product is pre-bound to the API protocol; collect communication messages, the communication messages are obtained by assembling the request data and return data of the called API based on the API protocol and API definition; bind the collected communication messages to the token; based on the binding relationship between the communication message and the token, and the corresponding relationship between the token and the API protocol, obtain the API protocol corresponding to the communication message; A determination module, used to determine the location, data type and encoding method of the API definition in the communication message according to the API protocol; A parsing module is used to parse the communication message based on the position, the data type and the encoding method to obtain the API definition.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the API definition acquisition method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Method, device and apparatus for generating interface description file and readable storage medium
CN110795084A
Automatic API interface document analysis and configuration method, system and device and storage medium
CN111553150A
Auto-documentation for application program interfaces based on network requests and responses
US20190037005A1