Skip to content

Instantly share code, notes, and snippets.

@forcementor
Last active December 16, 2017 05:21
Show Gist options
  • Select an option

  • Save forcementor/754efb1e2a6cfbb42f087d38761c6a12 to your computer and use it in GitHub Desktop.

Select an option

Save forcementor/754efb1e2a6cfbb42f087d38761c6a12 to your computer and use it in GitHub Desktop.
//====================================================================
// Name: MicroserviceManager
// Type: Class
// Purpose: Routines for managing setup and calls to a microservice
// Created by: Don Robins
// Created on: Dec 1, 2017
//====================================================================
public class MicroserviceManager {
//Endpoint as Named Credential for the PDF Parser service.
public static final String PDF_PARSER_ENDPOINT = 'callout:PDFParser' + '/convert';
public static void parsePDFToText(List<Id> idList) {
System.Debug('MicroserviceManager.parsePDFToText idList[]: ' + idList[0]);
Id targetId = idList[0]; // If bulk, only process first
//Fetch the parent record.
Contact c = MicroserviceManager.getContactById(targetId);
//Fetch the PDF attachment.
Blob pdfBody = MicroserviceManager.getPDF(c.id);
if(pdfBody != null) {
//Queue the job.
System.enqueueJob(new QueueableParsePDFCall(PDF_PARSER_ENDPOINT, 'POST', pdfBody, targetId));
} else {
string msg = 'No attachment found for Contact: ' + c.name;
system.debug('MicroserviceManager.parsePDFToText() msg: ' + msg);
//Store the message in the record.
c.Parsed_Text__c = msg;
update c;
}
}
//Async apex to call out to the PDF Parser service and return parsed text from the PDF blob.
public class QueueableParsePDFCall implements System.Queueable, Database.AllowsCallouts {
private final String url;
private final String method;
private final Blob body;
private final Id parentId;
public QueueableParsePDFCall(String url, String method, Blob body, Id parentId) {
this.url = url;
this.method = method;
this.body = body;
this.parentId = parentId;
}
public void execute(System.QueueableContext ctx) {
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(method);
req.setBodyAsBlob(body);
system.debug('Request: ' + req);
//Make the callout.
Http http = new Http();
HttpResponse res;
res = http.send(req);
//Get the parsed text.
String textToParse = res.getBody();
//system.debug('MicroserviceManager.QueueableParsePDFCall.execute() textToParse: ' + textToParse);
//Fetch the Contact record.
Contact c = MicroserviceManager.getContactById(parentId);
//Adjust fields.
c.Parsed_Text__c = textToParse;
//Persist.
try {
update c;
system.debug('Updated contact with parsed text!');
} catch (Exception e) {
system.debug('Update exception: ' + e);
}
//system.debug('MicroserviceManager.QueueableParsePDFCall.execute() Stored raw text in: ' + c.name);
}
}
//==================
//Utility routines
//==================
//Fetch the parent by record id.
public static Contact getContactById(id parentId) {
Contact c;
List<Contact> cList = [SELECT id, Name, Parsed_Text__c FROM Contact WHERE Id = : parentId];
if (cList.size() > 0) {
c = cList[0];
} else {
c = new Contact();
}
//system.debug('MicroserviceManager.getContactById() c: ' + c);
return c;
}
//Fetch a PDF attachment as a blob based on the parent record id.
public static Blob getPDF(id parentId) {
Blob returnBlob;
//Fetch the PDF Attachment as a blob (this WON'T work in Lightning if attached as a File.)
List<Attachment> aList = [SELECT Body, ParentId FROM Attachment WHERE parentId = :parentId];
if (aList.size() > 0) {
returnBlob = aList[0].body;
}
system.debug('MicroserviceManager.getPDF() returnBlob: ' + returnBlob);
return returnBlob;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment