123 lines
6.2 KiB
OpenEdge ABL
123 lines
6.2 KiB
OpenEdge ABL
public with sharing class GoogleCloudPubSubService {
|
|
// AuraEnabled annotation makes the method available to Lightning components
|
|
public static String Endpoint ='https://pubsub.googleapis.com/v1/projects/cosmic-kayak-415607/subscriptions/Salesforce_Sub:';
|
|
public static String token = 'Bearer ' + 'ya29.a0AfB_byCl9AVfe1VW7aF3xcztdGfO2CV_fYiuSEDSfpOBurXq0q5iTvKbJIzbZZUfIvvazwe61V5IEa3ktEof_CsByETJuNjni09HPdjL2mPuqrTabCF3EEtm48FL_58FdMPJgB_03RTXVcVB0itk_CgBe5f9M1cgYpZ-vu-C0hjchmA-vs04Oej_4ynTUPAv3zydoKlCXrJfTeHyxx6GWiHiz7Butdy7JvwysN_Q_E1KYrTzzX4tijS4V9qGw6vE5_WHGkeM3AR4wMncOmxKLnY6jOkDgh7sMOnRMJbqEXjgs0NzWVV4S3F2WfJ7FSgKqgmYGxkB6z7ddeApsrl2opozhZEbbF915ghHysBXB0rrDCCFUhaLWyTp4LJz8QbwlQNLA1SF4cMSERbJFPmGoeWfVLJOf5QYJwaCgYKAXYSARESFQHGX2Mih7BuVj3ub1fCx4ZEKbKu0w0425';
|
|
@AuraEnabled
|
|
public static List<Map<String, Object>> pullMessages() {
|
|
// Initialize a map to hold the response body
|
|
Map<String, Object> responseBody = new Map<String, Object>();
|
|
// Initialize a list to hold the processed messages data
|
|
List<Map<String, Object>> messagesData = new List<Map<String, Object>>();
|
|
try {
|
|
// Endpoint URL
|
|
String url = Endpoint+'pull';
|
|
|
|
// Create a map for the request payload
|
|
Map<String, Object> payloadMap = new Map<String, Object>();
|
|
payloadMap.put('maxMessages', 10); // Set the maximum number of messages to pull
|
|
|
|
// Convert the payload map to a JSON string
|
|
String payloadJson = JSON.serialize(payloadMap);
|
|
|
|
// Create a new HttpRequest
|
|
HttpRequest req = new HttpRequest();
|
|
req.setEndpoint(url); // Set the endpoint URL
|
|
req.setMethod('POST'); // Set the HTTP method to POST
|
|
req.setTimeout(120000); // Set the timeout to 120 seconds
|
|
req.setHeader('Content-Type', 'application/json'); // Set the Content-Type header to application/json
|
|
|
|
// Set the Authorization header to the bearer token
|
|
req.setHeader('Authorization',token);
|
|
req.setBody(payloadJson); // Set the request body
|
|
|
|
// Create a new Http object and send the request
|
|
Http http = new Http();
|
|
HttpResponse res = http.send(req);
|
|
if(res.getStatusCode() == 200){
|
|
// If the response status code is 200 (OK), deserialize the response body
|
|
responseBody = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
|
|
}
|
|
|
|
// Get the received messages from the response body
|
|
List<Object> receivedMessagesObjects = (List<Object>) responseBody.get('receivedMessages');
|
|
List<Map<String, Object>> receivedMessages = new List<Map<String, Object>>();
|
|
for (Object obj : receivedMessagesObjects) {
|
|
// Cast each received message to a map and add it to the list
|
|
receivedMessages.add((Map<String, Object>) obj);
|
|
}
|
|
|
|
for (Map<String, Object> receivedMessage : receivedMessages) {
|
|
// Get the message from the received message
|
|
Map<String, Object> message = (Map<String, Object>)receivedMessage.get('message');
|
|
// Get the data from the message and decode it
|
|
String data = (String)message.get('data');
|
|
Blob dataAsBlob = EncodingUtil.base64Decode(data);
|
|
String decodedData = dataAsBlob.toString();
|
|
// Deserialize the decoded data to a map
|
|
Map<String, Object> decodedDataMap = (Map<String, Object>) JSON.deserializeUntyped(decodedData);
|
|
// Get the message ID, ack ID, and publish time from the message
|
|
String messageId = (String)message.get('messageId');
|
|
String ackId = (String)receivedMessage.get('ackId');
|
|
String publishTime = (String)message.get('publishTime');
|
|
|
|
// Create a map to hold the message data
|
|
Map<String, Object> messageData = new Map<String, Object>();
|
|
messageData.put('messageId', messageId);
|
|
messageData.put('data', decodedDataMap);
|
|
messageData.put('ackId', ackId);
|
|
messageData.put('publishTime', publishTime);
|
|
|
|
// Add the message data to the list
|
|
messagesData.add(messageData);
|
|
}
|
|
|
|
// Log the entire response body
|
|
System.debug('Response Body: ' +messagesData);
|
|
|
|
|
|
} catch (Exception e) {
|
|
// Log any exceptions that occur
|
|
System.debug('Error in pullMessages: ' + e.getMessage());
|
|
}
|
|
return messagesData; // Return the list of message data
|
|
}
|
|
@AuraEnabled(cacheable=true)
|
|
public static void acknowledgeMessages(List<String> ackIds) {
|
|
try {
|
|
// Endpoint URL
|
|
String url = Endpoint+'acknowledge';
|
|
|
|
// Create a map for the request payload
|
|
Map<String, Object> payloadMap = new Map<String, Object>();
|
|
payloadMap.put('ackIds', ackIds); // Set the ackIds to acknowledge
|
|
|
|
// Convert the payload map to a JSON string
|
|
String payloadJson = JSON.serialize(payloadMap);
|
|
|
|
// Create a new HttpRequest
|
|
HttpRequest req = new HttpRequest();
|
|
req.setEndpoint(url); // Set the endpoint URL
|
|
req.setMethod('POST'); // Set the HTTP method to POST
|
|
req.setTimeout(120000); // Set the timeout to 120 seconds
|
|
req.setHeader('Content-Type', 'application/json'); // Set the Content-Type header to application/json
|
|
|
|
// Set the Authorization header to the bearer token
|
|
req.setHeader('Authorization',token);
|
|
req.setBody(payloadJson); // Set the request body
|
|
|
|
// Create a new Http object and send the request
|
|
Http http = new Http();
|
|
HttpResponse res = http.send(req);
|
|
|
|
if(res.getStatusCode() == 200){
|
|
System.debug('Acknowledge successful');
|
|
} else {
|
|
System.debug('Acknowledge failed: ' + res.getStatus());
|
|
}
|
|
} catch (Exception e) {
|
|
// Log any exceptions that occur
|
|
System.debug('Error in acknowledgeMessages: ' + e.getMessage());
|
|
}
|
|
}
|
|
|
|
} |