Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void validate() {
}
}

String generateOpenApiSpec() throws Exception {
String generateOpenApiSpec() throws IOException, InterruptedException {
String url =
String.format(
"https://%s-integrations.googleapis.com/v1/projects/%s/locations/%s:generateOpenApiSpec",
Expand Down Expand Up @@ -179,7 +179,7 @@ String generateOpenApiSpec() throws Exception {
httpClient.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());

if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new Exception("Error fetching OpenAPI spec. Status: " + response.statusCode());
throw new IOException("Error fetching OpenAPI spec. Status: " + response.statusCode());
}
return response.body();
}
Expand Down Expand Up @@ -343,7 +343,7 @@ ObjectNode getOpenApiSpecForConnection(String toolName, String toolInstructions)
return connectorSpec;
}

String getOperationIdFromPathUrl(String openApiSchemaString, String pathUrl) throws Exception {
String getOperationIdFromPathUrl(String openApiSchemaString, String pathUrl) throws IOException {
JsonNode topLevelNode = objectMapper.readTree(openApiSchemaString);
JsonNode specNode = topLevelNode.path("openApiSpec");
if (specNode.isMissingNode() || !specNode.isTextual()) {
Expand Down Expand Up @@ -372,7 +372,7 @@ String getOperationIdFromPathUrl(String openApiSchemaString, String pathUrl) thr
}
}
}
throw new Exception("Could not find operationId for pathUrl: " + pathUrl);
throw new IOException("Could not find operationId for pathUrl: " + pathUrl);
}

ConnectionsClient createConnectionsClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public class IntegrationConnectorTool extends BaseTool {
this.credentialsHelper = Preconditions.checkNotNull(credentialsHelper);
}

Schema toGeminiSchema(String openApiSchema, String operationId) throws Exception {
Schema toGeminiSchema(String openApiSchema, String operationId) throws IOException {
String resolvedSchemaString = getResolvedRequestSchemaByOperationId(openApiSchema, operationId);
return Schema.fromJson(resolvedSchemaString);
}
Expand All @@ -148,7 +148,7 @@ public Optional<FunctionDeclaration> declaration() {
.parameters(parametersSchema)
.build();
return Optional.of(declaration);
} catch (Exception e) {
} catch (IOException e) {
logger.error("Failed to get OpenAPI spec", e);
return Optional.empty();
}
Expand All @@ -175,20 +175,21 @@ public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContex
try {
String response = executeIntegration(args);
return ImmutableMap.of("result", response);
} catch (Exception e) {
} catch (IOException | InterruptedException e) {
logger.error("Failed to execute integration", e);
return ImmutableMap.of("error", e.getMessage());
}
});
}

private String executeIntegration(Map<String, Object> args) throws Exception {
private String executeIntegration(Map<String, Object> args)
throws IOException, InterruptedException {
String url = String.format("https://integrations.googleapis.com%s", this.pathUrl);
String jsonRequestBody;
try {
jsonRequestBody = objectMapper.writeValueAsString(args);
} catch (IOException e) {
throw new Exception("Error converting args to JSON: " + e.getMessage(), e);
throw new IOException("Error converting args to JSON: " + e.getMessage(), e);
}
Credentials credentials = credentialsHelper.getGoogleCredentials(this.serviceAccountJson);
HttpRequest.Builder requestBuilder =
Expand All @@ -203,7 +204,7 @@ private String executeIntegration(Map<String, Object> args) throws Exception {
httpClient.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());

if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new Exception(
throw new IOException(
"Error executing integration. Status: "
+ response.statusCode()
+ " , Response: "
Expand All @@ -212,7 +213,7 @@ private String executeIntegration(Map<String, Object> args) throws Exception {
return response.body();
}

String getOperationIdFromPathUrl(String openApiSchemaString, String pathUrl) throws Exception {
String getOperationIdFromPathUrl(String openApiSchemaString, String pathUrl) throws IOException {
JsonNode topLevelNode = objectMapper.readTree(openApiSchemaString);
JsonNode specNode = topLevelNode.path("openApiSpec");
if (specNode.isMissingNode() || !specNode.isTextual()) {
Expand Down Expand Up @@ -254,11 +255,11 @@ String getOperationIdFromPathUrl(String openApiSchemaString, String pathUrl) thr
}
}
}
throw new Exception("Could not find operationId for pathUrl: " + pathUrl);
throw new IOException("Could not find operationId for pathUrl: " + pathUrl);
}

private String getResolvedRequestSchemaByOperationId(
String openApiSchemaString, String operationId) throws Exception {
String openApiSchemaString, String operationId) throws IOException {
JsonNode topLevelNode = objectMapper.readTree(openApiSchemaString);
JsonNode specNode = topLevelNode.path("openApiSpec");
if (specNode.isMissingNode() || !specNode.isTextual()) {
Expand All @@ -268,13 +269,13 @@ private String getResolvedRequestSchemaByOperationId(
JsonNode rootNode = objectMapper.readTree(specNode.asText());
JsonNode operationNode = findOperationNodeById(rootNode, operationId);
if (operationNode == null) {
throw new Exception("Could not find operation with operationId: " + operationId);
throw new IOException("Could not find operation with operationId: " + operationId);
}
JsonNode requestSchemaNode =
operationNode.path("requestBody").path("content").path("application/json").path("schema");

if (requestSchemaNode.isMissingNode()) {
throw new Exception("Could not find request body schema for operationId: " + operationId);
throw new IOException("Could not find request body schema for operationId: " + operationId);
}

JsonNode resolvedSchema = resolveRefs(requestSchemaNode, rootNode);
Expand Down Expand Up @@ -355,7 +356,7 @@ private JsonNode resolveRefs(JsonNode currentNode, JsonNode rootNode) {
}

private String getOperationDescription(String openApiSchemaString, String operationId)
throws Exception {
throws IOException {
JsonNode topLevelNode = objectMapper.readTree(openApiSchemaString);
JsonNode specNode = topLevelNode.path("openApiSpec");
if (specNode.isMissingNode() || !specNode.isTextual()) {
Expand Down