Created
April 4, 2024 13:03
-
-
Save hicglobalsolutions/74e5c3a69e584ec88e5353c8681302fd to your computer and use it in GitHub Desktop.
Create a new Apex class with the name GoogleCloudPubSubService.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class GoogleCloudPubSubService { | |
// AuraEnabled annotation makes the method available to Lightning components | |
public static String Endpoint ='https://pubsub.googleapis.com/v1/<Replace with Your Subscription Name>:'; | |
public static String token = 'Bearer ' + '<Replace with your Generated token>'; | |
@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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment