API calling method and device and readable storage medium
By generating a parameter verification model based on the target API code, real-time verification of parameters in API call requests is solved, and the problem of inability to verify API parameters in real-time in the existing technology is solved, and the accuracy and efficiency of API calls are improved.
Patent Information
- Application Number
- CN202510068740.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-16
- Publication Date
- 2025-02-28
AI Technical Summary
Existing API calling methods cannot verify parameters in real time after receiving the request, resulting in parameter errors that cause API call to fail and cannot provide accurate feedback, affecting the accuracy and efficiency of API calls.
When receiving the API call request sent by the user device, it determines the API-related parameter types and other information based on the stored target API code, generates a parameter verification model, and checks the parameters in the API call request to promptly notify the user device whether the parameter type is correct.
Real-time parameter verification of API call requests is implemented, which avoids API call failures caused by parameter errors, and improves the accuracy and efficiency of API calls.
Smart Images

Figure CN119537061A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of application programming interface management, and in particular to an API calling method, device and readable storage medium. Background Art
[0002] Currently, application programming interface (API) calls are mainly implemented through GET requests of the hypertext transfer protocol (HTTP). The values of API parameters (e.g., conditions to be queried, specific identifiers) are added in the uniform resource locator (URL) field of the GET request in the form of strings. For example, a GET request including the URL field is: https: / / example.com / api?param1=value1¶m2=value2, where param1 and param2 are the parameter names of the API parameters, and value1 and value2 are the values of the API parameters.
[0003] Since the parameters in the GET request provide specific operation instructions or data for the API, so that the API can perform corresponding operations according to these parameters and return appropriate results, in order to ensure the normal and accurate calling of the API, the parameter types need to be verified. However, currently, after receiving the above request, the server cannot verify the parameters in real time. In the case that a certain parameter error causes the API call to fail, the server can only return a general error message to the user, and cannot provide an accurate feedback, resulting in the user being unable to discover and correct the error in time, thereby affecting the accuracy and efficiency of the API call. Summary of the invention
[0004] The present application discloses an API calling method, apparatus and readable storage medium. When an API calling request sent by a user device is received, the method determines the parameter type and other information related to the API according to the stored target API code, determines a parameter verification model according to the parameter type and other information related to the API, verifies the parameter type and other information included in the API calling request according to the parameter verification model, determines whether the parameter type in the API calling request is correct, and promptly notifies the user device if it is incorrect. The method can promptly perform parameter verification on the API calling request, avoid the situation where the API calling fails due to parameter errors, and improve the accuracy and efficiency of the API calling.
[0005] In a first aspect, the present application provides an API calling method, the method comprising: receiving a first request for calling a first API sent by a user device; determining a parameter verification model based on parameter information extracted from a first API code corresponding to a stored first API, and verifying first parameter information included in the first request according to the parameter verification model, wherein the parameter information includes at least one parameter name, and one or more of a parameter type, a parameter format, and a parameter range corresponding to each parameter name in the at least one parameter name; when it is determined that there is an abnormality in the first parameter information, sending a first error response to the user device, the first error response is used to indicate first abnormal parameter information in the first parameter information, including one or more of a parameter type error, a parameter format error, or a parameter range error.
[0006] In the above process, when the server receives a request for calling an API sent by a user device, it can promptly verify the parameters in the request. Since the server in this application stores API codes corresponding to multiple APIs, such as Python code, etc., after determining the API code corresponding to the target API according to the request, determine at least one parameter name, parameter type, parameter format, and parameter range according to the parameter definition in the API code, and generate a parameter verification model based on the above content for parameter verification in the request. Specific parameter errors, such as parameter type errors, etc., can be determined, so that the user device can determine the specific parameter errors and perform modifications in a timely manner, and prevent the occurrence of API call failures caused by parameter mismatches. The server can still maintain stable operation under abnormal circumstances, thereby improving the accuracy and efficiency of API calls.
[0007] Exemplarily, the method also includes: when it is determined that there is no abnormality in the first parameter information, determining a first cache file corresponding to the stored first API according to the first API information included in the first request, wherein the first API information includes the platform, category, tool name, API name and calling parameters; according to the first parameter information, determining whether there is a first response result in the first cache file, the first response result being obtained by the first API processing according to the first parameter information; and when the first response result exists in the first cache file, sending the first response result to the user device.
[0008] In this application, multiple API call results are cached in the server. When the parameters in the request are normal, the cache file of the target API is first searched in the cache results, and the corresponding response result in the cache is determined based on the first parameter information in the request. Through the caching mechanism, the number and time of repeated calls to the external interface can be significantly reduced, and the response speed and server performance can be improved in high concurrency scenarios.
[0009] Exemplarily, the method also includes: when the first response result does not exist in the first cache file, determining the first API code corresponding to the stored first API according to the first API information; when the first API is an interface provided by a third-party service, establishing a connection with a network device according to the first parameter information and the first API code, the network device includes multiple APIs, and the first API is any one of the multiple APIs; when receiving a first return result determined by the network device according to the first API and the first parameter information, determining a second response result according to the first return result, and sending the second response result to the user device, wherein the second response result is the same as the first response result, and the formats of the first return result and the second response result are different.
[0010] In this application, the server stores API codes corresponding to multiple APIs from multiple sources. Compared with the current method of directly calling the API by the user device, calling the API through the API code does not require in-depth understanding of the underlying implementation details and complex calling methods of the API. Operations are implemented according to a unified interface code, which reduces the difficulty of use and improves the efficiency and accuracy of API calls.
[0011] Exemplarily, the method also includes: in case the first return result is not received and a return failure message sent by the network device is received, re-determining the second API code corresponding to the first API, the return failure message is used to indicate that the first API does not exist or the parameters included in the first parameter information do not exist; in case the first API is an interface provided by a third-party service, establishing a connection with the network device according to the first parameter information and the second API code; in case a second return result determined by the network device according to the first API and the first parameter information is received, determining a third response result according to the second return result, and sending the third response result to the user device, wherein the third response result is the same as the first response result, and the second return result and the third response result have different formats.
[0012] In the above process, when a failure message is received, the server starts the API information automatic collection program, re-acquires the information corresponding to the first API, generates and stores the corresponding API code, and obtains the corresponding response result based on the new API code. This can ensure that the user device obtains the latest services and data, and each API remains accessible. Compared with the current method that relies on manual collection, management and updating of API related information, it can significantly reduce labor costs.
[0013] Exemplarily, the specific process of redetermining the second API code corresponding to the first API is as follows: obtain the web page information of the first API; extract the first API document information from the web page information of the first API according to the extraction model, the extraction model is obtained through machine learning training, and the first API document information includes at least one or more of the second parameter information, the first API name, and the first API return value; determine the function name according to the first API name, determine the parameter definition according to the second parameter information, determine the function body and the document string according to the second parameter information, the first API name, and the first API return value, wherein the parameter definition is used to indicate the input variables of the function, the function body is used to indicate the calculation or operation logic, and the document string is used to describe the function function, parameters, and return value; determine the second API code according to the function name, parameter definition, function body, and document string.
[0014] The above process can also be applied to scenarios where the number of APIs from more sources is expanded. It can integrate API resources from multiple platforms without human intervention. By extracting API document information through extraction models, the efficiency and accuracy of data extraction can be improved. By generating API code, the extracted API document information can be converted into a unified interface code to facilitate API calls. The above operation is simpler than the current manual operation method and reduces labor costs.
[0015] Exemplarily, the method also includes: when the first API is not an interface provided by a third-party service, executing the first API code according to the first parameter information, determining a fourth response result, and sending the fourth response result to the user device, the fourth response result being the same as the first response result.
[0016] In the above process, the method in which the server directly determines the response result through the stored API code can avoid errors caused by network delays or network connection problems, improve execution efficiency, and perform data processing locally to obtain the response result, which can improve the stability and security of API calls.
[0017] In the second aspect, the present application provides an API calling device, which includes a receiving unit, a parameter verification unit and an error handling unit. The receiving unit is used to receive a first request sent by a user device, and the first request is used to call a first application programming interface API; the parameter verification unit is used to verify the first parameter information included in the first request according to a parameter verification model, wherein the parameter verification model is determined based on the parameter information extracted from the first API code corresponding to the stored first API, and the parameter information includes at least one parameter name, and the parameter type, parameter format and parameter range corresponding to each parameter name in the at least one parameter name; the error handling unit is used to send a first error response to the user device when it is determined that the first parameter information is abnormal, and the first error response is used to indicate the first abnormal parameter information in the first parameter information, including one or more of a parameter type error, a parameter format error or a parameter range error.
[0018] Exemplarily, the device further includes a processing unit, a cache unit and a sending unit. The processing unit is used to determine the first cache file corresponding to the first API in the cache unit according to the first API information included in the first request when it is determined that there is no abnormality in the first parameter information, wherein the first API information includes the platform, category, tool name, API name and call parameters; it is also used to determine whether there is a first response result in the first cache file according to the first parameter information, and the first response result is obtained by the first API processing according to the first parameter information; the sending unit is also used to send the first response result to the user device when the first response result exists in the first cache file.
[0019] Exemplarily, the processing module is also used to: when the first response result does not exist in the first cache file, determine the first API code corresponding to the stored first API according to the first API information; when the first API is an interface provided by a third-party service, establish a connection with the network device according to the first parameter information and the first API code, the network device includes multiple APIs, and the first API is any one of the multiple APIs; the sending unit is also used to determine the second response result according to the first return result when the receiving unit receives the first return result determined by the network device according to the first API and the first parameter information, and send the second response result to the user device, wherein the second response result is the same as the first response result, and the formats of the first return result and the second response result are different.
[0020] Exemplarily, the device also includes a code generation unit. The code generation unit, when the receiving unit does not receive the first return result and receives a return failure message sent by the network device, re-determines the second API code corresponding to the first API, and the return failure message is used to indicate that the first API does not exist or the parameters included in the first parameter information do not exist; when the first API is an interface provided by a third-party service, establishes a connection with the network device according to the first parameter information and the second API code; when receiving a second return result determined by the network device according to the first API and the first parameter information, determines a third response result according to the second return result, and sends the third response result to the user device, wherein the third response result is the same as the first response result, and the second return result and the third response result have different formats.
[0021] Exemplarily, the device also includes a collection unit and an information extraction unit. The collection unit is used to obtain the web page information of the first API; the information extraction unit is used to extract the first API document information from the web page information of the first API, the extraction model is obtained through machine learning training, and the first API document information at least includes one or more of the second parameter information, the first API name, and the first API return value; the code generation unit is also used to determine the function name according to the first API name, determine the parameter definition according to the second parameter information, determine the function body and the document string according to the second parameter information, the first API name, and the first API return value, wherein the parameter definition is used to indicate the input variable of the function, the function body is used to indicate the calculation or operation logic, and the document string is used to describe the function function, parameters, and return value; the code generation unit is also used to determine the second API code according to the function name, parameter definition, function body, and document string.
[0022] Exemplarily, the processing module is also used to execute the first API code according to the first parameter information when the first API is not an interface provided by a third-party service, and determine a fourth response result, which is the same as the first response result; and a sending unit is used to send the fourth response result to the user device.
[0023] In a third aspect, the present application provides a computing device cluster, which includes at least one computing device, the computing device including a processor and a memory, the processor is used to execute instructions stored in the memory so that the computing device executes the API calling method described in the first aspect.
[0024] In a fourth aspect, an embodiment of the present application provides a computer program product comprising instructions, which, when executed by a computing device, enables the computing device to implement the API calling method described in the first aspect.
[0025] In a fifth aspect, an embodiment of the present application provides a computer-readable storage medium, comprising computer program instructions, which, when executed by a computing device, enable the computing device to implement the API calling method described in the first aspect.
[0026] Based on the implementations provided in the above aspects, this application can also be further combined to provide more implementations. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following briefly introduces the drawings required for describing the embodiments.
[0028] Figure 1 It is a structural diagram of a virtual API system provided in an embodiment of the present application; Figure 2 It is a structural diagram of an API calling system provided in an embodiment of the present application; Figure 3 This is a flowchart of an API calling method provided by an embodiment of the present application; Figure 4 is a flow chart of a method for determining a second API code provided by an embodiment of the present application; Figure 5 is a schematic diagram of the structure of a computing device provided in an embodiment of the present application; Figure 6 is a schematic diagram of the structure of a computing device cluster provided in an embodiment of the present application; Figure 7 It is a structural diagram of one or more computing devices connected via a network provided by an embodiment of the present application. DETAILED DESCRIPTION
[0029] The following will describe the technical solutions in the embodiments of the present application in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0030] like Figure 1 As shown, Figure 1 It is a structural diagram of a virtual API system provided in an embodiment of the present application. The virtual API system 100 includes a cache unit 110 and an API simulation unit 120. The virtual API system 100 and the API aggregation platform 130 are interconnected through a network.
[0031] The cache unit 110 is used to cache the response results of multiple APIs; the API simulation unit 120 is used to simulate the response results of the API through a large language model when the response results of the API cannot be obtained; the API aggregation platform 130 can be a Rapid API, which includes various APIs from multiple different providers in multiple fields such as social media, payment gateways, and machine learning services, so that users can call and manage APIs through a unified interface and tools. The API aggregation platform can also be other platforms that include multiple APIs, which is not specifically limited in this application.
[0032] In a possible implementation, when the virtual API system receives a request to call an API sent by a user device, it searches the cache unit for a response result of the called target API; when the response result of the target API exists in the cache unit, the response result is sent to the user device; when the response result of the target API does not exist in the cache unit, the API aggregation platform is accessed according to the API call request in JavaScript object notation (JSON) format sent by the user, and the target API is called; when the response result of the target API is obtained by accessing the API aggregation platform, the response result is sent to the user device and cached in the cache unit; when the response result of the target API cannot be obtained through the API aggregation platform, the response result of the target API is simulated according to the large language model provided by the API simulation unit, the response result is sent to the user device, and the response result is cached in the cache unit.
[0033] In the above process, after receiving the request to call the API sent by the user device, the virtual API system did not promptly verify the parameters in the request. If there are abnormal parameters, the virtual API system cannot determine the specific parameter abnormality information and cannot promptly feedback to the user device, which will affect the efficient and accurate calling of the API.
[0034] In order to ensure the normal calling of API, the Berkeley function calling mechanism is currently used to verify the parameters in the request. However, this mechanism only supports one-layer nested structure and has certain limitations. The Berkeley function calling mechanism cannot effectively check the parameter type in scenarios where complex data needs to be processed. For example, when a verification object includes multiple layers of nested objects or arrays, the accuracy of parameter type verification cannot be guaranteed, which will affect the accuracy and performance of API calls.
[0035] To solve the above problems, the embodiment of the present application provides an API calling method. When receiving an API calling request sent by a user device, a parameter verification model is determined according to the API code corresponding to the target API, and the parameter information in the API calling request is verified according to the parameter verification model. When it is determined that the parameter information is abnormal, the specific parameter abnormality information is sent to the user device. The above process can verify the parameter information in the request in a timely manner, determine the specific parameter problem, and feedback it to the user device so that the user device performs the corresponding processing operation, which can avoid the failure of API calling due to parameter abnormality and improve the accuracy and efficiency of API calling. The above method is applied to Figure 2 in the system shown.
[0036] like Figure 2 As shown, Figure 2 2 is a schematic diagram of the structure of an API calling system provided in an embodiment of the present application. The API calling system includes an API platform 210, an API management system 220, and a client 230. Among them, there is a communication connection between the API platform 210, the API management system 220, and the client 230, which can be a wired connection or a wireless connection, which is not specifically limited in this application. Among them, the number of API platforms 210 that establish a communication connection with the API management system 220 can be one or more, which is not specifically limited in this application.
[0037] Optionally, the API platform 210 can be deployed on a computing device, usually on different computing devices, where the computing devices include bare metal servers (BMS), virtual machines, and containers. BMS refers to a general physical server, such as an ARM server or an X86 server; a virtual machine refers to a complete computer system with complete hardware system functions implemented by network functions virtualization (NFV) technology and simulated by software, running in a completely isolated environment; a container refers to a group of processes that are isolated from each other and are subject to resource constraints. The API platform 210 can also be deployed on a computing device cluster, which may include multiple of the above-mentioned computing devices.
[0038] Optionally, the API platform 210 includes Rapid API, which includes various APIs from multiple different providers in multiple fields such as social media, payment gateways, machine learning services, etc., providing a unified interface and platform so that clients can access the API more conveniently. The API platform also includes other API platforms, LeetCode website, etc., which are not specifically limited in this application.
[0039] Optionally, upon receiving an API call request sent by the API management system, the API platform 210 is used to forward the request to a server corresponding to a target API provider, receive API response information, and send the API response information to the API management system.
[0040] Optionally, the API management system 220 can be an API management platform, or an intelligent agent management or scheduling platform, which can be deployed on different computing devices, or on the same computing device, where the computing device includes a bare metal server, a virtual machine, a container, or an edge computing device.
[0041] Optionally, the client 230 and the API management system 220 can be deployed on different computing devices, or the client 230 is deployed on a terminal device and the API management system 220 is deployed on a computing device. When the client 230 and the API management system 220 are deployed on different computing devices, the client 230 and the API management system 220 can be deployed on different computing devices in the same computing device cluster, or on different computing devices in different computing clusters, which can be determined according to the actual application scenario and is not specifically limited in this application.
[0042] Optionally, the client 230 includes one or more user devices, which can be deployed on a terminal device or a computing device, wherein the terminal device includes a personal computer, a smart phone, a handheld processing device, a tablet computer, a mobile notebook, etc., which are not specifically limited here. The computing device can be a BMS, a virtual machine, a container or an edge computing device. The specific description can be referred to the above content, which is not repeated here.
[0043] Optionally, the API management system 220 is used to obtain the latest multiple API information from different platform sources from the API platform, process the multiple API information, generate API document information, and generate callable API code based on the API document information, wherein the API document information includes the name, type and parameter information of the API, such as parameter name, parameter type and parameter range, etc. The API code can be Python code or code represented by other programming languages, which is not specifically limited in this application. The API management system 220 is also used to cache the response results of multiple APIs sent by the API platform.
[0044] The API management system 220 is used to verify the parameters in the API call request after receiving it from the client, and send the parameter exception information to the client if the parameters are abnormal; if the parameters are normal, search the cached response results of multiple APIs to see whether there is a response result of the target API; if there is no response result of the target API in the cache, obtain the response result of the target API through the API call request and API code, and send the response result of the target API to the client; if there is a response result of the target API in the cache, send the response result of the target API directly to the client.
[0045] Optionally, the client 230 is used to send an API call request to the API management system, and receive a response result of the target API sent by the API management system.
[0046] It should be understood that the above Figure 2 The API management system 220 shown is only a possible example provided by an embodiment of the present application. The system may also include more types and quantities of devices, which is not specifically limited in the present application.
[0047] Furthermore, the API management system 220 can be further divided into a plurality of unit modules, for example, Figure 2 As shown, the API management system 220 may include a receiving unit 2201, a parameter verification unit 2202, and an error handling unit 2203. It should be understood that Figure 2 This is an exemplary division method. The API management system 220 may also include more or fewer units. The above units may be merged or split, and this application does not make any specific limitation on this.
[0048] The parameter verification unit 2202 may be a flow control software development kit (SDK), which is usually provided in the form of software and includes a series of interfaces, functions, class libraries, documents, etc., to help developers easily integrate flow control functions in their applications. The receiving unit 2201, the error handling unit 2203 and other units may also be implemented by software. In addition, the receiving unit 2201, the parameter verification unit 2202 and the error handling unit 2203 and other units may also be implemented by hardware.
[0049] For example, the parameter verification unit 2202 is taken as an example to introduce the implementation of the parameter verification unit 2202. Similarly, the implementation of the receiving unit 2201, the error processing unit 2203 and other units can refer to the implementation of the parameter verification unit 2202.
[0050] The parameter verification unit 2202 is taken as an example of a software functional unit. The parameter verification unit 2202 may include code running on a computing instance. The computing instance may include at least one of a physical host (computing device), a virtual machine, and a container. Furthermore, the computing instance may be one or more. For example, the parameter verification unit 2202 may include code running on multiple hosts / virtual machines / containers. It should be noted that the multiple hosts / virtual machines / containers used to run the code may be distributed in the same region or in different regions. Furthermore, the multiple hosts / virtual machines / containers used to run the code may be distributed in the same availability zone (AZ) or in different AZs, each AZ including one data center or multiple data centers with similar geographical locations. Generally, a region may include multiple AZs.
[0051] The parameter verification unit 2202 is taken as an example of a hardware functional unit, and the parameter verification unit 2202 may include at least one computing device, such as a server, etc. Alternatively, the parameter verification unit 2202 may also be a device implemented by an application-specific integrated circuit (ASIC) or a programmable logic device (PLD), etc. The PLD may be a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0052] The functions of each unit module in the API management system 220 are described below.
[0053] The receiving unit 2201 is configured to receive a first request sent by a user device, wherein the first request is used to call a first API.
[0054] The first request may be a GET request based on the hypertext transfer protocol (HTTP), including a request line, a request header, and the like. The request line includes a request method (GET), a uniform resource locator (URL) of the request, and an HTTP version; the request header includes a client type, a received response data type, a domain name of a target server, and the like.
[0055] The first request includes first parameter information. The parameter information is usually expressed by adding a key-value pair after the "?" in the URL field. For example, when the client accesses an API to obtain user information, it needs to pass the user ID as a parameter. When it is necessary to obtain user information whose user ID is 123, the URL in the GET request sent is: https: / / example.com / api / users?id=123, where id=123 is the parameter information, id is the parameter name, and 123 is the parameter value.
[0056] The first request also includes first API information, and the first API information includes the platform, category, tool name, API name, etc. to which the API belongs. In a possible implementation, the first request may also include more or less data, which is not specifically limited in this application.
[0057] The parameter verification unit 2202 is used to verify the first parameter information included in the first request according to the parameter verification model.
[0058] The parameter verification model is determined based on parameter information extracted from the first API code corresponding to the stored first API, and the parameter information includes at least one parameter name, and one or more of the parameter type, parameter format and parameter range corresponding to each parameter name in at least one parameter name, which is not specifically limited in this application.
[0059] The API management system 220 stores API codes corresponding to multiple APIs. The first API code is any one of the multiple API codes. The multiple API codes are obtained by the API management system in the process of integrating multiple APIs included in multiple API platforms from different sources. The specific generation process is as follows.
[0060] In a possible implementation, the API management system 220 further includes a collection unit 2204 , an information extraction unit 2205 , a code generation unit 2206 and a training unit 2207 .
[0061] The collecting unit 2204 is used to obtain web page information of multiple APIs in multiple API platforms, and the web page information of the first API is any one of the web page information of the multiple APIs.
[0062] Specifically, the acquisition unit 2204 selects and configures a WebDriver driver suitable for the target browser, sends a request to the target browser through WebDriver, and opens the target web page. After that, WebDriver can also simulate various operations of the user, such as clicking on a link, scrolling a page, etc., to access the home page of the API and the dynamic content loaded by the web page. Among them, the API home page may include the name of the website, a navigation bar for linking to different pages or functional areas, and an introduction and description related to the API, etc. The dynamic content may include real-time updated data, messages displayed according to operations, etc. In a possible implementation, the driver may have other possible types according to the changes in the browser, and the API home page and dynamic content may also include more types and quantities of data, which is not specifically limited in this application.
[0063] Specifically, after the target webpage is loaded through WebDriver, the acquisition unit 2204 simulates user operations through WebDriver and jumps to a page including an introduction and description related to the target API, thereby obtaining the hypertext markup language (HTML) text of the webpage.
[0064] In a possible implementation, the collection unit may also obtain the web page information of the API in other ways, which is not specifically limited in this application.
[0065] The information extraction unit 2205 is used to extract API document information from the API web page information according to the extraction model.
[0066] The extraction model is obtained by the training unit 2207 through machine learning training. The embodiment of the present application takes the case of determining the extraction model by supervised fine-tuning (SFT) of the pre-trained large language model according to the data of the RapidAPI platform as an example for explanation. The specific process is as follows: the acquisition unit accesses the website of the Rapid API through the browser driver of the automation tool Selenium. After obtaining an HTML text, according to the pre-set rules, the required API-related information is extracted, for example, the API name, return value, parameters, etc., to form a tuple of HTML text and API-related information. Multiple HTML texts and corresponding API-related information tuples constitute a training set. Afterwards, the pre-trained large language model is supervised and fine-tuned so that the large language model determines how to extract API-related information from a complex web page structure by learning how to extract API-related information from HTML text. Among them, the pre-trained large language model can be obtained by pre-training based on a large-scale data set, self-supervised learning or unsupervised learning algorithm. In a possible implementation, the large language model can also be obtained by training with other algorithms, which is not specifically limited in the present application.
[0067] Specifically, the training unit first performs data preprocessing, performs data cleaning on the binary groups of multiple HTML texts and corresponding API-related information, processes noise, errors, and incomplete data in the binary groups, and converts the data in the multiple binary groups into a unified format, for example, into a unified encoding format, etc. Afterwards, the extraction model is trained according to the set learning rate, batch size, and training cycle. Generate prediction results based on the preprocessed data and the parameters of the current large language model, determine the value of the loss function based on the prediction results and the actual labels (for example, the real API-related information, the real HTML text), determine the gradient of the loss function for each parameter in the model through the back propagation algorithm, and update the parameters of the large language model through the optimization algorithm. Among them, the loss function includes mean square error, cross entropy, etc., and the optimization algorithm includes stochastic gradient descent algorithm, etc., which is not specifically limited in this application. Afterwards, repeat the above process, and continuously adjust the parameters of the large language model during the training cycle until the performance of the model on the training set reaches the expected, and determine the extraction model.
[0068] The extraction model trained by the training unit can normally and accurately obtain API-related information when the HTML text structure is complex or changing, ensuring the continuity and stability of information extraction, improving the robustness and reliability of the system, and does not require human intervention, thereby reducing labor costs.
[0069] The API document information includes parameter information, API name, and API return value, etc. In a possible implementation, the API document information may include more or fewer types and quantities of data, which is not specifically limited in this application.
[0070] The code generation unit 2206 is used to determine the function name according to the API name, determine the parameter definition according to the parameter information, and determine the function body and the document string according to the parameter information, the API name and the API return value, wherein the parameter definition is used to indicate the input variables of the function, the function body is used to indicate the calculation or operation logic, and the document string is used to describe the function function, parameters and return value; determine the API code according to the function name, parameter definition, function body and document string.
[0071] In a possible implementation, the API code may be a Python code, or may be represented by other programming languages, which is not specifically limited in this application.
[0072] In the above process, the collection unit, information extraction unit and code generation unit work together to integrate API resources from multiple platforms. By generating API code, the extracted API document information can be converted into a unified interface code to facilitate API calls. The above API integration process does not require human participation, which can reduce labor costs.
[0073] Specifically, the embodiment of the present application takes the case where the first API code is Python code, and the parameter verification unit only needs to verify the parameter type in the request as an example. After the receiving unit receives the first API call request sent by a user device in the client, the parameter verification unit analyzes the first API code through the inspect library, and obtains information such as the parameter name and parameter type annotation through the attributes of the parameter object, thereby obtaining the parameter type of the parameter. For example, when the parameter type annotation corresponding to the parameter name param1 is "int", it is determined that param1 in the first API call request also needs to be expressed in the form of an integer. When the parameter type annotation corresponding to the parameter name param2 is "str", it is determined that param1 in the first API call request also needs to be expressed in the form of a string. Afterwards, a Pydantic model is constructed according to the parameter type information of the parameter, and the model is used as a parameter verification model to perform type verification on the parameters in the first API call request. For example, when the Pydantic model defines the same parameter name and parameter type as the first API code, it is checked whether the parameters in the first API call request input by the user device meet the defined parameter type requirements, that is, it is determined whether the value of the parameter corresponding to param1 is expressed in the form of an integer.
[0074] Similarly, the process of verifying other aspects of the parameters in the API call request is similar to the above process and will not be described in detail here.
[0075] In a possible implementation, when the first API code is described in other types of programming languages, the process by which the parameter verification unit verifies the parameters in the request is similar to the above process, and there may be adaptive modifications in the process of analyzing the first API code and generating a parameter verification model, which is not specifically limited in this application.
[0076] The error processing unit 2203 is configured to send a first error response to the user equipment when it is determined that the first parameter information is abnormal.
[0077] The first error response is used to indicate the first abnormal parameter information in the first parameter information, including one or more of a parameter type error, a parameter format error or a parameter range error, so that the user device can perform corresponding modifications to the API call parameters according to the first error response.
[0078] In the above process, the parameter verification unit and the error handling unit cooperate with each other. When the API call request sent by the user device is received, the parameters in the API call request can be verified in time, and when there are abnormalities in the parameters, the specific abnormal information can be fed back to the user device in time, so as to avoid situations such as API call failure caused by parameter abnormalities, thereby improving the accuracy and efficiency of API calls.
[0079] In a possible implementation, the API management system 220 further includes a cache unit 2208 and a sending unit 2209 .
[0080] The cache unit 2208 is used to cache one or more API cache files. The API cache file is used to save the response results of the previous API call and the corresponding parameter information, which can be: the parameter name and parameter value of the API call parameter, the result returned by the API, and the time of cache creation or update, etc. This application does not make specific restrictions on this. Each API cache file includes one or more cache entries, each cache entry corresponds to a specific API call and the corresponding API response result, and each specific API call corresponds to a parameter name and a specific parameter value.
[0081] The cache unit 2208 is used to determine the first cache file corresponding to the first API in the cache unit according to a pre-set first path rule and the first API information included in the first request when it is determined that there is no abnormality in the first parameter information. The first path rule is used to indicate the storage address of the cache file corresponding to the API in the system, which can be represented by the API type, the API function, and the API name, etc. Thereafter, based on the parameter names in the first parameter information and the values of the parameters corresponding to each parameter name, it is determined whether there is a first cache entry in the first cache file.
[0082] The sending unit 2209 is used to send a first response result corresponding to the first cache entry to the user device when there is a first cache file and a first cache entry in the cache unit, wherein the first response result is obtained by processing the first API according to the first parameter information.
[0083] In the above process, the cache unit and the sending unit cooperate with each other to pre-search the response result corresponding to the target API in the cache. This cache mechanism not only reduces the number and time of repeated calls to the external API, but also improves the system's throughput and response time. Especially in high-concurrency scenarios, the improvement in response speed and performance is more obvious.
[0084] In a possible implementation, the API management system 220 further includes an execution unit 2210 .
[0085] The execution unit 2210 is used to determine the first API code corresponding to the stored first API according to the first API information when there is no first response result in the first cache file.
[0086] The execution unit determines the first API code corresponding to the first API according to the preset second path rule and the first API information, wherein the second path rule is used to indicate the storage address of the API code corresponding to the API in the system. Specifically, the execution unit, when determining that the second path rule is represented by the API type, the API function and the API name, obtains the first API type, the first API function and the first API name included in the first API information, searches in the directory structure according to the above content, determines the corresponding first Python API file, and loads it into the memory, wherein the first Python API file includes the first API code.
[0087] In a possible implementation, the execution unit 2210 is used to establish a connection with a network device according to the first parameter information and the first API code when the first API is an interface provided by a third-party service, and send a first request to call the first API to the network device, wherein the network device may be a server deployed with a Rapid API platform, including multiple APIs provided by multiple different providers such as social and financial, the first API is any one of the multiple APIs, and the network device may also be other servers including APIs, which is not specifically limited in this application. Afterwards, the sending unit 2209 is used to determine a second response result according to the first return result when the receiving unit receives a first return result determined by the network device according to the first API and the first parameter information, and send the second response result to the user device, wherein the second response result is the same as the first response result, and the format of the first return result is different from that of the second response result.
[0088] Compared with the current method in which user devices directly call the API in network devices, the above process calls the API through API code. The execution unit does not need to have an in-depth understanding of the underlying implementation details and complex calling methods of the API. Based on the unified interface code, the difficulty of use is reduced and the efficiency and accuracy of API calls are improved. In a possible implementation, when the receiving unit does not receive the first return result and receives a return failure message sent by the network device, the collection unit, the information extraction unit and the code generation unit work together to re-determine the second API code corresponding to the first API, wherein the return failure message is used to indicate that the first API does not exist or the parameters included in the first parameter information do not exist, and the process of generating the second API code is the same as the process of determining the API code as described above, and will not be repeated here.
[0089] After determining the second API code corresponding to the first API, the execution unit repeats the above process to establish a connection with the network device according to the first parameter information and the second API code when the first API is an interface provided by a third-party service, and to determine whether a second return result determined by the network device according to the first API and the first parameter information is received. When the second return result is received, a third response result is determined according to the second return result, and the third response result is sent to the user device, wherein the third response result is the same as the first response result, and the formats of the second return result and the third response result are different.
[0090] In the above process, when a failure message is received, the server starts the API information automatic collection program, re-acquires the information corresponding to the first API, generates and stores the corresponding API code, and obtains the corresponding response result based on the new API code. This can ensure that the user device obtains the latest services and data, and each API remains accessible. Compared with the current method that relies on manual collection, management and updating of API related information, it can significantly reduce labor costs.
[0091] In another possible implementation, the execution unit is also used to execute the first API code according to the first parameter information when the first API is not an interface provided by a third-party service and is a non-external interface, and determine a fourth response result, which is the same as the first response result; and the sending unit is used to send the fourth response result to the user device.
[0092] The above method can be applied to the scenario of generating executable problem-solving code based on the content of the LeetCode website, which expands the application scope of this application and enables user devices to flexibly use the rich question bank provided by LeetCode to perform various programming exercises and algorithm verification. In the specific implementation process, the method in which the server directly determines the response result through the stored API code can avoid errors caused by network delays or network connection problems compared to the current method of obtaining the response result through network communication, improve execution efficiency, and perform data processing locally to obtain the response result, which can improve the stability and security of API calls.
[0093] In one possible implementation, the cache unit is used to cache a new response result determined by the execution unit, where the new response result may be the second response result, the third response result, or the fourth response result, so that when the same API call request is received next time, the corresponding response result can be directly sent to the user device, thereby reducing the number and time of repeated API calls and improving system performance.
[0094] The API management system provided in the above-mentioned embodiment of the present application can be an API management platform or an intelligent management system, etc. Through the mutual cooperation of various units, it can automatically integrate API resources of multiple platforms, and automatically update API resources, provide a unified interface code for user devices, and facilitate API calls; and during the API call process, timely verify the parameters in the API call request to ensure the accuracy and efficiency of the API call. All operation processes of the above-mentioned API management system do not require human participation, which can reduce labor costs.
[0095] like Figure 3 As shown, Figure 3 This is a flowchart of an API calling method provided in an embodiment of the present application. The method is applied to Figure 2 The API management system in the API calling system shown includes the following steps.
[0096] S310: Receive a first request sent by a user equipment.
[0097] The first request is used to call the first API, which is usually a GET request based on HTTP. In a possible implementation, the first request can also be other types of requests, which is not specifically limited in this application. The first request includes first parameter information and first API information, wherein the first parameter information includes the parameter name and the parameter value, and the first API information includes the platform to which the API belongs, the category, the tool name, the API name, the calling parameters, etc. In a possible implementation, the first request, the first parameter information and the first API information can include more or fewer types and quantities of data, which is not specifically limited in this application.
[0098] In a specific implementation, an HTTP-based GET request for calling an API is as follows: "https: / / example.com / api? platform=MyPlatform&category=DataAnalysis&tool=AnalysisTool&apiName=GetDataSummary¶m1=value1¶m2=value2", wherein "platform=MyPlatform" is used to indicate that the platform to which the API belongs is "MyPlatform"; "category=DataAnalysis" is used to indicate that the API category is "DataAnalysis"; "tool=AnalysisTool" is used to indicate that the API tool name is "AnalysisTool"; "apiName=GetDataSummary" is used to indicate that the API name is "GetDataSummary"; "param1=value1" and "param2=value2" are API call parameters; "param1" and "param2" are parameter names in the first parameter information; "value1" and "value2" are parameter values in the first parameter information.
[0099] S320: Verify the first parameter information included in the first request according to the parameter verification model.
[0100] The parameter verification model is determined based on parameter information extracted from the first API code corresponding to the first API stored, wherein the first API code is determined based on the first API information and can be Python code, or code in other programming languages. The parameter information extracted from the first API code includes at least one parameter name, and one or more of the parameter type, parameter format and parameter range corresponding to each parameter name, and this application does not make any specific limitations on this.
[0101] In a possible implementation, the parameter verification model is determined as follows: according to the first request sent by the received user device, the first API code corresponding to the first API is determined. When the first API code is a Python code, the first API code is analyzed through the inspect library to determine information such as parameter names and parameter types, and a Pydantic model is constructed based on the information such as parameter names and parameter types, so that the Pydantic model defines the same parameter names and parameter types as the parameters in the API, and the Pydantic model is used as a parameter verification model. It should be understood that the determination of the parameter verification model can also be achieved in other ways, which is not specifically limited in this application.
[0102] After the parameter verification model is determined, the first parameter information included in the first request is used as input data of the parameter verification model. For example, the parameter verification model defines the first parameter name "data_list", the second parameter name "threshold" and the third parameter name "name", and defines the parameter type corresponding to the first parameter name as an integer (int) list, the parameter type corresponding to the second parameter name as a floating point number (float), and the parameter type corresponding to the third parameter name as a string (str). The first parameter information in the first request includes "data_list":[1,2,3], "threshold":5.5, "name":"John"", and the parameter verification model determines that the parameter types of all parameters in the first parameter information are correct.
[0103] In the above process, through the parameter verification model, when an API call request is received, the parameter type and other information can be automatically and timely verified. Compared with the current parameter verification method, the parameter verification model can ensure more accurate parameter verification when the request includes complex parameter types and there are multiple layers of nested objects.
[0104] When it is determined that the first parameter information is abnormal, internal environment feedback is performed and step S330 is executed.
[0105] When it is determined that there is no abnormality in the first parameter information, step S340 is executed.
[0106] S330: Determine a first error response, and send the first error response to the user equipment.
[0107] The first error response is used to indicate the first abnormal parameter information in the first parameter information, including one or more of a parameter type error, a parameter format error, or a parameter range error, which is not specifically limited in this application. Specifically, when the parameter "data_list" in the first request is abnormal, the first error response includes the first parameter name "data_list" and the specific error "type=type_error.list", indicating that the value of "data_list" is not a normal list, and it is determined that the parameter type of the first parameter is incorrect.
[0108] After determining the first error response, the first error response is sent to the user device in a timely manner, so that the user device can regenerate and send the first API call request according to the specific error information.
[0109] In the above process, the specific error information of the parameters is determined through parameter inspection, and the response information including the specific error parameter field and the error type is fed back to the user device, so that the user device can adjust the API call request in time and send the correct API call request in the subsequent process, thereby ensuring the accuracy and efficiency of the API call and avoiding the occurrence of repeated sending of requests containing erroneous content.
[0110] S340: Determine a first cache file corresponding to the stored first API according to the first request, and respond with a first result indicating whether the first cache file exists.
[0111] The first cache file is used to store response results of one or more first API calls and corresponding parameter information. The first response result is obtained by the first API according to the first parameter information. In a possible implementation, the first cache file also stores more types and quantities of data, for example, a timestamp indicating the creation or update of the cache file, etc., which is not specifically limited in this application.
[0112] When there is no abnormality in the parameters in the first request, the first cache file is determined according to the first API information in the first request and a pre-set first path rule, and whether there is a first response result in the first cache file is determined according to the first parameter information, wherein the first path rule is used to determine the storage address of the cache file in the system, which is represented according to the API type, API function, API name, etc. included in the API information.
[0113] If the first response result does not exist in the first cache file, step S350 is executed.
[0114] When the first response result exists in the first cache file, external environment feedback is performed and step S360 is executed.
[0115] S350: Determine a first API code according to the first request, and determine a response result according to the first parameter information and the first API code.
[0116] When the first response result does not exist in the first cache file, the first Python API file is determined according to the first API information and a pre-set second path rule, and the first API code is loaded into the current running environment by using a dynamic import mechanism so that the first API code can be called and executed, wherein the second path rule is used to indicate the storage address of the API code corresponding to the API in the system, which can be represented by the API type, API function and API name included in the API information, the first Python API file includes the first API code, and in a possible implementation, when the API code is Python code, the dynamic import mechanism can be Python's importlib module, which is not specifically limited in this application.
[0117] In a possible implementation, when the first API is an interface provided by a third-party service, a connection is established with a network device based on the first parameter information and the first API code, and a first request to call the first API is sent to the network device, wherein the network device may be a server deployed with a Rapid API platform, including multiple APIs provided by multiple different providers such as social and financial providers, the first API is any one of the multiple APIs, and the network device may also be other servers including APIs, which is not specifically limited in this application.
[0118] In one possible implementation, when a first return result determined by the network device based on the first API and the first parameter information is received, a second response result is determined based on the first return result, wherein the second response result is the same as the first response result, and the formats of the first return result and the second response result are different, and then step S360 is executed.
[0119] Compared with the current method in which user devices directly call the API in network devices, the above process calls the API through API code. The execution unit does not need to have an in-depth understanding of the underlying implementation details and complex calling methods of the API. Based on the unified interface code, the difficulty of use is reduced and the efficiency and accuracy of API calls are improved.
[0120] In another possible implementation, when the first return result is not received but a return failure message sent by the network device is received, response information to be sent to the user device is determined based on the return failure message, so that the user device sends the first request again based on the response information. For example, when the return failure message sent by the Rapid API is "Rate limit reached, request rejected", a response message of "Your requests are too frequent, please try again later" is sent to the user device; or, when the return failure message sent by the Rapid API is "Authorization is invalid and the API cannot be accessed", a response message of "There is a problem with your authorization, please check your account permissions or log in again" is sent to the user device.
[0121] When the return failure information indicates that the first API does not exist or the parameters included in the first parameter information do not exist, redetermine the second API code corresponding to the first API, and establish a connection with the network device based on the first parameter information and the second API code, repeat the above steps to receive the second return result sent by the network device, and determine the third response result based on the second return result, wherein the third response result is the same as the first response result, and the formats of the second return result and the third response result are different, and then execute step S360.
[0122] In the above process, when receiving the return failure information sent by the network device, the server starts the API information automatic collection program, re-acquires the information corresponding to the first API, generates and stores the corresponding new API code, and obtains the corresponding response result according to the new API code, which can ensure that the user device obtains the latest services and data, and each API remains accessible. Compared with the current method that relies on manual collection, management and update of API related information, it can significantly reduce labor costs, and compared with Figure 1 The API simulation method in the system shown can realize automatic expansion of API resources to ensure more accurate API call results.
[0123] In the above process, the process of re-determining the second API code corresponding to the first API can refer to Figure 4 The contents shown are not described in detail here.
[0124] In another possible implementation, when the first API is not an interface provided by a third-party service, the first API code is executed according to the first parameter information to determine a fourth response result, wherein the fourth response result is the same as the first response result, and then step S360 is executed.
[0125] The above method can be applied to the scenario of generating executable problem-solving code based on the content of the LeetCode website, which expands the application scope of this application and enables user devices to flexibly use the rich question bank provided by LeetCode to perform various programming exercises and algorithm verification. In the specific implementation process, the method in which the server directly determines the response result through the stored API code can avoid errors caused by network delays or network connection problems compared to the current method of obtaining the response result through network communication, improve execution efficiency, and perform data processing locally to obtain the response result, which can improve the stability and security of API calls.
[0126] In one possible implementation, when the first request is received, an execution time threshold is set for the first API execution task shown in step S350. When the first API execution task reaches the execution time threshold but is still not completed, the server takes certain actions, such as terminating the first API execution task, and sends a timeout response message to the corresponding user device.
[0127] The above method can prevent complex or abnormal API execution tasks from occupying system resources for a long time, thereby ensuring system performance and ensuring the normal calling of other APIs.
[0128] In one possible implementation, when step S350 is executed according to multiple API call requests, multiple API execution tasks are placed in a process pool so that the process pool can run multiple processes simultaneously to handle multiple API execution tasks. For example, when the process pool can run five processes simultaneously and there are currently ten parallel API execution tasks, the process pool processes the first five API execution tasks in parallel by running five processes. When one of the processes completes the corresponding API execution task and is released, the sixth API execution task is processed by this process.
[0129] The above method processes API execution tasks in parallel, so that API execution tasks that take a long time will not cause other API execution tasks to be blocked, which can improve the processing capacity of the system.
[0130] S360: Send the response result to the user device.
[0131] In a possible implementation, when the first response result exists, the first response result is directly sent to the user equipment.
[0132] In the above process, when the first request parameters are normal, the cache file is first searched to see whether the first response result exists. If the first response result exists, the first response result can be directly returned to the user device. This caching mechanism not only reduces the number and time of repeated calls to the external API, but also improves the system's throughput and response time. Especially in high-concurrency scenarios, the improvement in response speed and performance is more obvious.
[0133] In another possible implementation, when the first API is an interface provided by a third-party service, and the second response result is determined according to the first return result, the second response result is sent to the user device.
[0134] In another possible implementation, when the first API is an interface provided by a third-party service, and the third response result is determined according to the second return result, the third response result is sent to the user device.
[0135] In another possible implementation, when the first API is not an interface provided by a third-party service and the fourth response result is determined, the fourth response result is sent to the user device.
[0136] To sum up, an API calling method provided by an embodiment of the present application, when receiving a request to call an API, determines the type, format, range and other information of API-related parameters according to the stored API code of the target API, and determines a parameter verification model according to the parameter information, and promptly verifies the parameters in the request according to the parameter verification model. When there are errors in the parameters, the specific errors are determined in a timely and accurate manner, and relevant measures are taken, thereby avoiding the occurrence of API call failures caused by parameter errors in the request, and enabling the user device to determine the specific error information, take corresponding measures, and improve the accuracy and efficiency of API calls.
[0137] like Figure 4 As shown, Figure 4 is a flowchart of a method for determining a second API code provided in an embodiment of the present application, the method is applied to Figure 2 The API management system in the API calling system shown includes the following steps.
[0138] S410: Obtain web page information of a new first API.
[0139] The web page information is the HTML text corresponding to the web page including API information provided by the API platform. The HTML text is used to describe the structure and content of the web page, including tags, attributes, text and other contents. In a possible implementation mode, the web page information can also be represented in other ways, which is not specifically limited in this application.
[0140] Specifically, select and configure a WebDriver driver suitable for the target browser, and send a request to the target browser through WebDriver to open the target web page. For example, when the API platform is Rapid API, use WebDriver to automatically open the target web page (https: / / rapidapi.com / hub). In a possible implementation, when the API platform changes, the driver may change, which is not specifically limited in this application. Afterwards, use XML path language (XPath) selector or cascading style sheet (CSS) selector to locate the web page element to be clicked, and use WebDriver to simulate the user operation of clicking the web page element, etc., to achieve interaction with the web page, wherein the user operation can also include more types, such as scrolling the page, etc., which is not specifically limited in this application. When the web page content is loaded by simulating user operations, the HTML text of the current web page is obtained through WebDriver.
[0141] S420: Extracting first API document information from the web page information of the new first API according to the extraction model.
[0142] The extraction model is obtained by machine learning training. For example, the extraction model is obtained by using a pre-trained large language model and performing supervised fine-tuning based on a data set including API web page information and API document information.
[0143] Specifically, when the API platform is Rapid API, as shown in step S410, the HTML texts corresponding to the multiple APIs are obtained, and according to the pre-set rules, the API-related information is extracted from each HTML text, and the tuples of the multiple HTML texts and the corresponding API-related information are used to form a training set, wherein the API-related information includes the API name, return value, and parameter information, etc., which is not specifically limited in this application. After preprocessing the data in the training set and setting the learning rate, batch size, and training cycle, the model is trained, and the extraction model is determined by continuously adjusting the parameters of the large language model.
[0144] The first API document information includes at least one or more of parameter information, API name, and API return value. In a possible implementation, the first API document information may include more or fewer types and quantities of data, such as API type, etc. This application does not make any specific limitations on this.
[0145] Specifically, after obtaining the web page information of the new first API, find the HTML text of the new first API. <style>和<script>标签的起始和结束位置,并将整个标签及其内部的内容删除,这两个标签通常包括网页的样式和脚本代码,与API相关的信息无关。同时,由于标签的属性值通常不包括第一API相关的信息,仅有锚点标签()的href属性可能与API的链接相关,删除HTML文本中除href属性外的属性。
[0146] 在执行完上述操作对HTML文本进行数据清洗后,将新的第一API的HTML文本进行格式化,并输入抽取模型中,生成第一API文档信息。
[0147] 上述方法中,通过抽取模型从API对应的网页信息中提取API文档信息,可以在HTML文本结构复杂,或者变化的情况下,正常、准确地获取API文档信息,可以保证信息提取的连续性和稳定性,提升系统的鲁棒性和可靠性,并且上述过程不需要人工参与,可以减少人工成本。
[0148] S430:根据新的第一API文档信息,确定第二API代码。
[0149] 新的第一API文档信息中包括的API名称确定函数名,根据参数信息确定参数定义,根据参数信息、API名称以及API返回值确定函数体以及文档字符串,其中,参数定义用于指示函数的输入变量,函数体用于指示计算或者操作逻辑,文档字符串用于描述函数功能、参数和返回值,之后根据函数名、参数定义、函数体以及文档字符串确定第二API代码。
[0150] 在一种可能的实施方式中,第二API代码可以是Python代码,或者可以以其他编程语言进行表示,对此本申请不作具体限定。
[0151] 在一种可能的实施方式中,图4所示的方法可以在用户设备发送API调用请求前执行,以确定第一API代码等多个不同的API代码,完成多来源API的集合,在确定其他API代码的过程中,图4中的步骤S410~S430中的内容可能存在适应性修改,例如获取初始第一API的网页信息等,对此本申请不作具体限定。
[0152] 综上所述,本申请实施例提供的一种确定第二API代码的方法,通过自动获取最新的API网页信息,通过抽取模型,以及最新的API网页信息,生成统一格式的API文档信息,并根据API文档信息生成各自对应的API代码,可以自动集成并更新Rapid API、Leetcode等多个平台的API资源,使用户能够灵活访问和管理多种数据源,大幅拓展了应用场景,支持广泛的业务需求并为未来功能扩展提供了基础,提升了应用开发效率,保证用户设备获得最新的服务和数据。通过生成API代码,可以将提取的API文档信息转换为统一的接口代码,便于API调用,同时,上述API集成过程不需要人工参与,可以减少人工成本。
[0153] 在一种可能的实施方式中,图4所示的方法可以应用于智慧城市的扩展。由于智慧城市涉及交通、能源、环境监测、安防、公共服务等多个领域的数据,通过上述图4所示的方法,智慧城市管理平台可以集成上述多领域各自提供的API,并按照一定的时间间隔,或者监测机制进行自动化更新。上述过程可以在没有人工参与的情况下,实现大规模API的集成和API的生命周期管理,保障了智慧城市的扩展性,从而适应城市的快速发展和变化的需求,提升了智慧城市系统的灵活性和响应能力。
[0154] 在一种可能的实施方式中,图4所示的方法还可以应用于其他需要大规模工具集成,以及自动化管理、更新的场景中,对此本申请不作具体限定。
[0155] 如图5所示,图5是本申请实施例提供的一种计算设备的结构示意图,计算设备用于部署图2所示的API调用系统,实现图4所示的API调用方法。该计算设备500至少包括总线510、处理器520、存储器530和通信接口540。处理器520、存储器530和通信接口540之间通过总线510通信。应理解,本申请不限定计算设备500中的处理器、存储器的个数。
[0156] 总线510可以是外设部件互连标准(peripheral component interconnect,PCI)总线或扩展工业标准结构(extended industry standard architecture,EISA)总线等。总线可以分为地址总线、数据总线、控制总线等。为便于表示,图5中仅用一条线表示,但并不表示仅有一根总线或一种类型的总线。总线510可包括在计算设备500各个部件(例如,处理器520、存储器530、通信接口540)之间传送信息的通路。
[0157] 处理器520可以包括中央处理器(central processing unit,CPU)、微处理器(micro processor,MP)或者数字信号处理器(digital signal processor,DSP)等处理器中的任意一种或多种,用于执行程序代码。
[0158] 存储器530可以包括易失性存储器(volatile memory),例如随机存取存储器(random access memory,RAM)。存储器530还可以包括非易失性存储器(non-volatilememory),例如只读存储器(read-only memory,ROM),快闪存储器,机械硬盘(hard diskdrive,HDD)或固态硬盘(solid state drive,SSD),存储器还可以包括上述种类的组合。存储器530上存有可执行的程序代码,在存储器上存有接收单元2201~执行单元2210中一个或者多个单元对应的可执行的程序代码,处理器520执行可执行的程序代码可以分别实现前述接收单元2201~执行单元2210的功能,从而实现图3所示的API调用方法以及图4所示的API集成方法,也即,存储器530上存有用于执行API调用方法和API集成方法的指令。另外,存储器530上还可以存有更多种类及数量的数据,例如,多个API对应的Python代码文件、多个包括API调用的响应结果的缓存文件等,对此本申请不作具体限定。
[0159] 通信接口540使用例如但不限于网络接口卡、收发器一类的收发模块,来实现计算设备500与其他设备或通信网络之间的通信。
[0160] 需要说明的,图5仅仅是本申请实施例的一种可能的实现方式,实际应用中,计算设备还可以包括更多或更少的部件,本申请对此不作具体限定。
[0161] 如图6所示,图6是本申请实施例提供的一种计算设备集群的结构示意图,该计算设备集群包括至少一台计算设备,该计算设备可以是服务器,例如是中心服务器、边缘服务器,或者是本地数据中心中的本地服务器。在一些实施例中,计算设备也可以是台式机、笔记本电脑或者智能手机等终端设备。计算设备集群中的一个或多个计算设备500中的存储器530中可以存有相同的用于执行本申请提供的API调用方法的指令。
[0162] 在一些可能的实现方式中,该计算设备集群中的一个或多个计算设备500的存储器530中也可以分别存有用于执行API调用方法的部分指令。换言之,一个或多个计算设备500的组合可以共同执行用于执行API调用方法的指令。
[0163] 需要说明的是,计算设备集群中的不同的计算设备500中的存储器530可以存储不同的指令,分别用于执行多个单元的部分功能。也即,不同的计算设备500中的存储器530存储的指令可以实现接收单元2201~执行单元2210中的一个或多个单元的功能。
[0164] 在一些可能的实现方式中,计算设备集群中的一个或多个计算设备可以通过网络连接。其中,所述网络可以是广域网或局域网等等。图7是本申请实施例提供的一种一个或多个计算设备通过网络连接的结构示意图。
[0165] 如图7所示,计算设备500A包括总线510A、处理器520A、存储器530A和通信接口540A,计算设备500B包括总线510B、处理器520B、存储器530B和通信接口540B。两个计算设备500A和500B之间通过网络进行连接。具体地,通过各个计算设备中的通信接口与网络进行连接。在这一类可能的实现方式中,计算设备500A中的存储器530A中存有执行采集单元2204、信息抽取单元2205、代码生成单元2206以及训练单元2207的功能的指令。同时,计算设备500B中的存储器530B中存有执行接收单元2201、参数验证单元2202、错误处理单元2203、缓存单元2208、发送单元2209以及执行单元2210的功能的指令。计算设备500A和计算设备500B通过数据交互,可以实现本申请提供的API调用方法。
[0166] 本申请实施例,还提供一种计算机可读存储介质,计算机可读存储介质中存储有指令,当其在处理器上运行时,图3所示的API调用方法和图4所示的API集成方法的流程得以实现。
[0167] 本申请实施例,还提供一种计算机程序产品,当计算机程序产品在处理器上运行时,图3所示的API调用方法和图4所示的API集成方法流程得以实现。
[0168] 在计算机上加载或执行计算机程序指令时,全部或部分地产生按照本申请实施例的流程或功能。计算机可以为通用计算机、专用计算机、计算机网络、或者其他可编程装置。
[0169] 计算机指令可以存储在计算机可读存储介质中,或者从一个计算机可读存储介质向另一个计算机可读存储介质传输,例如,计算机指令可以从一个网站站点、计算机、服务器或数据中心通过有线(例如同轴电缆、光纤、数字用户线(digital subscriber line,DSL))或无线(例如红外、无线、微波等)方式向另一个网站站点、计算机、服务器或数据中心进行传输。计算机可读存储介质可以是计算机能够存取的任何可用介质或者是包含至少一个可用介质集合的服务器、数据中心等数据存储节点。可用介质可以是磁性介质(例如,软盘、硬盘、磁带)、光介质(例如,高密度数字视频光盘(digital video disc, DVD)、或者半导体介质)。
[0170] 最后应说明的是:以上实施例仅用以说明本申请的技术方案,而非对其限制;尽管参照前述实施例对本申请进行了详细的说明,本领域的普通技术人员应当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本申请各实施例技术方案的保护范围。< / style>
Claims
1. An API calling method, characterized in that: The method comprises: Receive a first request sent by a user device, where the first request is used to call a first application programming interface API; Verifying first parameter information included in the first request according to a parameter verification model, wherein the parameter verification model is determined according to parameter information extracted from a first API code corresponding to the stored first API, the parameter information including at least one parameter name, and one or more of a parameter type, a parameter format, and a parameter range corresponding to each parameter name in the at least one parameter name; When it is determined that there is an abnormality in the first parameter information, a first error response is sent to the user equipment, where the first error response is used to indicate first abnormal parameter information in the first parameter information, including one or more of a parameter type error, a parameter format error, or a parameter range error.
2. The method according to claim 1, characterized in that The method further comprises: When it is determined that there is no abnormality in the first parameter information, determining a first cache file corresponding to the first API stored according to the first API information included in the first request, wherein the first API information includes one or more of a platform, a category, a tool name, an API name, and a calling parameter; Determine, according to the first parameter information, whether there is a first response result in the first cache file, where the first response result is obtained by processing the first API according to the first parameter information; If the first response result exists in the first cache file, the first response result is sent to the user equipment.
3. The method according to claim 2, characterized in that The method further comprises: If the first response result does not exist in the first cache file, determining a first API code corresponding to the stored first API according to the first API information; In a case where the first API is an interface provided by a third-party service, establishing a connection with a network device according to the first parameter information and the first API code, the network device including a plurality of APIs, the first API being any one of the plurality of APIs; Upon receiving a first return result determined by the network device based on the first API and the first parameter information, a second response result is determined based on the first return result, and the second response result is sent to the user device, wherein the second response result is the same as the first response result, and the formats of the first return result and the second response result are different.
4. The method according to claim 3, characterized in that The method further comprises: In the case where the first return result is not received but return failure information sent by the network device is received, re-determining a second API code corresponding to the first API, wherein the return failure information is used to indicate that the first API does not exist or the parameters included in the first parameter information do not exist; In a case where the first API is an interface provided by a third-party service, establishing a connection with the network device according to the first parameter information and the second API code; Upon receiving a second return result determined by the network device based on the first API and the first parameter information, a third response result is determined based on the second return result, and the third response result is sent to the user device, wherein the third response result is the same as the first response result, and the second return result is different from the third response result in format.
5. The method according to claim 4, characterized in that The re-determining the second API code corresponding to the first API includes: Obtaining web page information of the first API; Extracting first API document information from the web page information of the first API according to an extraction model, wherein the extraction model is obtained through machine learning training, and the first API document information includes at least one or more of the second parameter information, the first API name, and the first API return value; Determine a function name according to the first API name, determine a parameter definition according to the second parameter information, and determine a function body and a document string according to the second parameter information, the first API name, and the first API return value, wherein the parameter definition is used to indicate an input variable of the function, the function body is used to indicate a calculation or operation logic, and the document string is used to describe the function function, parameters, and return value; The second API code is determined according to the function name, the parameter definition, the function body, and the document string.
6. The method according to any one of claims 3 to 5, characterized in that: The method further comprises: In the case where the first API is not an interface provided by a third-party service, the first API code is executed according to the first parameter information, a fourth response result is determined, and the fourth response result is sent to the user device, and the fourth response result is the same as the first response result.
7. An API calling device, characterized in that: The device comprises: A receiving unit, configured to receive a first request sent by a user equipment, wherein the first request is used to call a first application programming interface API; a parameter verification unit, configured to verify first parameter information included in the first request according to a parameter verification model, wherein the parameter verification model is determined according to parameter information extracted from a first API code corresponding to the stored first API, the parameter information including at least one parameter name, and one or more of a parameter type, a parameter format, and a parameter range corresponding to each parameter name in the at least one parameter name; an error handling unit, configured to send a first error response to the user equipment when it is determined that the first parameter information is abnormal, wherein the first error response is used to indicate first abnormal parameter information in the first parameter information, including one or more of a parameter type error, a parameter format error, or a parameter range error.
8. The device according to claim 7, characterized in that The device also includes a cache unit and a sending unit: The cache unit is used to determine, when it is determined that there is no abnormality in the first parameter information, a first cache file corresponding to the first API in the cache unit according to the first API information included in the first request, wherein the first API information includes one or more of a platform, a category, a tool name, an API name, and a calling parameter; further configured to determine, according to the first parameter information, whether there is a first response result in the first cache file, the first response result being obtained by the first API processing according to the first parameter information; The sending unit is further configured to send the first response result to the user equipment if the first response result exists in the first cache file.
9. The device according to claim 8, characterized in that The device also includes an execution unit: The execution unit is configured to determine, if the first response result does not exist in the first cache file, a first API code corresponding to the stored first API according to the first API information; Also used for establishing a connection with a network device according to the first parameter information and the first API code when the first API is an interface provided by a third-party service, the network device including a plurality of APIs, the first API being any one of the plurality of APIs; The sending unit is also used to determine a second response result based on the first return result determined by the network device according to the first API and the first parameter information when the receiving unit receives the first return result, and send the second response result to the user device, wherein the second response result is the same as the first response result, and the formats of the first return result and the second response result are different.
10. The device according to claim 9, characterized in that The device also includes a code generation unit: The code generating unit is configured to re-determine a second API code corresponding to the first API when the receiving unit does not receive the first return result but receives return failure information sent by the network device, wherein the return failure information is used to indicate that the first API does not exist or the parameters included in the first parameter information do not exist; The execution unit is further configured to establish a connection with the network device according to the first parameter information and the second API code when the first API is an interface provided by a third-party service; It is also used to determine a third response result based on the second return result when receiving a second return result determined by the network device based on the first API and the first parameter information, and send the third response result to the user device, wherein the third response result is the same as the first response result, and the second return result is different from the third response result in format.
11. The device according to claim 10, characterized in that The device also includes a collection unit, an information extraction unit and a training unit: The acquisition unit is used to obtain web page information of the first API; The information extraction unit is used to extract first API document information from the web page information of the first API according to an extraction model, wherein the extraction model is obtained by the training unit through machine learning training, and the first API document information at least includes one or more of the second parameter information, the first API name, and the first API return value; The code generation unit is further used to determine a function name according to the first API name, determine a parameter definition according to the second parameter information, and determine a function body and a document string according to the second parameter information, the first API name, and the first API return value, wherein the parameter definition is used to indicate an input variable of the function, the function body is used to indicate a calculation or operation logic, and the document string is used to describe the function function, parameters, and return value; The code generation unit is further used to determine the second API code according to the function name, the parameter definition, the function body and the document string.
12. The device according to any one of claims 9 to 11, characterized in that: The execution unit is also used for: In a case where the first API is not an interface provided by a third-party service, executing the first API code according to the first parameter information, and determining a fourth response result, the fourth response result being the same as the first response result; The sending unit is configured to send the fourth response result to the user equipment.
13. A computing device cluster, characterized in that: The method comprises at least one computing device, wherein the computing device comprises a processor and a memory, wherein the processor is configured to execute instructions stored in the memory, so that the computing device executes the method according to any one of claims 1 to 6.
14. A computer program product comprising instructions, characterized in that When the instructions are executed by a computing device, the computing device is enabled to implement the method according to any one of claims 1 to 6.
15. A computer-readable storage medium, characterized in that: The method comprises computer program instructions, and when the computer program instructions are executed by a computing device, the computing device is caused to implement the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Data processing method and device and computer readable storage medium
CN114003647A
Interface document generation method and device, computer equipment and storage medium
CN114168190A
Service data processing method and device based on aggregation interface, medium and equipment
CN116109112A
API (Application Program Interface) input verification test method, API input verification test device and electronic equipment
CN116820845A
Application programming interface API management method and device, equipment and medium
CN118276903A
Cited By
Dynamic asynchronous calling method for rate-limited API (Application Program Interface)
CN121116663A
A dynamic asynchronous calling method for a rate-limited API
CN121116663B