Created
August 18, 2017 09:42
-
-
Save shrutis22/8f601e36933d0f8533cf22bf9bbdd7fc to your computer and use it in GitHub Desktop.
This class is created to make requests to various Einstein Endpoints.
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
/** | |
* This class is created to make requests to | |
* various Einstein Endpoints. | |
* | |
* @author Shruti Sridharan | |
* @since 10/08/2017 | |
* @revisions N/A | |
**/ | |
public class EinsteinAPI { | |
public String tokenEndpoint { | |
get { | |
Einstein_API_Settings__c settings = Einstein_API_Settings__c.getInstance( UserInfo.getOrganizationId() ); | |
return settings.Token_Endpoint__c; | |
} | |
} | |
public Decimal tokenExpirationSeconds { | |
get { | |
Einstein_API_Settings__c settings = Einstein_API_Settings__c.getInstance( UserInfo.getOrganizationId() ); | |
return settings.Token_Expiration_Seconds__c; | |
} | |
} | |
public String registeredEmail { | |
get { | |
Einstein_API_Settings__c settings = Einstein_API_Settings__c.getInstance( UserInfo.getOrganizationId() ); | |
return settings.Registered_Email__c; | |
} | |
} | |
public String sentimentEndpoint { | |
get { | |
Einstein_API_Settings__c settings = Einstein_API_Settings__c.getInstance( UserInfo.getOrganizationId() ); | |
return settings.Sentiment_Endpoint__c; | |
} | |
} | |
public String sentimentModelId { | |
get { | |
Einstein_API_Settings__c settings = Einstein_API_Settings__c.getInstance( UserInfo.getOrganizationId() ); | |
return settings.Sentiment_Model_Id__c; | |
} | |
} | |
/** | |
* This method is created to make a call | |
* to the Token Endpoint and get the token | |
* which will help us to make request to | |
* other Endpoints of Einstein Services. | |
* | |
* @return String Returns the access token of the Org | |
*/ | |
public String getAccessToken() { | |
ContentVersion base64Content = [ | |
SELECT Title | |
,VersionData | |
FROM ContentVersion | |
WHERE Title = 'einstein_platform' | |
OR Title = 'predictive_services' | |
ORDER BY Title | |
LIMIT 1 | |
]; | |
String keyContents = base64Content.VersionData.tostring(); | |
keyContents = keyContents.replace( '-----BEGIN RSA PRIVATE KEY-----', '' ); | |
keyContents = keyContents.replace( '-----END RSA PRIVATE KEY-----', '' ); | |
keyContents = keyContents.replace( '\n', '' ); | |
JWT jwt = new JWT( 'RS256' ); | |
jwt.pkcs8 = keyContents; | |
jwt.iss = 'developer.force.com'; | |
jwt.sub = registeredEmail; | |
jwt.aud = tokenEndpoint; | |
jwt.exp = String.valueOf( tokenExpirationSeconds ); | |
String access_token = JWTBearerFlow.getAccessToken( tokenEndpoint, jwt ); | |
return access_token; | |
} | |
/** | |
* This method is created to make call | |
* to the Sentiment Endpoint and get | |
* the Sentiment of the block of text. | |
* | |
* @param text Block of text whose Sentiment has to be analysed | |
* | |
* @return SentimentAnalysisResponse Returns an instance of SentimentAnalysisResponse class | |
*/ | |
public SentimentAnalysisResponse findSentiment( String text ) { | |
String key = getAccessToken(); | |
Http http = new Http(); | |
HttpRequest req = new HttpRequest(); | |
req.setMethod( 'POST' ); | |
req.setEndpoint( sentimentEndpoint ); | |
req.setHeader( 'Authorization', 'Bearer ' + key ); | |
req.setHeader( 'Content-type', 'application/json' ); | |
String body = '{\"modelId\":\"'+ sentimentModelId + '\",\"document\":\"' + text + '\"}'; | |
req.setBody( body ); | |
HTTPResponse res = http.send( req ); | |
SentimentAnalysisResponse resp = ( SentimentAnalysisResponse ) JSON.deserialize( res.getBody(), SentimentAnalysisResponse.class ); | |
return resp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment