Esempio di codice per la creazione di una classe di provider Apex
Utilizzare questo codice di esempio per creare la classe di provider Apex per convertire un'immagine di prodotto in Base64. Incollare questo codice di esempio quando si crea la classe Apex ImageToBase64ConversionService.
Versioni (Edition) richieste
| Disponibile nelle versioni: Lightning Experience |
| Disponibile in: Enterprise Edition e Unlimited Edition con licenza aggiuntiva Life Sciences Cloud per Customer Engagement e pacchetto gestito Life Sciences Customer Engagement. |
Esempio
global with sharing class ImageToBase64ConversionService implements industriesintegrationfwk.ServiceIntegrationProvider, industriesintegrationfwk.ProcessIntegrationProvider {
public static final String HTTP_GET = 'GET';
public static final String SLASH = '/';
public static final String CALLOUT_KEY = 'callout:';
public static final String CONTENT_TYPE_HEADER = 'Content-Type';
public static final String HTTP_RESPONSE_KEY = 'httpResponse';
public static final String STATUS_CODE_KEY = 'statusCode';
public static final String CONTENT_TYPE_KEY = 'contentType';
public static final String BODY_KEY = 'body';
public static final String DISPLAY_URL_PARAM = 'displayUrl';
public static final String NAME_CRED_DEV_NAME = 'Get_Image_Named_Credential';
public static final String IMAGE_NAMED_CREDENTIAL = 'image_named_credential';
// Kept in sync with the Java constant PAYLOAD_NAMED_CRED_DELIMITER.
public static final String PAYLOAD_NAMED_CRED_DELIMITER = '::';
// OPTIONAL (required = false): the synchronous executeCallout path used by the
// Invocable Action resolves the Named Credential from the payload prefix and
// never reads this attribute. The async getCalloutRequest path still reads it
// (with a default fallback) when configured.
public static final industriesintegrationfwk.ApexProviderAttr IMAGE_NAMED_CRED_ATTR =
new industriesintegrationfwk.ApexProviderAttr(
'Named Credential', NAME_CRED_DEV_NAME, IMAGE_NAMED_CREDENTIAL, false, 'String');
class ImageFetchException extends Exception { }
// ---- ProcessIntegrationProvider — async / process flow path ----
global static industriesintegrationfwk.IntegrationCalloutRequest getCalloutRequest(
String requestGuid, Map<String, String> params, Map<String, Object> attributes) {
if (params == null || params.isEmpty() || String.isBlank(params.get(DISPLAY_URL_PARAM))) {
throw new IllegalArgumentException('displayUrl parameter is required!');
}
String displayUrl = params.get(DISPLAY_URL_PARAM);
// Attribute is optional; fall back to the default Named Credential dev name.
String namedCred = (attributes != null && attributes.get(NAME_CRED_DEV_NAME) != null)
? (String) attributes.get(NAME_CRED_DEV_NAME)
: IMAGE_NAMED_CREDENTIAL;
HttpRequest req = new HttpRequest();
req.setMethod(HTTP_GET);
req.setEndpoint(CALLOUT_KEY + namedCred + SLASH + stripLeadingSlash(displayUrl));
industriesintegrationfwk.IntegrationCalloutRequest intgReq = new industriesintegrationfwk.IntegrationCalloutRequest();
intgReq.addHttpRequest(req);
return intgReq;
}
global static industriesintegrationfwk.IntegrationCalloutResponse getCalloutResponse(
List<HttpResponse> httpResponseList, Object state, Map<String, Object> attributes) {
if (httpResponseList == null || httpResponseList.size() < 1) {
throw new IllegalArgumentException('Http Response List is empty!');
}
return buildResponse(httpResponseList.get(0));
}
// ---- ServiceIntegrationProvider — synchronous path used by the IA ----
global static industriesintegrationfwk.IntegrationCalloutResponse executeCallout(
String requestGuid, String inputRecordId, String payload, Map<String, Object> attributes) {
if (String.isBlank(payload)) {
throw new ImageFetchException('displayUrl payload is required');
}
// The Java caller packs the resolved Named Credential dev name into the
// payload as "<namedCredentialDevName>::<displayUrl>".
Integer delimIdx = payload.indexOf(PAYLOAD_NAMED_CRED_DELIMITER);
if (delimIdx <= 0) {
throw new ImageFetchException(
'Payload must be formatted as "<namedCredentialDevName>' + PAYLOAD_NAMED_CRED_DELIMITER + '<displayUrl>"');
}
String namedCred = payload.substring(0, delimIdx);
String displayUrl = payload.substring(delimIdx + PAYLOAD_NAMED_CRED_DELIMITER.length());
if (String.isBlank(namedCred) || String.isBlank(displayUrl)) {
throw new ImageFetchException('Named Credential developer name and displayUrl are both required');
}
HttpRequest request = new HttpRequest();
request.setMethod(HTTP_GET);
request.setEndpoint(CALLOUT_KEY + namedCred + SLASH + stripLeadingSlash(displayUrl));
HttpResponse response = new Http().send(request);
return buildResponse(response);
}
global static List<industriesintegrationfwk.ApexProviderAttr> getProviderAttributes() {
return new List<industriesintegrationfwk.ApexProviderAttr>{ IMAGE_NAMED_CRED_ATTR };
}
// ---- Helpers ----
private static industriesintegrationfwk.IntegrationCalloutResponse buildResponse(HttpResponse response) {
Integer statusCode = response.getStatusCode();
Boolean isSuccess = (statusCode != null && statusCode >= 200 && statusCode < 300);
Map<String, Object> httpResponseData = new Map<String, Object>{
STATUS_CODE_KEY => statusCode,
CONTENT_TYPE_KEY => response.getHeader(CONTENT_TYPE_HEADER),
BODY_KEY => isSuccess ? EncodingUtil.base64Encode(response.getBodyAsBlob()) : ''
};
Map<String, Object> returnValue = new Map<String, Object>{ HTTP_RESPONSE_KEY => httpResponseData };
industriesintegrationfwk.IntegrationCalloutResponse intgResp =
new industriesintegrationfwk.IntegrationCalloutResponse(isSuccess);
intgResp.setReturnValue(returnValue);
intgResp.setResponseCode(statusCode);
if (!isSuccess) {
String body = response.getBody();
String snippet = (body != null && body.length() > 200) ? body.substring(0, 200) + '...' : body;
intgResp.setErrorMessage('HTTP callout failed with status code: ' + statusCode
+ (snippet != null ? '. Response: ' + snippet : ''));
}
return intgResp;
}
private static String stripLeadingSlash(String path) {
if (path == null) return '';
return path.startsWith(SLASH) ? path.substring(1) : path;
}
}
Questo articolo ha risolto il problema?
Facci sapere, così possiamo migliorare!
