Message generation method and device
By obtaining and processing the request and response configuration information of the network interface, a unified format message is generated, which solves the problem of non-uniform message format of the network interface and improves development efficiency and user experience.
Patent Information
- Application Number
- CN202410674933.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-29
- Publication Date
- 2024-09-24
AI Technical Summary
In the prior art, the message formats generated by each network interface are not uniform, resulting in different backend network interface message formats for different services in the same front-end application, which increases development complexity and application difficulty.
By obtaining the request configuration information of the target interface, the interface request is generated, and preprocessed based on the initial response message to generate a target message in a predetermined format. The request and response interceptor processing functions are used to process the configuration items and data to ensure that the message format is correct. consistency.
It realizes the unification of message formats between network interfaces on different platforms, reduces the difficulty of development, improves the user experience and development efficiency of applications, reduces repeated message processing codes, and improves loading speed.
Smart Images

Figure CN118694830A
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of data processing, and is a message generating method and device. Background Art
[0002] With the development of Internet applications, almost all Internet applications need to receive data from the service background through network interface requests. Taking the most commonly used Http protocol network interface in current Internet application development as an example, since the Http protocol is highly configurable, the network interface provider of the Http protocol can flexibly define or configure the format and data of the interface's sent and returned messages when designing the interface. When using the interface, the user needs to follow the defined interface rules and requirements, splice the request to send data, receive the interface response return message data and process it strictly according to the returned data format.
[0003] As Internet applications become more powerful and complex, this also means that most Internet applications cannot avoid connecting to multiple backend services through network interfaces. When submitting or receiving data through the network interfaces provided by multiple different backend services, since the network interface provider can flexibly define or configure the interface's send and return messages when designing the interface, and a complex application often has many modules, and each module is connected to one or more interfaces, different modules and interfaces are often designed and developed by different developers. Due to differences in each developer's technical level, design concept, and business understanding, there will inevitably be differences when designing external service interfaces. Therefore, the backend network interface message formats corresponding to different services in the same front-end application may be different, and even the message formats of several backend network interfaces corresponding to the same service in the same front-end application may be different. Specifically, Figure 1 As shown, for the same front-end application / front-end business, A service background interface 1, B service background interface 1 and C service background interface 1, because the network interface corresponding to the service background is used for processing and encapsulation, the message formats generated by the three interfaces are different.
[0004] Therefore, a message generation method is urgently needed to solve the problem of inconsistency in the message formats generated by various network interfaces in the prior art. Summary of the invention
[0005] In view of this, the present invention provides a message generation method and device, the main purpose of which is to solve the problem of inconsistent message formats generated by various network interfaces.
[0006] To solve the above problems, the present application provides a message generation method, including.
[0007] In response to an access request operation for a target interface, obtaining request configuration information of the target interface;
[0008] Based on the request configuration information, using a predetermined request generation method, an interface request for accessing a target interface is generated, and key configuration information is obtained from the interface request configuration information;
[0009] Sending the interface request to the target interface, and receiving an initial response message returned by the target interface in response to the interface request;
[0010] Preprocessing the initial response message based on the key configuration information to obtain a current message;
[0011] A target message in a predetermined format is generated based on the current message and a predetermined message generation method.
[0012] Optionally, based on the interface request configuration information, generating an interface request for accessing a target interface by using a predetermined request processing method specifically includes:
[0013] Pre-configuring a request interceptor processing function based on a predetermined request template, or pre-configuring a request interceptor processing function based on a predetermined target request processing function;
[0014] Based on the request template or the target request processing function in the request interceptor processing function, the interface request configuration information is processed to generate the interface request.
[0015] Optionally, the processing of the interface request configuration information based on the request template or target request function in the request interceptor processing function to generate the interface request specifically includes:
[0016] Based on the pre-configured request interceptor processing function, according to each target configuration item in the request template, the target request configuration information corresponding to each target configuration item is determined from the request configuration information;
[0017] Processing each of the target request configuration information according to the processing mode corresponding to each target configuration item in the request template to generate the interface request;
[0018] Alternatively, obtain the request parameter option of the target request processing function;
[0019] The target request processing function is called from the request interceptor processing function, and the request configuration information and the request parameter option are processed to generate the interface request.
[0020] Optionally, the request configuration information includes any one or more of the following: user name, password, request header, request body information, network interface basic path, network interface uniform resource locator URL, message encryption method, request method;
[0021] Before configuring the request interceptor processing function, the method further includes creating the request template, specifically including:
[0022] Configure the first constructor;
[0023] Based on the first constructor, in response to the user's adding operation on the target configuration item, and in response to the user's adding operation on the processing mode of each target configuration item, the request template is created.
[0024] Optionally, the processing of each target request configuration information according to the processing mode corresponding to each target configuration item in the request template to generate the interface request specifically includes:
[0025] For a target configuration item of a method type, executing a target method in the target request configuration information to obtain execution information;
[0026] For the target configuration item of the path type, access path concatenation is performed to obtain a concatenated path;
[0027] For the target configuration item of the request parameter type, verify the request parameters in the target request configuration information to obtain verification information;
[0028] For the target configuration item of the request header information type, perform interface request header merging processing to obtain request header merging information;
[0029] For the target configuration item of the encryption requirement type, the encryption method of the request message is obtained from the target request configuration information item;
[0030] An interface request is generated based on the execution information, the splicing path, the verification information, the request header merging information and the encryption method.
[0031] Optionally, the key configuration information includes encryption method:
[0032] The preprocessing of the initial response message based on the key configuration information to obtain the current message specifically includes:
[0033] Determine a decryption method based on the encryption method;
[0034] The initial message is decrypted based on the decryption method to obtain the current message.
[0035] Optionally, the generating a target message in a predetermined format based on the current message and a predetermined message generation method specifically includes:
[0036] Pre-configuring a response interceptor processing function based on a predetermined message template, or pre-configuring a response interceptor processing function based on a predetermined target response processing function;
[0037] Based on the message template or the target response processing function in the response interceptor processing function, the current message is processed to generate the target message.
[0038] Optionally, the processing the current message based on the message template or the target response processing function in the response interceptor processing function to generate the target message specifically includes:
[0039] Based on the pre-configured response interceptor processing function, according to each target data item in the message template, determine the target response data corresponding to each target data item from the current message;
[0040] According to the data format corresponding to each target data item in the request template, format conversion processing is performed on each target response data to generate the target message;
[0041] Alternatively, obtaining the response parameter respOption of the target response processing function;
[0042] The target response processing function is called from the response interceptor processing function, and processing is performed based on each response data and response parameter respOption in the current message to obtain the target message.
[0043] Optionally, after the target interface initiates the interface request, the method further includes:
[0044] Monitoring whether the interface request initiated by the target interface times out, and monitoring whether the target interface receives an error code;
[0045] When the interface request times out, or the interface receives an error code, the target message is generated based on predetermined error information.
[0046] In order to solve the above problems, the present application provides a message generating device, comprising:
[0047] An acquisition module, used for acquiring the requested configuration information of the target interface in response to the access request operation for the target interface;
[0048] A request generation module, used to generate an interface request for accessing a target interface based on the request configuration information and using a predetermined request generation method, and to obtain key configuration information from the interface request configuration information;
[0049] A receiving module, used for sending the interface request to the target interface, and receiving an initial response message returned by the target interface for the interface request;
[0050] A preprocessing module, used to preprocess the initial response message based on the key configuration information to obtain a current message;
[0051] The message generation module is used to generate a target message in a predetermined format based on the current message and a predetermined message generation method.
[0052] The message generation method and device in the present application can be connected to network interfaces of different platforms. Through a predetermined request generation method and a predetermined message generation method, a request data / interface request of a predetermined format can be generated based on the request generation method, and an interface request can be initiated. When the target network interface returns a response message, a target message of a unified format can be generated according to the predetermined message generation method, solving the problem of inconsistency in the message format generated by each network interface.
[0053] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0054] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present invention. Moreover, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0055] Figure 1 A schematic diagram of the relationship between the front-end application and the network interfaces in the back-end service according to an embodiment of the present application;
[0056] Figure 2 This is a flowchart of a message generation method according to another embodiment of the present application;
[0057] Figure 3 This is a processing flow chart of a request interceptor processing function according to another embodiment of the present application;
[0058] Figure 4 This is a processing flow chart of a response interceptor processing function according to another embodiment of the present application;
[0059] Figure 5 This is a structural block diagram of a message generating device according to another embodiment of the present application. DETAILED DESCRIPTION
[0060] Various aspects and features of the present application are described herein with reference to the accompanying drawings.
[0061] It should be understood that various modifications may be made to the embodiments of the present application. Therefore, the above description should not be considered as limiting, but only as an example of an embodiment. Other modifications within the scope and spirit of the present application will occur to those skilled in the art.
[0062] The accompanying drawings, which are incorporated in and constitute a part of the specification, illustrate embodiments of the present application and, together with the general description of the present application given above and the detailed description of the embodiments given below, serve to explain the principles of the present application.
[0063] These and other characteristics of the present application will become apparent from the following description of a preferred form of embodiment given as a non-limiting example with reference to the accompanying drawings.
[0064] It should also be understood that although the present application has been described with reference to some specific examples, those skilled in the art will be able to readily implement many other equivalent forms of the present application.
[0065] The above and other aspects, features and advantages of the present application will become more apparent in view of the following detailed description when taken in conjunction with the accompanying drawings.
[0066] Specific embodiments of the present application are described hereinafter with reference to the accompanying drawings; however, it should be understood that the embodiments applied for are merely examples of the present application, which may be implemented in a variety of ways. Well-known and / or repeated functions and structures are not described in detail to avoid unnecessary or redundant details that obscure the present application. Therefore, the specific structural and functional details applied for herein are not intended to be limiting, but merely serve as a basis and representative basis for the claims to teach those skilled in the art to use the present application in a variety of ways with substantially any suitable detailed structure.
[0067] This specification may use the phrases "in one embodiment," "in another embodiment," "in yet another embodiment," or "in other embodiments," all of which may refer to one or more of the same or different embodiments according to the present application.
[0068] The present application embodiment provides a message generation method, which can be specifically used for various Http network interfaces, such as Figure 2 As shown, the method in this embodiment includes the following steps:
[0069] Step S101, in response to an access request operation for a target interface, obtaining request configuration information of the target interface;
[0070] In this step, the interface request configuration based on the http protocol will basically include any one or several of the following configuration items: request header, request body information, network interface basic path, network interface uniform resource locator url, message encryption method, request method, and of course, other configuration items customized by users / developers.
[0071] Step S102, based on the request configuration information, using a predetermined request generation method, generating an interface request for accessing the target interface, and obtaining key configuration information from the interface request configuration information;
[0072] In this step, an interface request of a predetermined format may be generated by a predetermined method such as a request template or a request processing function. Specifically, a request interceptor processing function interceptorRequest may be pre-configured based on a predetermined request template, or a request interceptor processing function interceptorRequest may be pre-configured based on a predetermined target request processing function; then, based on the request template or target request processing function in the request interceptor processing function interceptorRequest, the interface request configuration information is processed to generate the interface request.
[0073] Step S103, sending the interface request to the target interface, and receiving an initial response message returned by the target interface in response to the interface request;
[0074] Step S104, pre-processing the initial response message based on the key configuration information to obtain a current message;
[0075] In this step, the key configuration information includes encryption mode, compression mode, etc. Therefore, after receiving the initial response message, the decryption mode can be determined based on the encryption mode in the key configuration information, or the decompression mode can be determined based on the compression mode of the key configuration information; then the initial message is decrypted / decrypted based on the decryption mode or decompression mode to obtain the current message.
[0076] Step S105: generating a target message in a predetermined format based on the current message and a predetermined message generation method.
[0077] In this step, the response interceptor processing function may be pre-configured based on a predetermined message template, or the response interceptor processing function may be pre-configured based on a predetermined target response processing function;
[0078] Based on the message template or the target response processing function in the response interceptor processing function, the current message is processed to generate the target message.
[0079] The method in the present application can connect to network interfaces of different platforms. Through a predetermined request generation method and a predetermined message generation method, a request data / interface request of a predetermined format can be generated based on the request generation method, and an interface request can be initiated. When the target network interface returns a response message, a target message of a unified format can be generated according to the predetermined message generation method, solving the problem of inconsistency in the message format generated by each network interface.
[0080] On the basis of the above-mentioned embodiments, another embodiment of the present application provides a message generation method. In the specific implementation process of this embodiment, in response to the access request operation for the target interface, before obtaining the request configuration information of the target interface, the request interceptor processing function can be pre-configured. The specific process is: configure the first constructor; based on the first constructor, respond to the user's addition operation for the target configuration item, and respond to the user's addition operation for the processing method of each target configuration item to create the request template, so as to configure the target request processing function based on the request template to configure the request interceptor processing function interceptorRequest; or it can also respond to the user's addition operation for the target request processing function to configure the request interceptor processing function based on the target request processing function.
[0081] In this embodiment, after configuring the request interceptor processing function interceptorRequest, when an interface request needs to be generated, the following two methods can be used to generate the interface request:
[0082] Method 1: Based on a pre-configured request interceptor processing function, according to each target configuration item in the request template, determine the target request configuration information corresponding to each target configuration item from the request configuration information; according to the processing method corresponding to each target configuration item in the request template, process each target request configuration information to generate the interface request.
[0083] Method 2: obtaining the parameter option of the target request processing function; calling the target request processing function from the request interceptor processing function, processing the request configuration information and the parameter option to generate the interface request.
[0084] That is, after configuring the request interceptor processing function interceptorRequest, the request interceptor processing function interceptorRequest can receive parameters: the target request configuration information config corresponding to each target configuration item in the request template, the custom interception processing function / target request processing function handler, and the parameter option of the target request processing function handler.
[0085] If the request interceptor processing function interceptorRequest contains the target request processing function handler, then method 2 can be used to generate an interface request, that is, the request interceptor will directly call the handler, and pass the configuration parameter config and the handler parameter option as the actual parameters of the target request processing function handler to execute, obtain the configuration file processed by the target request processing function handler and generate an interface request.
[0086] If the developer does not pass in the target request processing function handler, the request interceptor processing function interceptorRequest will use method 1 to generate the interface request and configuration file by processing and merging. Generally speaking, the interface request configuration based on the http protocol will basically include the network interface basic path, interface url, interface request message, interface request header information and custom configuration information. After receiving the configuration parameters, you can Figure 3 As shown, the request interceptor processing function interceptorRequest can be processed as follows:
[0087] (1) for a target configuration item of a method type, executing a target method in the target request configuration information to obtain execution information;
[0088] In this step, the configuration items of the target request configuration information / configuration parameter config may be screened. If the configuration item contains a method type, the configuration item method is executed first, and the configuration information after execution is returned.
[0089] (2) For the target configuration item of the path type, access path splicing processing is performed to obtain a spliced path;
[0090] In this step, the complete request path of the network service interface can be spliced. After receiving the target request configuration information / configuration parameter config, the complete request path of the network interface is spliced first, and the host host, base path baseUrl and interface path apiPath in the configuration are spliced. If there is no corresponding parameter in the target request configuration information / configuration parameter config, the default configuration parameters can be used for splicing.
[0091] (3) for a target configuration item of a request parameter type, verify the request parameters in the target request configuration information to obtain verification information;
[0092] In this step, you can verify the request parameters, traverse the parameters sent by the interface, and remove the data with empty values.
[0093] (4) performing interface request header merging processing on the target configuration item of the request header information type to obtain request header merging information;
[0094] In this step, you can merge the interface request header information, merge it with the header and headers configuration items in the config configuration, and then merge it with the default configuration defaultConfig. When the attribute names are the same, replace the default configuration value based on the received configuration value.
[0095] (5) generating an interface request based on the execution information, the splicing path, the verification information, and the request header merging information;
[0096] (6) For a target configuration item of the encryption requirement type, determine the decryption method of the message based on the encryption method in the target request configuration information item.
[0097] In this step, you can process the custom configuration in the config configuration, such as whether to encrypt the message data requested by the network interface, whether to display the default loading prompt when initiating an interface request, and other operations based on the custom configuration in the config configuration.
[0098] In this embodiment, after completing the above process, the processed configuration config can be stored in the Http class variable config and returned.
[0099] In this embodiment, the interface request of the same format can be generated by adopting the above method, which facilitates the subsequent return of an initial response message containing predetermined response content for the interface request, thereby facilitating the generation of a target message of the same format based on the initial response message.
[0100] On the basis of the above-mentioned embodiment, another embodiment of the present application provides a message generation method. In the specific implementation process of this embodiment, before responding to the access request operation for the target interface and obtaining the request configuration information of the target interface, the response interceptor processing function interceptorResponse can also be pre-configured. The specific process is: in response to the user's addition operation for the target data item, and in response to the user's addition operation for the data format of each target data item, a message template template is created based on each target data item and the data format corresponding to each target data item, and the response interceptor processing function interceptorResponse is configured based on the message template; or it can also respond to the user's addition operation for the target response processing function to configure the response interceptor processing function interceptorResponse based on the target response processing function. In this embodiment, the message template / template data format template refers to the message format returned by the service background network interface, which is a literal object format and can include multiple attributes, such as the following four attributes: (1) The data format contentType returned by the network interface response message: It can support the parsing of two data formats JSON and XML, and the default value is JSON. (2) Business processing status code code returned by the network interface response message: the attribute value is a user-defined literal object. (3) Data format data returned by the network interface response message. (4) Business processing error prompt message msg returned by the network interface response message. In this implementation, the message template can be created by defining a unified response data tool class. The specific process is: define a unified response data tool class Response, add a constructor, and the constructor receives a resp object. The resp object can be pre-set to contain multiple attributes, for example, 4 attributes, the interface return status code code, the interface return data data, the error message msg for developers to debug, and the error prompt message notice for users. Response also has a default processing function handler for business data. The handler function receives two parameters, the response data processing method callback cb defined by the developer / user, and the callback method parameter option. If the cb callback method is not passed in when calling the handler function, the handler function will perform default processing, including matching the status code and returning a boolean result. If it is correct, it returns true. If it is wrong, it will perform default pop-up window display processing and return false.
[0101] In this embodiment, after the response interceptor processing function interceptorResponse is configured, the response interceptor processing function interceptorResponse can receive parameters: target response data corresponding to each target data item in the message template, the customized business data processing function dataHandler, the target response processing function / response interception processing function respHandler, and the parameter respOption of the processing method handler. Specifically, the response interceptor processing function interceptorResponse can generate a target message of a predetermined format in the following two ways. Method 1: Based on the pre-configured response interceptor processing function, according to each target data item in the message template, the target response data corresponding to each target data item is determined from the current message; according to the data format corresponding to each target data item in the request template, each target response data is format converted to generate the target message; Method 2: Obtain the parameter respOption of the target response processing function; Call the target response processing function from the response interceptor processing function, and process based on each response data in the current message and the parameter option to obtain the target message.
[0102] That is, if the request interceptor processing function interceptorRequest contains the target response processing function / response interception processing function respHandler, then the response interceptor / response interceptor processing function interceptorResponse will directly execute the respHandler function. During execution, the parameter respOption will be passed in as the actual parameter of respHandler, and the data data returned by the interface and the key configuration information config after the request interceptor processing will be passed in to the handle function for execution. That is, the response interceptor processing function interceptorResponse can pre-process the initial message received by the interface based on the key configuration information config, such as decrypting the message based on the decryption method in the key configuration information to obtain the current message, and then pass each response data data in the current message into the handle function to generate a target message in a predetermined format.
[0103] If the request interceptor processing function interceptorRequest does not contain the respHandler function, then after receiving the key configuration information, the response interceptor processing function interceptorResponse will perform the following processing:
[0104] Based on the pre-configured response interceptor processing function, according to each target data item in the message template, the target response data corresponding to each target data item is determined from the current message; according to the data format corresponding to each target data item in the request template, the format conversion processing is performed on each target response data to generate the target message. That is, after correctly receiving the return message of the network interface, the message is parsed. It should be noted that the correct reception of the interface return message here refers to the successful return of the response message by the interface after the Http network interface request is initiated, but this does not mean the success of the business processing of the interface. After receiving the interface response message, according to the request configuration / key configuration information config, pre-processing is first performed, such as message decryption processing, so as to obtain the current message. Then, when executing the template parsing function findRespData, the format conversion processing of the message template and the response message data is performed. By using the template parsing function findRespData, the parameters: template data format / message template template and the current response message respData can be received. The method mainly parses the current response message respData according to the predetermined message template template, obtains each target data item from the current response message, and then converts the data content of each target data item into a predetermined format. For example, the data content corresponding to the four target data items of status code code, business data data, error information msg, and user-friendly prompt notice is obtained, and then the data content can be converted based on the format corresponding to each data item. For example, the data format of the status code code is 0 or 1, where 0 indicates that the interface business processing fails, and 1 indicates that the interface business processing succeeds. Therefore, the interface business processing failure can be determined according to the data R corresponding to the status code, and thus R can be converted to 0. Specifically, the attribute array of the template data format template object can be obtained, and the array can be traversed to obtain the key representing the status code in the response message, that is, key=0 or key=1, and the attribute value of the key in the template (0 indicates that the interface business processing fails, and 1 indicates that the interface business processing succeeds), the attribute value is of object type, and then the key value of the obtained attribute value is compared with the response message respData value, so as to determine whether the key corresponding to the status code in the response message is 1 or 0. Similarly, other data contents can be converted. In this embodiment, after the data contents in a predetermined format are converted, that is, after the status code, business data, error message msg (optional) and user-friendly prompt notice (optional) in a predetermined format are converted, an instance of a unified response data tool class Response can be initialized using the above parameters and returned. Of course, the data contents in each predetermined format can also be output one by one.
[0105] In the specific implementation process of this embodiment, after the target interface initiates the interface request, the response interceptor processing function can also monitor whether the interface request initiated by the target interface has timed out, and monitor whether the target interface has received an error code; when the interface request has timed out, or the interface receives an error code, the target message is generated based on the predetermined error message. In other words, the method in this application can process the interface request not being returned or the request timeout. That is, in the response interceptor processing function, if the service end does not return data or the interface request does not respond after the preset time after initiating the interface request, then the predetermined timeout error status code and the predetermined timeout error prompt information can be used to initialize and return an instance of the unified response data tool class Response. In addition, this application can also process common Http network return error codes, that is, for common error codes in the Http protocol such as 404, 500, and 502, the predetermined error status code and the predetermined error prompt information are used to initialize and return an instance of the unified response data tool class Response, that is, to generate a target message in a predetermined format.
[0106] In the specific implementation process of this embodiment, based on the pre-configured response interceptor processing function, according to each target data item in the message template, the target response data corresponding to each target data item is determined from the current message; according to the data format corresponding to each target data item in the request template, the format conversion processing is performed on each target response data to generate the target message. The specific process can be as follows: Figure 4 shown.
[0107] (1) Determine whether there is an interface request prompt configuration in the initiating request configuration config. If true, first close the default loading prompt box of the network interface request.
[0108] (2) To process the interface request that is not returned or the request times out. In the response interceptor processing function, if the server does not return data after initiating the interface request or the interface request does not respond within the preset time, the software will use the timeout error status code and timeout error prompt information to initialize and return an instance of the unified response data tool class Response.
[0109] (3) Process common error codes returned by the HTTP network. For common error codes in the HTTP protocol, such as 404, 500, and 502, process them by throwing errors upward or directly popping up error prompts according to the error codes. At the same time, use the error status code and the error prompt information returned by the interface to initialize an instance of the unified response data tool class Response and return it.
[0110] (4) After correctly receiving the return message from the network interface, parse the message. It should be noted that the correct reception of the interface return message here refers to the successful return of the interface response message after the developer initiates the Http network interface request, but this does not mean that the business processing of the interface is successful. After receiving the interface response message, configure the config according to the request and perform custom configuration processing first, such as message decryption.
[0111] (5) Execute the template parsing function findRespData to process the template and network response message data. The function receives the parameters: template data format template and response message respData. This method mainly parses the response message respData according to the message template template, and finally parses out four parameters: status code code, business data data, error message msg (optional) and user-friendly prompt notice (optional). Then use these parameters to initialize an instance of a unified response data tool class Response and return it. The specific processing process is as follows:
[0112] (5.1) Check the data format of the message template. If the data format of the message template is empty or does not meet the format requirements, an error message is given and the process returns to end.
[0113] (5.2) Get the attribute array of the object in the message template template, traverse the array, get the key representing the status code in the response message, and get the attribute value of the key in the template. The attribute value is of object type. Use the key and value of the attribute value to compare with the respData value of the response message to get the actual interface return status code value, and assign it to the status code code.
[0114] (5.3) Compare the status code code with the business processing success code to see if they are consistent. If they are inconsistent, it is considered that the interface business processing is wrong. At this time, the attribute key representing the error information in the message template template is traversed to obtain. After obtaining the key, the error information returned by the interface is obtained from the response message respData and assigned to the error information msg.
[0115] (5.4) Get the key representing the response data message in the response message, and get the attribute value of the key in the message template. In order to take into account the flexibility of the Http response message, the data attribute value here may be multiple data types or multi-layer nested. For example, multiple data type judgments can be performed, and recursive traversal can be performed to obtain the key representing the business data in the response message respData in the final message template, and use the key to obtain the business data from the response message respData and assign it to the business data data.
[0116] (6) If the response interceptor processing function contains the business data processing function dataHandler, then the dataHandler function will be executed, and the parsed data will be passed in as the actual parameter. After the processing is completed, it will be returned to obtain the target message.
[0117] In this application, by configuring templates / request templates and message templates, network interface response messages are processed uniformly, which can shield the differences in response messages of front-end applications when connecting to multiple platform service network interfaces, and provide consistent network request response data objects from the bottom layer of the application to the top layer, so that when users connect to the network interfaces of multiple platforms, they can obtain a unified response data format at the business processing logic layer and perform consistent data processing, which is conducive to reducing the difficulty of application development, improving development efficiency, and enhancing the user experience of the application.
[0118] In this application, the network request is encapsulated. After instantiating the public network request tool class, users can simply and conveniently use the methods provided by it to make network requests.
[0119] In the encapsulation class, there is a default processing for the Http network interface. For example, before initiating a request, the default data processing format, timeout settings and other request header information are specified in the "Request Interceptor". When the request is returned, the "Response Interceptor" performs default processing for common Http network request error codes, such as 404, 500, 502, etc. Of course, users can adjust and modify the configuration template and message template according to actual needs when using it.
[0120] In this application, when the application needs to connect to the network interfaces of different platforms, it is only necessary to configure the request data requirements / request templates and the response data format / message templates for the interfaces of the corresponding platforms, and then the request data can be spliced in the "request interceptor" according to the request template configured by the user, and the interface request can be initiated. When the interface returns, the network interface data of each platform can be processed in the "response interceptor" according to the message template configured by the user, thereby generating a target message in a unified format. In this application, in addition to processing common Http network request error codes, such as 404, 500, 502, etc., business data can also be preliminarily processed, for example, login timeout and re-jump login, data parsing failure pop-up window prompt, etc., and consistent request response status codes and data are provided upward.
[0121] By adopting the method in this application, a large amount of repeated network interface message processing code can be greatly reduced, which helps to improve the development speed of developers, and can also reduce the file size after packaging, reduce the traffic loss when loading resources, and improve the loading speed of the application. In addition, since the network request has been encapsulated at the bottom of the application, when developing the application, a general, simple and easy-to-use network call interface can be provided upward, which can also reduce the development difficulty of other developers. At the same time, the same error handling method can bring a consistent user experience to users, which is also conducive to improving the user experience of the application.
[0122] Based on the above embodiment, another embodiment of the present application is a message generating device, such as Figure 5 As shown, including:
[0123] The acquisition module 11 is used to obtain the requested configuration information of the target interface in response to the access request operation for the target interface;
[0124] A request generation module 12, configured to generate an interface request for accessing a target interface based on the request configuration information and using a predetermined request generation method, and to obtain key configuration information from the interface request configuration information;
[0125] The receiving module 13 is used to send the interface request to the target interface and receive an initial response message returned by the target interface for the interface request;
[0126] A preprocessing module 14, configured to preprocess the initial response message based on the key configuration information to obtain a current message;
[0127] The message generation module 15 is used to generate a target message of a predetermined format based on the current message and a predetermined message generation method.
[0128] Based on the above embodiments, the request generation module is specifically used to: pre-configure a request interceptor processing function based on a predetermined request template, or pre-configure a request interceptor processing function based on a predetermined target request processing function; based on the request template or target request processing function in the request interceptor processing function, process the interface request configuration information to generate the interface request.
[0129] On the basis of the above embodiment, the request generation module is specifically used to: determine the target request configuration information corresponding to each target configuration item from the request configuration information based on the pre-configured request interceptor processing function and according to each target configuration item in the request template; process each target request configuration information according to the processing method corresponding to each target configuration item in the request template to generate the interface request;
[0130] Or used to obtain the parameter option of the target request processing function; call the target request processing function from the request interceptor processing function, process the request configuration information and the parameter option to generate the interface request.
[0131] In the specific implementation process of this embodiment, the request configuration information includes any one or more of the following: user name, password, request header, request body information, network interface basic path, network interface uniform resource locator URL, message encryption method, request method;
[0132] The message generating device also includes a creation module, which is used to: configure a first constructor before configuring the request interceptor processing function; based on the first constructor, respond to the user's addition operation on the target configuration item, and respond to the user's addition operation on the processing method of each target configuration item to create the request template.
[0133] In the specific implementation process of this embodiment, the request generation module is specifically used to: for a target configuration item of a method type, execute the target method in the target request configuration information to obtain execution information; for a target configuration item of a path type, perform access path splicing processing to obtain a splicing path; for a target configuration item of a request parameter type, perform verification processing on the request parameters in the target request configuration information to obtain verification information; for a target configuration item of a request header information type, perform interface request header merging processing to obtain request header merging information; for a target configuration item of an encryption requirement type, obtain the encryption method of the request message from the target request configuration information item; and generate an interface request based on the execution information, splicing path, verification information, request header merging information and encryption method.
[0134] In the specific implementation process of this embodiment, the key configuration information includes an encryption method: the processing module is specifically used to: determine a decryption method based on the encryption method; and decrypt the initial message based on the decryption method to obtain the current message.
[0135] In the specific implementation process of this embodiment, the message generation module is specifically used to:
[0136] Preconfigure a response interceptor processing function based on a predetermined message template, or preconfigure a response interceptor processing function based on a predetermined target response processing function; based on the message template or target response processing function in the response interceptor processing function, process the current message to generate the target message.
[0137] In the specific implementation process of this embodiment, the current message is processed based on the message template or the target response processing function in the response interceptor processing function to generate the target message, specifically including: based on the pre-configured response interceptor processing function, according to the target data items in the message template, the target response data corresponding to each target data item in the current message is determined; according to the data format corresponding to each target data item in the request template, each target response data is format converted to generate the target message; or, the parameter respOption of the target response processing function is obtained; the target response processing function is called from the response interceptor processing function, and the target message is obtained based on the response data in the current message and the parameter option.
[0138] In the specific implementation process of this embodiment, the message generating device also includes a monitoring module, which is used to: after the target interface initiates an interface request, monitor whether the interface request initiated by the target interface times out, and monitor whether the target interface receives an error code; when the interface request times out or the interface receives an error code, generate the target message based on a predetermined error message.
[0139] The message generation device in the present application can be connected to network interfaces of different platforms. Through a predetermined request generation method and a predetermined message generation method, it can generate request data / interface request of a predetermined format based on the request generation method, and initiate an interface request. When the target network interface returns a response message, a target message of a unified format can be generated according to the predetermined message generation method, solving the problem of inconsistent message formats generated by various network interfaces.
[0140] The above embodiments are only exemplary embodiments of the present application and are not intended to limit the present application. The protection scope of the present application is defined by the claims. Those skilled in the art may make various modifications or equivalent substitutions to the present application within the essence and protection scope of the present application, and such modifications or equivalent substitutions shall also be deemed to fall within the protection scope of the present application.
Claims
1. A message generation method, characterized in that: include: In response to an access request operation for a target interface, obtaining request configuration information of the target interface; Based on the request configuration information, using a predetermined request generation method, an interface request for accessing a target interface is generated, and key configuration information is obtained from the interface request configuration information; Sending the interface request to the target interface, and receiving an initial response message returned by the target interface in response to the interface request; Preprocessing the initial response message based on the key configuration information to obtain a current message; A target message in a predetermined format is generated based on the current message and a predetermined message generation method.
2. The method according to claim 1, characterized in that Based on the interface request configuration information, using a predetermined request processing method, an interface request for accessing a target interface is generated, specifically including: Pre-configuring a request interceptor processing function based on a predetermined request template, or pre-configuring a request interceptor processing function based on a predetermined target request processing function; Based on the request template or the target request processing function in the request interceptor processing function, the interface request configuration information is processed to generate the interface request.
3. The method according to claim 2, characterized in that The processing of the interface request configuration information based on the request template or the target request function in the request interceptor processing function to generate the interface request specifically includes: Based on the pre-configured request interceptor processing function, according to each target configuration item in the request template, the target request configuration information corresponding to each target configuration item is determined from the request configuration information; Processing each of the target request configuration information according to the processing mode corresponding to each target configuration item in the request template to generate the interface request; Alternatively, obtain the request parameter option of the target request processing function; The target request processing function is called from the request interceptor processing function, and the request configuration information and the request parameter option are processed to generate the interface request.
4. The method according to claim 2, characterized in that The request configuration information includes any one or more of the following: request header, request body information, network interface basic path, network interface uniform resource locator URL, message encryption method, request method; Before configuring the request interceptor processing function, the method further includes creating the request template, specifically including: Configure the first constructor; Based on the first constructor, in response to the user's adding operation on the target configuration item, and in response to the user's adding operation on the processing mode of each target configuration item, the request template is created.
5. The method according to claim 3, characterized in that The processing of each target request configuration information according to the processing mode corresponding to each target configuration item in the request template to generate the interface request specifically includes: For a target configuration item of a method type, executing a target method in the target request configuration information to obtain execution information; For the target configuration item of the path type, access path concatenation is performed to obtain a concatenated path; For the target configuration item of the request parameter type, verify the request parameters in the target request configuration information to obtain verification information; For the target configuration item of the request header information type, perform interface request header merging processing to obtain request header merging information; For the target configuration item of the encryption requirement type, the encryption method of the request message is obtained from the target request configuration information item; An interface request is generated based on the execution information, the splicing path, the verification information, the request header merging information and the encryption method.
6. The method according to claim 1, characterized in that The key configuration information includes encryption method: The preprocessing of the initial response message based on the key configuration information to obtain the current message specifically includes: Determine a decryption method based on the encryption method; The initial message is decrypted based on the decryption method to obtain the current message.
7. The method according to claim 1, characterized in that The step of generating a target message in a predetermined format based on the current message and a predetermined message generation method specifically includes: Pre-configuring a response interceptor processing function based on a predetermined message template, or pre-configuring a response interceptor processing function based on a predetermined target response processing function; Based on the message template or the target response processing function in the response interceptor processing function, the current message is processed to generate the target message.
8. The method according to claim 7, characterized in that The processing of the current message based on the message template or the target response processing function in the response interceptor processing function to generate the target message specifically includes: Based on the pre-configured response interceptor processing function, according to each target data item in the message template, determine the target response data corresponding to each target data item from the current message; According to the data format corresponding to each target data item in the request template, format conversion processing is performed on each target response data to generate the target message; Alternatively, obtaining the response parameter respOption of the target response processing function; The target response processing function is called from the response interceptor processing function, and processing is performed based on each response data and response parameter respOption in the current message to obtain the target message.
9. The method according to claim 1, characterized in that After the target interface initiates an interface request, the method further includes: Monitoring whether the interface request initiated by the target interface times out, and monitoring whether the target interface receives an error code; When the interface request times out, or the interface receives an error code, the target message is generated based on predetermined error information.
10. A message generating device, characterized in that: include: An acquisition module, used for acquiring the requested configuration information of the target interface in response to the access request operation for the target interface; A request generation module, used to generate an interface request for accessing a target interface based on the request configuration information and using a predetermined request generation method, and to obtain key configuration information from the interface request configuration information; A receiving module, used for sending the interface request to the target interface, and receiving an initial response message returned by the target interface for the interface request; A preprocessing module, used to preprocess the initial response message based on the key configuration information to obtain a current message; The message generation module is used to generate a target message in a predetermined format based on the current message and a predetermined message generation method.
