Created
November 18, 2022 17:39
-
-
Save sfboss/c3955c197842ad108f394d44924324b8 to your computer and use it in GitHub Desktop.
apex to accompany the google page speed lwc
This file contains hidden or 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
| public with sharing class GoogleAPIService { | |
| @AuraEnabled | |
| public static String sendPayloadToApex(String theData) { | |
| GoogleAPI_PageSpeedInsights_resp res = GoogleAPI_PageSpeedInsights_resp.parse( | |
| theData | |
| ); | |
| return JSON.serialize(res); | |
| } | |
| public static final string DEVELOPER_TOKEN = ''; | |
| public static final string LOGIN_CUSTOMER_ID = 'ENTER'; | |
| public static final string API_KEY = 'ENTER'; | |
| public static final string CUSTOM_SEARCH_ID = 'ENTER'; | |
| public static final string NAMED_CREDENTIAL_GOOGLE_ADS = 'Google_Drive_Files_Export'; | |
| public static final string NAMED_CREDENTIAL_GOOGLE_DRIVE = '4b2f49502adf4a81f'; | |
| public static String endpointCustomSearch = '?cx={0}&q={1}&start={2}'; | |
| public static String endpointTenor = 'https://tenor.googleapis.com/v2/search?key={0}&q={1}&limit={2}&client_key={3}'; | |
| public static String endpointSearchConsole = 'https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?key={0}'; | |
| public static String endPointPageSpeedInsights = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={0}?key={1}'; | |
| public static String endpointSearchConsoleLegacy = '/sites/{0}/searchAnalytics/query'; | |
| public enum ENDPOINT_TYPES { | |
| CUSTOM_SEARCH, | |
| TENOR, | |
| GOOGLE_ADS, | |
| GOOGLE_SUGGEST, | |
| GOOGLE_SEARCH_CONSOLE, | |
| GOOGLE_SEARCH_CONSOLE_LEGACY, | |
| GOOGLE_PAGESPEED_INSIGHTS | |
| } | |
| public static Map<String, String> buildHeaders( | |
| String contentType, | |
| String developerToken, | |
| String loginCustomerId, | |
| String authorization | |
| ) { | |
| return new Map<String, String>{ | |
| 'Content-Type' => contentType, | |
| 'developer-token' => developerToken, | |
| 'login-customer-id' => '3399365278', | |
| 'Authorization' => authorization | |
| }; | |
| } | |
| public static String doGoogleCalloutNamedCredentials( | |
| String namedCredential, | |
| String theSeedKeyword, | |
| Map<String, String> headers, | |
| Boolean hasBody, | |
| Integer startIndex | |
| ) { | |
| // prepare request body when needed | |
| GoogleAPI_generateKeywordIdeas_req theReq; | |
| String endpointTransposed = ''; | |
| Integer theTimeout = 120000; | |
| String requestBodyString = ''; | |
| HttpRequest httpReq = new HttpRequest(); | |
| HttpResponse httpRes = new HttpResponse(); | |
| if (namedCredential == 'Google_API_Ads') { | |
| httpReq.setHeader('developer-token', 'WnCBhF3hBoT5doT5c0xswA'); | |
| httpReq.setHeader('login-customer-id', '3399365278'); | |
| httpReq.setHeader('Content-Type', 'application/json'); | |
| httpReq.setMethod('POST'); | |
| theReq = new GoogleAPI_generateKeywordIdeas_req( | |
| ); | |
| endpointTransposed = getEndpointURL( | |
| ENDPOINT_TYPES.GOOGLE_ADS, | |
| new Map<String, String>{ 'login-customer-id' => LOGIN_CUSTOMER_ID } | |
| ); | |
| httpReq.setBody(JSON.serialize(theReq, true)); | |
| httpReq.setHeader( | |
| 'Content-Length', | |
| String.valueOf(JSON.serialize(theReq).length()) | |
| ); | |
| httpReq.setEndpoint('callout:' + namedCredential + endpointTransposed); | |
| //requestBodyString = theReq.getRequestBody(theSeedKeyword); | |
| } else if (namedCredential == 'Google_API_CloudSearch') { | |
| httpReq.setMethod('GET'); | |
| httpReq.setHeader('Authorization', '{$Credential.OAuthToken}'); | |
| httpReq.setHeader('Content-Type', 'application/json'); | |
| endpointTransposed = getEndpointURL( | |
| ENDPOINT_TYPES.CUSTOM_SEARCH, | |
| new Map<String, String>{ | |
| 'query' => theSeedKeyword, | |
| 'startIndex' => String.valueOf(startIndex) | |
| } | |
| ); | |
| } | |
| httpReq.setEndpoint('callout:' + namedCredential + endpointTransposed); | |
| httpReq.setTimeout(theTimeout); | |
| httpRes = (new Http()).send(httpReq); | |
| String theResponseBody = httpRes.getBody(); | |
| return theResponseBody; | |
| } | |
| @AuraEnabled | |
| public static GoogleAPI_PageSpeedInsights_resp doGooglePageSpeedInsightsCallout( | |
| String theURL | |
| ) { | |
| String theURLEncoded = getEndpointURL( | |
| ENDPOINT_TYPES.GOOGLE_PAGESPEED_INSIGHTS, | |
| new Map<String, String>{ 'theSiteURL' => 'qqb.com' } | |
| ); | |
| Http http = new Http(); | |
| HttpRequest httpReq = new HttpRequest(); | |
| HttpResponse httpRes = new HttpResponse(); | |
| httpReq.setEndpoint(theURLEncoded); | |
| httpReq.setHeader('Content-Type', 'application/json'); | |
| httpReq.setMethod('GET'); | |
| httpReq.setTimeout(120000); | |
| httpRes = (new Http()).send(httpReq); | |
| String theRespJSON = httpRes.getBody(); | |
| //system.debug(theRespJSON); | |
| theRespJSON = theRespJSON.replaceAll('group', 'groupp'); | |
| GoogleAPI_PageSpeedInsights_resp resp = GoogleAPI_PageSpeedInsights_resp.parse( | |
| theRespJSON | |
| ); | |
| //GoogleAPI_Tenor_search_resp theResponse = (GoogleAPI_Tenor_search_resp) JSON.deserialize( | |
| // theRespJSON, | |
| //GoogleAPI_Tenor_search_resp.class | |
| //); | |
| return resp; | |
| } | |
| @AuraEnabled | |
| public static GoogleAPI_SearchConsoleLegacy_resp doLegacyGoogleSearchConsoleCallout() { | |
| String theURLEncoded = getEndpointURL( | |
| ENDPOINT_TYPES.GOOGLE_SEARCH_CONSOLE_LEGACY, | |
| new Map<String, String>{ 'theSiteURL' => 'qqb.com' } | |
| ); | |
| Http http = new Http(); | |
| HttpRequest httpReq = new HttpRequest(); | |
| HttpResponse httpRes = new HttpResponse(); | |
| httpReq.setEndpoint('callout:Google_Search_Console' + theURLEncoded); | |
| httpReq.setHeader('Content-Type', 'application/json'); | |
| httpReq.setMethod('POST'); | |
| httpReq.setBody( | |
| JSON.serialize( | |
| new GoogleAPI_SearchConsoleLegacy_req( | |
| '2022-06-01', | |
| '2022-07-21', | |
| new List<String>{ 'country', 'device' } | |
| ) | |
| ) | |
| ); | |
| httpReq.setTimeout(120000); | |
| httpRes = (new Http()).send(httpReq); | |
| String theRespJSON = httpRes.getBody(); | |
| //system.debug(theRespJSON); | |
| GoogleAPI_SearchConsoleLegacy_resp resp = GoogleAPI_SearchConsoleLegacy_resp.parse( | |
| theRespJSON | |
| ); | |
| //GoogleAPI_Tenor_search_resp theResponse = (GoogleAPI_Tenor_search_resp) JSON.deserialize( | |
| // theRespJSON, | |
| //GoogleAPI_Tenor_search_resp.class | |
| //); | |
| return resp; | |
| } | |
| public static String getEndpointURL( | |
| ENDPOINT_TYPES endPoint, | |
| Map<String, String> paramsMap | |
| ) { | |
| String theURL = ''; | |
| String theSiteURL = paramsMap.get('theSiteURL'); | |
| String theQueryString = paramsMap.get('query') != null | |
| ? EncodingUtil.urlEncode(paramsMap.get('query'), 'UTF-8') | |
| : ''; | |
| //if (theQueryString != '' && theQueryString != null){ | |
| switch on endPoint { | |
| when GOOGLE_PAGESPEED_INSIGHTS { | |
| theURL = endPointPageSpeedInsights; | |
| String[] pathParams = new List<String>{ | |
| EncodingUtil.urlEncode(theSiteURL, 'UTF-8'), | |
| API_KEY | |
| }; | |
| theURL = String.format(theURL, pathParams); | |
| } | |
| when else { | |
| } | |
| } | |
| // } | |
| return theURL; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment