I'm using the PageSpeed API in my Java application. It is working fine, but since I need to check a lot of URLs I would need to do some batching. I've been looking online and in their documentation but I can't find anything on how to do it. Does anyone know?
I am connecting with a normal HTTP Request because it turned out to be faster than using their package, but I assume for batching maybe their package is better, so here is the code I use for both scenarios:
HTTP Request:
String call = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" + SITE + "&key=" + GOOGLE_KEY + "&strategy=" + DEVICE;
URL url = new URL(call);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(response.toString());
PageSpeed Java Package:
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
httpRequest.setReadTimeout(60000); // 60 seconds
}
};
Pagespeedonline p = new Pagespeedonline.Builder(transport, jsonFactory, httpRequestInitializer).setApplicationName("APP_NAME").build();
Pagespeedonline.Pagespeedapi.Runpagespeed runpagespeed = p.pagespeedapi().runpagespeed(SITE).setKey(GOOGLE_KEY).setStrategy(DEVICE);
PagespeedApiPagespeedResponseV5 response = runpagespeed.execute();
Both are working fine, I just need to know how to integrate batching in any of the two.
Can anyone help me?
Thank you very much!
I managed to do it but I forgot to share it, here it is:
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
httpRequest.setReadTimeout(60000); // 60 seconds
}
};
BatchRequest batch = p.batch(httpRequestInitializer);
Pagespeedonline p = new Pagespeedonline.Builder(transport, jsonFactory, httpRequestInitializer).setApplicationName(APP_NAME).build();
//Loop to add as many as you want
for (int i = 0; i < urls.size(); ++i) {
Pagespeedonline.Pagespeedapi.Runpagespeed check = p.pagespeedapi().runpagespeed(urls.get(i)).setKey(GOOGLE_KEY).setStrategy(DEVICE);
check.queue(batch, callback);
}
batch.execute();
And this is the callback:
JsonBatchCallback<PagespeedApiPagespeedResponseV5> callback = new JsonBatchCallback<PagespeedApiPagespeedResponseV5>() {
public void onSuccess(PagespeedApiPagespeedResponseV5 response, HttpHeaders responseHeaders) {
//ON SUCCESS
}
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
//ON FAILURE
}
};