ProcessingConfiguration is an important component in Remote Communication Kit for customizing response processing actions. It allows you to execute custom logic before or after messages are distributed to different processors. An example scenario is checking whether the result code 200 (success response) is returned.
The processing customization capability supports phones, 2-in-1 devices, tablets, and wearables. Starting from version 5.1.1(19), it is also supported on TVs.
Import the required modules.
import { rcp } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit'; Create a session and a request, and define the processing configuration.
const session = rcp.createSession();
const request = new rcp.Request('https://www.example.com'); // Replace the URL with the actual one.
// Define the processing configuration. In this case, the logic is used to check whether the result code is 200.
const processing: rcp.ProcessingConfiguration = {
validateResponse: (response: rcp.Response): boolean => {
return response.statusCode === 200;
},
};
// Apply the processing configuration to the request.
request.configuration = {
processing: processing,
}; Send a request and wait for the response. If the return value of processing.validateResponse is true, the then method is executed to process the success response. If the return value of processing.validateResponse is false, the catch method is executed to handle the error that occurs.
session.fetch(request).then((response: rcp.Response) => {
// If the return value of processing.validateResponse is true, the following process is executed.
if (response) {
console.info(`Response received with status code: ${response.statusCode}`);
// ...
} else {
console.error('No response received');
// ...
}
session.close();
}).catch((err: BusinessError) => {
// If the return value of processing.validateResponse is false, the following process is executed.
console.error(`The error code is ${err.code}, error data is ${err.data}`);
// ...
session.close();
});