forked from community/device-mgt-core
parent
a668d3e364
commit
37bef26862
@ -0,0 +1,108 @@
|
||||
package org.wso2.carbon.device.mgt.jaxrs.service.impl;
|
||||
|
||||
|
||||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
||||
import com.google.api.client.googleapis.json.GoogleJsonError;
|
||||
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
|
||||
import com.google.api.client.http.javanet.NetHttpTransport;
|
||||
import com.google.api.services.sheets.v4.Sheets;
|
||||
import com.google.api.services.sheets.v4.model.AppendValuesResponse;
|
||||
import com.google.api.services.sheets.v4.model.ValueRange;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.osgi.service.application.ApplicationDescriptor.APPLICATION_NAME;
|
||||
import static org.wso2.carbon.device.mgt.jaxrs.service.impl.GoogleSheet.JSON_FACTORY;
|
||||
import static org.wso2.carbon.device.mgt.jaxrs.service.impl.GoogleSheet.getCredentials;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
@RestController
|
||||
public class ApiResponseTime {
|
||||
|
||||
public static Object getstartTime() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object getendTime() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object getelapsedTime() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void appendDetails(long startTime, long endTime, long elapsedTime)
|
||||
throws GeneralSecurityException, IOException, URISyntaxException {
|
||||
final String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
||||
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
|
||||
|
||||
Sheets service =
|
||||
new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
|
||||
.setApplicationName(APPLICATION_NAME)
|
||||
.build();
|
||||
|
||||
// Create a list of values to append to the sheet
|
||||
List<Object> newList1 = new ArrayList<>(Arrays.asList(startTime, endTime, elapsedTime));
|
||||
List<List<Object>> values = new ArrayList<>(Arrays.asList(newList1));
|
||||
|
||||
// Append the values to the sheet
|
||||
AppendValuesResponse result = null;
|
||||
try {
|
||||
ValueRange valueRange = new ValueRange().setValues(values);
|
||||
result = service.spreadsheets().values().append(spreadsheetId, "A2", valueRange)
|
||||
.setValueInputOption("RAW").execute();
|
||||
System.out.printf("%d cells appended.", result.getUpdates().getUpdatedCells());
|
||||
} catch (GoogleJsonResponseException e) {
|
||||
GoogleJsonError error = e.getDetails();
|
||||
if (((GoogleJsonError) error).getCode() == 404) {
|
||||
System.out.printf("Spreadsheet not found with id '%s'.\n", spreadsheetId);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/my-api")
|
||||
public String myApi() {
|
||||
// Set the URL for the API call
|
||||
String url = "/next-pending/operation/{type}/{id}";
|
||||
|
||||
try {
|
||||
// Record the start time
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
// Make the API call using the RestTemplate
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String response = restTemplate.getForObject(url, String.class);
|
||||
|
||||
// Record the end time
|
||||
long endTime = System.currentTimeMillis();
|
||||
|
||||
// Calculate the elapsed time
|
||||
long elapsedTime = endTime - startTime;
|
||||
|
||||
// Print the elapsed time in milliseconds
|
||||
System.out.println("Start Time: " + startTime + " ms");
|
||||
System.out.println("End Time: " + endTime + " ms");
|
||||
System.out.println("Elapsed time: " + elapsedTime + " ms");
|
||||
|
||||
// Append the API call details to a Google Sheet
|
||||
appendDetails(startTime, endTime, elapsedTime);
|
||||
|
||||
return response;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "Error occurred: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package org.wso2.carbon.device.mgt.jaxrs.service.impl;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.http.javanet.NetHttpTransport;
|
||||
import com.google.api.client.json.JsonFactory;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.google.api.services.sheets.v4.SheetsScopes;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class GoogleSheet {
|
||||
|
||||
private static final String APPLICATION_NAME = "ApiCall";
|
||||
static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||
private static final String TOKENS_DIRECTORY_PATH = "tokens/path";
|
||||
private static final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS,SheetsScopes.DRIVE);
|
||||
|
||||
static GoogleCredential getCredentials(final NetHttpTransport HTTP_TRANSPORT)
|
||||
throws IOException, GeneralSecurityException {
|
||||
GoogleCredential credential = new GoogleCredential.Builder()
|
||||
.setTransport(HTTP_TRANSPORT)
|
||||
.setJsonFactory(JSON_FACTORY)
|
||||
.setServiceAccountId("apicall@apicall-382608.iam.gserviceaccount.com")
|
||||
.setServiceAccountPrivateKeyFromP12File(new File("/home/entgra/MyProject/device-mgt-core/components/device-mgt/org.wso2.carbon.device.mgt.api/target/credentials.p12")).setServiceAccountScopes(Collections.singleton(SheetsScopes.SPREADSHEETS)).build();
|
||||
credential.refreshToken();
|
||||
return credential;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue