Calling RESTful APIs with a RESTful Service
You can configure a RESTful service so Astro Zero can call the RESTful APIs provided by third-party systems. A RESTful service is a group of modularized RESTful APIs (RESTful actions). Once the connection is established, you can add actions to transform third-party APIs into a format recognizable by Huawei Cloud Astro Zero. This allows Huawei Cloud Astro Zero to call the APIs effectively.
Creating a Custom Connector to Interconnect with a RESTful Service
You can configure a custom link to connect to a RESTful service and incorporate actions that enable the system to call the RESTful APIs from a third-party system, thereby utilizing the services provided by that third-party system.
- Log in to the application designer by referring to Logging In to the Application Designer.
- In the navigation pane, choose Integrations.
- In the Connectors area, click Connector Instance.
- In the Type area, select Custom Connector.
- Click +, configure connector information, and click Save.
Figure 1 Creating a custom connector
- On the Authentication Information tab page, click New.
- Set authentication information and click Save.
You can create multiple authentications. Different actions can have different authentication modes.
Figure 2 Setting basic authentication informationTable 2 Authentication information parameters Parameter
Description
Label
Authentication label, which is displayed on the page.
Value: 1–64 characters.
Name
Authentication name, which uniquely identifies the authentication in the system. The naming requirements are as follows:
- The value cannot exceed 64 characters, including the prefix namespace.
To prevent duplicate data names among different tenants in Huawei Cloud Astro Zero, each tenant must define a unique namespace when first creating an application. A tenant can create only one namespace. After being created, the namespace cannot be modified.
- Start with a letter and can contain only letters, digits, and underscores (_). Do not end with an underscore (_).
Category
Category to which the authentication belongs. Select a value from the drop-down list.
Description
Enter the authentication description as required.
Value: 1–255 characters.
Use Certificate
Whether the client certificate is required on the server and whether bidirectional authentication is required. If this parameter is selected, you need to configure the Type, Certificate, and Key.
Use Root Certificate
Whether the server certificate is required on the client and whether bidirectional authentication is required.
If this parameter is selected, you need to enable root certificate verification in the management center and upload the root certificate. For details, see Enabling Root Certificate Authentication for Server Identity Verification. After the upload, select the corresponding certificate from the Root Certificate drop-down list.
API Host Address
After this parameter is configured, only RESTful operation requests whose request URL starts with the specified address will be allowed. This prevents leakage of sensitive information. Each time the address is changed, the authentication information needs to be reconfigured.
Package Current Configuration
If this parameter is selected, the connector will be packed together with applications. For example, if an application package is released to the runtime environment, the current configuration is synchronized to the runtime environment by default. This parameter is selected by default. You are advised not to select this option in information-sensitive scenarios.
Authentication Settings
- Authentication protocol: Options are OAuth, Simple headers, and JWT.
- Authentication mode: Set it to client credentials. Only client credentials of OAuth 2.0 are supported.
- Authentication address: Address for a third-party system to obtain the token through the OAuth 2.0 protocol. An example address to obtain a token is https://{host}:{port}/baas/auth/v1.0/oauth2/token.
- Client ID: Client ID in the OAuth 2.0 protocol. If the RESTful service is provided by Astro Zero, perform the following steps to view the RESTful service:
- Access the Astro Zero environment configuration page.
- In the navigation pane, choose Integrated Connection > OAuth.
- In the OAuth authentication list, view the client ID corresponding to the authentication.
- Client secret: Client secret in the OAuth 2.0 protocol. If the RESTful service is provided by Astro Zero, perform the following steps to view the RESTful service:
- The Huawei Cloud Astro Zero environment configuration page is displayed.
- In the navigation pane, choose Integrated Connection > OAuth.
- In the OAuth authentication list, click
of the corresponding authentication.
- In the displayed dialog box, click OK to download the key file to the local PC.
client_secret in the key file is the value of Client Secret.
- Body Property: Mapping rules for message fields returned from authentication APIs.
Figure 4 describes the response body based on the OAuth2 standard protocol. The key returned by some third-party APIs may not be the standard key. For example, some third-party APIs will return accessToken instead of access_token used in Astro Zero. Therefore, you need to configure the mapping between the keys.
The message body property configured in Figure 3 is accessToken returned by a third-party system. Huawei Cloud Astro Zero uses the access_token key value to store the message body property. The final storage result is as follows:
{ "access_token": ${accessToken} }
- Header Property: Mapping rules for message headers in requests to service APIs.
According to OAuth 2.0, the token is put in the request header: Authorization: Bearer ${access_token}.
However, some APIs do not comply with this rule. For example, for an API used to access the platform, the token is put in the request header Access-Token and does not need the prefix Bearer.
The message header property configured in Figure 3 indicates that you need to obtain the value of access_token from the message body and put the value in the request header Access-Token.
- The value cannot exceed 64 characters, including the prefix namespace.
Creating and Enabling a RESTful Action
After the RESTful service is interconnected, you can incorporate actions that enable the system to call the RESTful APIs from a third-party system, thereby utilizing the services provided by that third-party system.
- Click the RESTful service created in Creating a Custom Connector to Interconnect with a RESTful Service. On the Action tab page, click New.
- After setting the information, click the next button.
Figure 5 Parameters for creating an action
- Configure the header input parameter and click Next.
If this API does not have header input parameters, delete the first row. If there are multiple header input parameters, click
. If the header parameters have been defined during creation of the RESTful service, click Insert From Template and modify the template.
Figure 6 Header input parameter configuration pageTable 4 Parameters on the header input parameter configuration page Parameter
Description
Label
Label of a message header input parameter, which is displayed on the page.
Name
Unique name of a header input parameter.
Data Type
Data type of a header input parameter. The options are as follows:- Number
- String
- Boolean
- Sensitive Text
Default Value
Default value of the header input parameter. You can customize the value only after selecting Default.
Description
Enter the description of the input parameter.
Default
Indicates whether the input parameter of the header has a default value. If this parameter is selected, you can customize the value in Default Value. If Data Type is set to Sensitive Text, the default value cannot be set.
Required
Indicates whether the input parameter of the header is mandatory.
Operation
Click
or
to copy or delete the header input parameter.
- Configure the input parameter and click Next.
If this API does not have input parameters, you do not need to set the parameters. Delete the first row. If there are multiple input parameters, click
and define basic information about the input parameters.
Message body format in the upper right corner of the page is displayed only when Method is set to PUT or POST. If the third-party input parameter is of the array or non-JSON type, set Message body format to Value.
For example, if Message body format is set to Key-Value and input parameters A, B, and C are defined, the request message body will pass {A:1, B:2, C:3}, and the request receiver will receive {A:1, B:2, C:3}. If Message body format is set to Value, $in will be introduced. The request message body will pass {$in: [1,2,3]}, and the request receiver will receive [1,2,3].
Figure 7 Page for configuring the input parameterTable 5 Input parameter description Parameter
Description
Label
Label of an input parameter, which is displayed on the page.
Name
Name of an input parameter, which uniquely identifies the input parameter in the system.
Placement
Position of the input parameter. The options are as follows:
- Body
- URL Query
- URL Path
Data Type
Data type of the input parameter. The options are as follows:- Date: accurate to days.
- Date Time: accurate to seconds.
- Number: The value is a number.
- String
- Boolean: Only two values are allowed, for example, true and false.
- External Struct
- Map
- Object: Object identifier type.
- Sensitive Text: Such as name or mobile number.
Namespace
If DataType is set to External Struct, you need to set the namespace to which the input parameter belongs.
Metadata Name
When the data type is set to object, you need to select the metadata name, that is, the custom object created in the application.
Default Value
Sets the default value of an input parameter.
Description
Enter the description of the input parameter.
Value: 1–255 characters.
Default
Indicates whether an input parameter has a default value. If this parameter is selected, you can customize the value in Default Value. If DataType is set to Sensitive Text, the default value cannot be set.
Required
Indicates whether an input parameter is mandatory.
Collection
Whether the input parameter is set to the array type.
Operation
Click
or
to copy or delete the input parameter.
- Configure output parameters and click the save button.
If this API does not have output parameters, you do not need to set the parameters. Delete the first row. If there are multiple output parameters, click
and define basic information about the output parameters.
When you select Value for Message body format, a common variable $out, will be introduced. The body content will be saved to $out as output parameters.
For example, if Message body format is set to Key-Value and output parameters A, B, and C are defined, the response message body will pass {A:1, B:2, C:3}, and the request sender will receive {A:1, B:2, C:3}. If Message body format is set to Value, $out will be introduced. The response message body will pass [1,2,3], and the request sender will receive {$out: [1,2,3]}.
Figure 8 Page for configuring output parametersTable 6 Parameters for configuring output parameters Parameter
Description
Label
Label of an output parameter, which is displayed on the page.
Name
Name of an output parameter, which uniquely identifies the output parameter in the system.
Placement
Position of the output parameter. The options are as follows:
- Header
- Body
Data Type
Data type of the output parameter. The options are as follows:- Date: The value is accurate to day.
- Date/Time: time type, accurate to second.
- Number: The value is a number.
- Character string: The value is a character string.
- Boolean: The value is true or false.
- Global structure
- Key-value pair (Map): Map collection type.
- Object: Object identifier type.
Namespace
If the data type is set to global structure, you need to set the namespace to which the parameter belongs.
Metadata Name
When the data type is set to object, you need to select the metadata name, that is, the custom object created in the application.
Description
Description of the parameter.
Collection
Whether the input parameter is set to the array type.
Operation
Click
or
to copy or delete the parameter.
- On the Action tab page, click
in the row where the RESTful action is located to enable the RESTful action.
You can click Export to export a ZIP package that contains all RESTful APIs of the service. You can click Import to import a YAML file to import RESTful actions.
Using a Flow to Call an Action
Create a flow. Then, use the flow to call the actions added to the RESTful service to integrate the service functions provided by the third party into Astro Zero.
- Log in to the application designer by referring to Logging In to the Application Designer.
- In the navigation pane, choose Logic.
- Click
next to Flow. On the displayed dialog box, specify the flow label and name and click Add.
Figure 9 Adding a flow - In the navigation pane of the flow designer page, choose Connector > Custom > Service name, and drag the RESTful service icon created in 2 to the canvas.
- Set the parameters for calling an action.
Figure 10 Configuring an action
Table 7 Parameters for calling an action Parameter
Description
Action
Select the action created in Creating and Enabling a RESTful Action.
Input Parameters
Click Add Row to set input parameters.
In Target, select the input parameter set in 4 and set the corresponding value in Source.
Output Parameters
Click Add Row to set output parameters.
In Target, select the output parameter set in 5 and set the corresponding value in Source.
- Click
in the upper part of the flow designer page to save the flow.
- Click
to activate the flow.
- In the input parameter area, enter the request parameters, click Run, and view the output result.
Using a Script to Call an Action
Create a script. Then, use the script to call the actions added to the RESTful service to integrate the service functions provided by the third party into Huawei Cloud Astro Zero.
- Log in to the application designer by referring to Logging In to the Application Designer.
- In the navigation pane, choose Logic.
- Click
next to Script. On the displayed dialog box, specify the script name, and click Add.
Figure 11 Adding an empty script - In the script editor, enter the following code:
// Here's your code. import * as connector from "connector"; let client = connector.newClient("Namespace__Rest1", "Namespace__test1"); let result = client.invoke("Namespace__flapRefund", {}); console.log(result)
Namespace __ Rest1 indicates the connector name, Namespace__test1 indicates the name of the authentication information configured in Creating a Custom Connector to Interconnect with a RESTful Service, and Namespace __flapRefund indicates the action name configured in Creating and Enabling a RESTful Action.
If input parameters are set in the connector, define a input2 in the script. The following is an example script:
// Here's your code. import * as connector from "connector"; let client = connector.newClient("Namespace__Rest1", "Namespace__test1"); let input2 = { "Parameter name": "Parameter value" } let result = client.invoke("Namespace__flapRefund", {}); console.log(result)
- Click
in the upper part of the script editor to save the script.
- After the script is saved, click
to run the script.
- On the input parameter tab page at the bottom of the page, enter request parameters and click
in the upper right corner of the test window.
- On the log tab page, view the printed logs.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot