-
-
Save AdityaPavanVempati/8f4e092d056867246a59 to your computer and use it in GitHub Desktop.
This is the Apex Class of GoogleUrlShortener,which it shortener the Url's
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 exposes methods | |
* that calls the google URL | |
* shortener API to Shorten long | |
* URL's | |
* | |
* @author Pavan Vempati | |
* @since 22/02/2016 | |
* @version 1.0 | |
*/ | |
global class GoogleUrlShortener { | |
/** | |
* This Method calls the URL shortener | |
* API with the encoded key | |
* grabs the Shortened URL from the Response. | |
* | |
* @param longURL accepts the long URL that needs to be shorened | |
* @since 22/02/2016 | |
*/ | |
webService static void shortenURL( String longURL, String bookmarkId ) { | |
/** | |
* Fetching the Encoded Key | |
* and the google URL shortener Endpoint | |
* from the encoded key | |
* by using custom setting | |
*/ | |
String encKey = Google_API_Setting__c.getInstance( 'URL Shortener Key' ).Value__c; | |
String endPoint = Google_API_Setting__c.getInstance( 'URL Shortener Endpoint' ).Value__c; | |
endPoint = endPoint.replace( '{YOUR_API_KEY}',encKey ); | |
String body = '{"longUrl" : "' + longURL+ '"}'; | |
Http http = new Http(); | |
HttpRequest httpReq = new HttpRequest(); | |
HttpResponse httpResp = new HttpResponse(); | |
httpReq.setBody( body ); | |
httpReq.setMethod( 'POST' ); | |
httpReq.setHeader( 'Content-Type', 'application/json' ); | |
httpReq.setEndpoint( endPoint ); | |
httpResp = http.send( httpReq ); | |
JSONParser parser = JSON.createParser( HttpResp.getBody() ); | |
parser.nextToken(); | |
parser.nextToken(); | |
parser.nextToken(); | |
parser.nextValue(); | |
String sURL = parser.getText(); | |
Bookmark__c bm = new Bookmark__c(); | |
bm.Id = bookmarkId; | |
bm.Short_URL__c = sURL; | |
UPDATE bm; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment