My_Test_Repo/force-app/main/default/classes/PubSubDataRetriever.cls

54 lines
2.5 KiB
OpenEdge ABL

public class PubSubDataRetriever {
// Google Cloud Pub/Sub parameters
private static final String PUBSUB_PROJECT_ID = 'operating-ethos-414907';
private static final String PUBSUB_SUBSCRIPTION_ID = 'SalesforceTopic-sub';
private static final String PUBSUB_ENDPOINT = 'https://pubsub.googleapis.com/v1/projects/' + PUBSUB_PROJECT_ID +
'/subscriptions/' + PUBSUB_SUBSCRIPTION_ID + ':pull';
// Google Cloud API credentials
private static final String GOOGLE_CLOUD_API_KEY = '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';
// Method to retrieve data from Cloud Pub/Sub
public static List<Map<String, Object>> getDataFromPubSub() {
List<Map<String, Object>> pubsubMessages = new List<Map<String, Object>>();
// Build request JSON body
String requestBody = '{"returnImmediately": false, "maxMessages": 10}';
// Create HTTP request
HttpRequest request = new HttpRequest();
request.setEndpoint(PUBSUB_ENDPOINT);
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer ' + GOOGLE_CLOUD_API_KEY);
request.setHeader('Content-Type', 'application/json');
request.setBody(requestBody);
// Send HTTP request
Http http = new Http();
HttpResponse response = http.send(request);
// Parse response
if (response.getStatusCode() == 200) {
Map<String, Object> responseData = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
if (responseData.containsKey('receivedMessages')) {
List<Object> receivedMessages = (List<Object>) responseData.get('receivedMessages');
for (Object message : receivedMessages) {
Map<String, Object> messageData = new Map<String, Object>();
Map<String, Object> messageAttributes = (Map<String, Object>) ((Map<String, Object>) message).get('message');
String data = (String) messageAttributes.get('data');
messageData.put('data', EncodingUtil.base64Decode(data).toString());
pubsubMessages.add(messageData);
}
}
} else {
System.debug('Error retrieving data from Cloud Pub/Sub. Status code: ' + response.getStatusCode());
System.debug('Response body: ' + response.getBody());
}
// Print response body
System.debug('Response body: ' + response.getBody());
return pubsubMessages;
}
}