Setup a method to send Aync call to Heroku, add ‘?_HttpMethod=PATCH’ to the path to notify Heroku it’s a patch request
@future(callout=true)
public static void callApiEndpointAsync(String apiEndpoint, String method, String aPayload){
HttpRequest req = new HttpRequest();
List<String> theArgs = new List<String>();
HttpResponse res = new HttpResponse();
try {
if (apiEndpoint != null) {
req.setTimeout(120000);
if ('PATCH'.equals(method)){
apiEndpoint += '?_HttpMethod=PATCH';
req.setMethod('POST');
} else {
req.setMethod(method);
}
setAuthHeaderAsync(req);
req.setEndpoint(herokuUrl + apiEndpoint);
if (aPayload!=null)
req.setBody(String.valueOf(aPayload));
if (Test.isRunningTest() && (mock!=null)) {
mock.respond(req);
} else {
Integer retry = 0;
while (retry < 2){
Http http = new Http();
res = http.send(req);
if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
retry +=2;
} else if (res.getStatusCode()==503 || res.getStatusCode()==400){
retry +=1;
res = http.send(req);
} else {
retry +=1;
theArgs.add('Api');
theArgs.add(req.getEndpoint());
theArgs.add(res.getBody());
}
}
throw new Rest_Exception(ResponseCodes_Mgr.getCode('HEROKU_REQUEST_CALLOUT_FAILED', null, theArgs));
}
} else {
System.debug('Service apiEndpoint and payload must not be null');
throw new Rest_Exception(ResponseCodes_Mgr.getCode('HEROKU_REQUEST_CALLOUT_FAILED'));
}
} catch (Exception ex) {
theArgs.add('Api');
theArgs.add(req.getEndpoint());
theArgs.add(res.getBody());
throw new Rest_Exception(ResponseCodes_Mgr.getCode('HEROKU_REQUEST_CALLOUT_FAILED', ex, theArgs));
}
}
Spring controller checks the RequestParam _HttpMethod to see if it’s a POST or PATCH request
@RequestMapping(value = "/customer", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
@ApiOperation(value = "Creates new payment customer")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Creates new payment customer")})
public String createCustomer(@RequestBody PaymentCustomerWrapper paymentCustomerWrapper, @RequestParam(value="_HttpMethod", defaultValue="POST") String httpMethod)
throws IOException, URISyntaxException {
if (httpMethod!=null && "PATCH".equals(httpMethod)){
itsLogger.debug("update payment customer {}", paymentCustomerWrapper.toString());
return paymentService.updatePaymentCustomer(paymentCustomerWrapper).toJson();
} else {
itsLogger.debug("create payment customer {}", paymentCustomerWrapper.toString());
return paymentService.createChargeBeeCustomer(paymentCustomerWrapper).toJson();
}
}