I need to do a callout to Google Analytics in Salesforce. Based on the documentation, I can't bulkify the request with the records involved, because I can only have one date range.
Is doing a http request in a for loop problematic? I would do this loop in a batch.
for(Account a : accounts) {
HttpRequest req = new HttpRequest();
req.setEndpoint('https://analyticsreporting.googleapis.com/v4/reports:batchGet');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Authorization', 'Bearer ' + accessToken);
req.setMethod('POST');
req.setBody(requestBody);
HTTPResponse res;
if(Test.isRunningTest()) {
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGAData());
res = new Http().send(req);
} else {
res = new Http().send(req);
}
if(res.getStatusCode() == 200) {
// parse response
} else {
// throw error
}
}