Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ashera96 committed Mar 21, 2024
1 parent 11c2fec commit c3c0959
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 351 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,10 @@ public enum ExceptionCodes implements ErrorHandler {
KEY_MANAGER_RESTRICTED_FOR_USER(902013, "Unauthorized Access to Key Manager", 403, "Key Manager is Restricted for this user"),
// Admin portal get apis and api provider change related errors
CHANGE_API_PROVIDER_FAILED(903011, "Error while changing the API provider", 500, "Error while changing the API provider in the registry or DB"),
GET_SEARCH_APIS_IN_ADMIN_FAILED(903012, "Error while getting the apis", 500, "Error while getting/searching the apis from registry");
GET_SEARCH_APIS_IN_ADMIN_FAILED(903012, "Error while getting the apis", 500, "Error while getting/searching the apis from registry"),

// AI service invocation related exceptions
INVALID_RESPONSE_FROM_AI_SERVICE(903100, "Invalid response from AI service", 500, "Error while invoking AI service. %s", false);

private final long errorCode;
private final String errorMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,6 @@ public final class APIConstants {
public static final String API_CHAT_ENABLED = API_CHAT + "Enabled";
public static final String API_CHAT_AUTH_TOKEN = API_CHAT + "AuthToken";
public static final String API_CHAT_ENDPOINT = API_CHAT + "Endpoint";
public static final String API_CHAT_HEALTH_RESOURCE = "/health"; // "/api-chat/health"
public static final String API_CHAT_PREPARE_RESOURCE = "/prepare"; // "/api-chat/prepare"
public static final String API_CHAT_EXECUTE_RESOURCE = "/chat"; // "/api-chat/chat"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
Expand Down Expand Up @@ -10408,37 +10407,7 @@ public static boolean isAuthTokenProvidedForAIFeatures() {
return true;
}

/**
* This method is used for AI Service health check purposes. This will be utilized by API-Chat feature and
* Marketplace-Assistant feature
*
* @param endpointConfigName Config name to retrieve the AI Service URL
* @param resource Resource that we should forward the request to
* @return CloseableHttpResponse of the GET call
* @throws APIManagementException
*/
public static CloseableHttpResponse getAIServiceHealth(String endpointConfigName, String resource)
throws APIManagementException {

APIManagerConfiguration config = ServiceReferenceHolder.getInstance().
getAPIManagerConfigurationService().getAPIManagerConfiguration();
String endpoint = config.getFirstProperty(endpointConfigName);

try{
HttpGet healthGet = new HttpGet(endpoint + resource);
URL url = new URL(endpoint);
int port = url.getPort();
String protocol = url.getProtocol();
HttpClient httpClient = APIUtil.getHttpClient(port, protocol);
return executeHTTPRequest(healthGet, httpClient);
} catch (MalformedURLException e) {
throw new APIManagementException("Invalid/malformed URL encountered. URL: " + endpoint, e);
} catch (APIManagementException | IOException e) {
throw new APIManagementException("Error encountered while connecting to service", e);
}
}

public static CloseableHttpResponse invokeAIService(String endpointConfigName, String authTokenConfigName,
public static String invokeAIService(String endpointConfigName, String authTokenConfigName,
String resource, String payload, String requestId) throws APIManagementException {

APIManagerConfiguration config = ServiceReferenceHolder.getInstance().
Expand All @@ -10458,7 +10427,17 @@ public static CloseableHttpResponse invokeAIService(String endpointConfigName, S
String protocol = url.getProtocol();
HttpClient httpClient = APIUtil.getHttpClient(port, protocol);

return executeHTTPRequest(preparePost, httpClient);
CloseableHttpResponse response = executeHTTPRequest(preparePost, httpClient);
int statusCode = response.getStatusLine().getStatusCode();
String responseStr = EntityUtils.toString(response.getEntity());
if (statusCode != HttpStatus.SC_CREATED) {
return responseStr;
} else if (statusCode != HttpStatus.SC_UNAUTHORIZED) {

}else {
throw new APIManagementException("Unexpected response detected from the AI service." + responseStr,
ExceptionCodes.INVALID_RESPONSE_FROM_AI_SERVICE);
}
} catch (MalformedURLException e) {
throw new APIManagementException("Invalid/malformed URL encountered. URL: " + endpoint, e);
} catch (APIManagementException | IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3578,31 +3578,7 @@ paths:
######################################################
# The "API Chat Feature" resource APIs
######################################################
'/ai/api-chat/health':

#-----------------------------------------------------
# Check the health status of AI service
#-----------------------------------------------------
get:
summary: Heath check endpoint
security:
- OAuth2Security: []
description: |
Get the health status of API Chat AI service
operationId: getApiChatHealth
tags:
- API Chat
responses:
200:
description: |
OK.
Health status is returned.
500:
description: |
Internal Server Error.
An error occurred while checking the health status of API Chat service
'/ai/api-chat/prepare':
/apis/{apiId}/api-chat/prepare:

#-----------------------------------------------------
# Prepare API Chat for user queries by feeding spec
Expand All @@ -3615,8 +3591,8 @@ paths:
Processing the OpenAPI specification to extract the API endpoint definitions and generate sample queries
operationId: apiChatPrepare
parameters:
- $ref: '#/components/parameters/apiId'
- $ref: '#/components/parameters/apiChatRequestId'
- $ref: '#/components/parameters/apiId-Q'
tags:
- API Chat
responses:
Expand All @@ -3630,12 +3606,14 @@ paths:
$ref: '#/components/schemas/ApiChatPreparationResponse'
400:
$ref: '#/components/responses/BadRequest'
401:
$ref: '#/components/responses/Unauthorized'
500:
description: |
Internal Server Error.
An error occurred while preparing the API Chat service.
'/ai/api-chat/execute':
/apis/{apiId}/api-chat/execute:

#-----------------------------------------------------
# Execute test specified via API Chat
Expand All @@ -3649,6 +3627,7 @@ paths:
invoked alongside the inputs such as parameters, payloads, etc. while caching the progress.
operationId: apiChatExecute
parameters:
- $ref: '#/components/parameters/apiId'
- $ref: '#/components/parameters/apiChatRequestId'
requestBody:
description: |
Expand All @@ -3670,6 +3649,8 @@ paths:
$ref: '#/components/schemas/ApiChatExecuteResponse'
400:
$ref: '#/components/responses/BadRequest'
401:
$ref: '#/components/responses/Unauthorized'
500:
description: |
Internal Server Error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ public Response deleteComment(String commentId, String apiId, String ifMatch, Me
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String errorMessage = "Error while deleting comment " + commentId + "for API " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIListDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApiChatExecuteRequestDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApiChatExecuteResponseDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApiChatPreparationResponseDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentListDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.DocumentDTO;
Expand Down Expand Up @@ -69,6 +72,42 @@ public Response addCommentToAPI(@ApiParam(value = "**API ID** consisting of the
return delegate.addCommentToAPI(apiId, postRequestBodyDTO, replyTo, securityContext);
}

@POST
@Path("/{apiId}/api-chat/execute")
@Consumes({ "application/json" })
@Produces({ "application/json" })
@ApiOperation(value = "Execute a single API test case while caching the progress", notes = "Executes a test scenario against an API; which iteratively provide resources that need to be invoked alongside the inputs such as parameters, payloads, etc. while caching the progress. ", response = ApiChatExecuteResponseDTO.class, authorizations = {
@Authorization(value = "OAuth2Security", scopes = {

})
}, tags={ "API Chat", })
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Created. API Chat execute response payload. ", response = ApiChatExecuteResponseDTO.class),
@ApiResponse(code = 400, message = "Bad Request. Invalid request or validation error.", response = ErrorDTO.class),
@ApiResponse(code = 401, message = "Unauthorized. The user is not authorized.", response = ErrorDTO.class),
@ApiResponse(code = 500, message = "Internal Server Error. An error occurred while executing test using API Chat service. ", response = Void.class) })
public Response apiChatExecute(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Request ID " )@HeaderParam("apiChatRequestId") String apiChatRequestId, @ApiParam(value = "API Chat execute request payload " ) ApiChatExecuteRequestDTO apiChatExecuteRequestDTO) throws APIManagementException{
return delegate.apiChatExecute(apiId, apiChatRequestId, apiChatExecuteRequestDTO, securityContext);
}

@POST
@Path("/{apiId}/api-chat/prepare")

@Produces({ "application/json" })
@ApiOperation(value = "Prepare API Chat service by feeding the OpenAPI specification of the API to be tested", notes = "Processing the OpenAPI specification to extract the API endpoint definitions and generate sample queries ", response = ApiChatPreparationResponseDTO.class, authorizations = {
@Authorization(value = "OAuth2Security", scopes = {

})
}, tags={ "API Chat", })
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Created. API Chat preparation completed. Successful response with enriched API specification and sample queries. ", response = ApiChatPreparationResponseDTO.class),
@ApiResponse(code = 400, message = "Bad Request. Invalid request or validation error.", response = ErrorDTO.class),
@ApiResponse(code = 401, message = "Unauthorized. The user is not authorized.", response = ErrorDTO.class),
@ApiResponse(code = 500, message = "Internal Server Error. An error occurred while preparing the API Chat service. ", response = Void.class) })
public Response apiChatPrepare(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Request ID " )@HeaderParam("apiChatRequestId") String apiChatRequestId) throws APIManagementException{
return delegate.apiChatPrepare(apiId, apiChatRequestId, securityContext);
}

@GET
@Path("/{apiId}/async-api-specification")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIListDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApiChatExecuteRequestDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApiChatExecuteResponseDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApiChatPreparationResponseDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentListDTO;
import org.wso2.carbon.apimgt.rest.api.store.v1.dto.DocumentDTO;
Expand All @@ -35,6 +38,8 @@

public interface ApisApiService {
public Response addCommentToAPI(String apiId, PostRequestBodyDTO postRequestBodyDTO, String replyTo, MessageContext messageContext) throws APIManagementException;
public Response apiChatExecute(String apiId, String apiChatRequestId, ApiChatExecuteRequestDTO apiChatExecuteRequestDTO, MessageContext messageContext) throws APIManagementException;
public Response apiChatPrepare(String apiId, String apiChatRequestId, MessageContext messageContext) throws APIManagementException;
public Response apisApiIdAsyncApiSpecificationGet(String apiId, String environmentName, String ifNoneMatch, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException;
public Response apisApiIdDocumentsDocumentIdContentGet(String apiId, String documentId, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) throws APIManagementException;
public Response apisApiIdDocumentsDocumentIdGet(String apiId, String documentId, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) throws APIManagementException;
Expand Down
Loading

0 comments on commit c3c0959

Please sign in to comment.