Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add devportal rest api changes for API Chat feature #12347

Merged
merged 4 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -799,6 +799,28 @@ Set<SubscribedAPI> getPaginatedSubscribedAPIsByApplication(Application applicati
*/
Tier getThrottlePolicyByName(String name, int policyType, String organization) throws APIManagementException;

/**
* Returns the API Chat execute call response as a string
*
* @param apiChatRequestId Request UUID
* @param requestPayload Request payload to be used for the AI service execute call
* @return execution response as a string
* @throws APIManagementException if execute call failed
*/
String invokeApiChatExecute(String apiChatRequestId, String requestPayload) throws APIManagementException;

/**
* Returns the API Chat prepare call response as a string
*
* @param apiId ID of the API
* @param apiChatRequestId Request UUID
* @param organization Identifier of an organization
* @return prepare response
* @throws APIManagementException if prepare call failed
*/
String invokeApiChatPrepare(String apiId, String apiChatRequestId, String organization)
throws APIManagementException;

/**
* This method used to retrieve key manager configurations for tenant
* @param organization organization of the key manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,11 @@
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"),

Check warning on line 551 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java#L551

Added line #L551 was not covered by tests

// AI service invocation related exceptions
AI_SERVICE_INVALID_RESPONSE(903100, "Invalid response from AI service", 500, "Error while invoking AI service. %s", false),
AI_SERVICE_INVALID_ACCESS_TOKEN(900906, "Invalid access token provided for AI service", 401, "Invalid access token provided for AI service");

Check warning on line 555 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java#L554-L555

Added lines #L554 - L555 were not covered by tests

private final long errorCode;
private final String errorMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,14 @@ public final class APIConstants {
public static final String DIGEST = "x5t#S256";
public static final String CNF = "cnf";

// Constants related to AI features: API chat and Marketplace Assistant
ashera96 marked this conversation as resolved.
Show resolved Hide resolved
public static final String API_CHAT = "APIChat.";
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_PREPARE_RESOURCE = "/prepare"; // "/api-chat/prepare"
public static final String API_CHAT_EXECUTE_RESOURCE = "/chat"; // "/api-chat/chat"

//documentation rxt

public static final String DOC_NAME = "overview_name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3308,6 +3308,23 @@
return APIUtil.removeXMediationScriptsFromSwagger(definition);
}

@Override
public String invokeApiChatExecute(String apiChatRequestId, String requestPayload) throws APIManagementException {
return APIUtil.invokeAIService(APIConstants.API_CHAT_ENDPOINT,

Check warning on line 3313 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java#L3313

Added line #L3313 was not covered by tests
APIConstants.API_CHAT_AUTH_TOKEN, APIConstants.API_CHAT_EXECUTE_RESOURCE, requestPayload,
apiChatRequestId);
}

@Override
public String invokeApiChatPrepare(String apiId, String apiChatRequestId, String organization)
throws APIManagementException {
String swaggerDefinition = getOpenAPIDefinition(apiId, organization);
String payload = "{\"openapi\": " + swaggerDefinition + "}";
ashera96 marked this conversation as resolved.
Show resolved Hide resolved
String prepareResponse = APIUtil.invokeAIService(APIConstants.API_CHAT_ENDPOINT,

Check warning on line 3323 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java#L3321-L3323

Added lines #L3321 - L3323 were not covered by tests
APIConstants.API_CHAT_AUTH_TOKEN, APIConstants.API_CHAT_PREPARE_RESOURCE, payload, apiChatRequestId);
return prepareResponse;

Check warning on line 3325 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java#L3325

Added line #L3325 was not covered by tests
}

@Override
public String getOpenAPIDefinitionForEnvironment(API api, String environmentName)
throws APIManagementException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10371,4 +10371,76 @@
}
return scopesStringBuilder.toString().trim();
}

Check warning on line 10374 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10374

Added line #L10374 was not covered by tests
/**
* Check whether API Chat feature is enabled
*
* @return returns true if API Chat feature is enabled, false if disabled.
*/
public static boolean isApiChatEnabled() {

APIManagerConfiguration config = ServiceReferenceHolder.getInstance().
getAPIManagerConfigurationService().getAPIManagerConfiguration();
String isApiChatEnabled = config.getFirstProperty(APIConstants.API_CHAT_ENABLED);

Check warning on line 10384 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10384

Added line #L10384 was not covered by tests
if (isApiChatEnabled == null) {
ashera96 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

Check warning on line 10388 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10386-L10388

Added lines #L10386 - L10388 were not covered by tests
return Boolean.parseBoolean(isApiChatEnabled);
}

/**
* Checks whether an auth token is provided for AI features to use. This token is utilized for authentication and
* throttling purposes.
*
* @return returns true if a valid auth token is found, false otherwise.
*/
public static boolean isAuthTokenProvidedForAIFeatures() {
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().
getAPIManagerConfigurationService().getAPIManagerConfiguration();

Check warning on line 10400 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10398-L10400

Added lines #L10398 - L10400 were not covered by tests
String authToken = config.getFirstProperty(APIConstants.API_CHAT_AUTH_TOKEN);
if (authToken == null || authToken.equals("")) {

Check warning on line 10402 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10402

Added line #L10402 was not covered by tests
ashera96 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
return true;

Check warning on line 10405 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10405

Added line #L10405 was not covered by tests
}

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

APIManagerConfiguration config = ServiceReferenceHolder.getInstance().
getAPIManagerConfigurationService().getAPIManagerConfiguration();
String endpoint = config.getFirstProperty(endpointConfigName);
String authToken = config.getFirstProperty(authTokenConfigName);
try {
HttpPost preparePost = new HttpPost(endpoint + resource);
preparePost.setHeader(HttpHeaders.AUTHORIZATION, APIConstants.AUTHORIZATION_BEARER + authToken);

Check warning on line 10417 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10415-L10417

Added lines #L10415 - L10417 were not covered by tests
preparePost.setHeader(HttpHeaders.CONTENT_TYPE, APIConstants.APPLICATION_JSON_MEDIA_TYPE);
preparePost.setHeader("x-request-id", requestId);

Check warning on line 10419 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10419

Added line #L10419 was not covered by tests
StringEntity requestEntity = new StringEntity(payload, ContentType.APPLICATION_JSON);
preparePost.setEntity(requestEntity);

Check warning on line 10421 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10421

Added line #L10421 was not covered by tests

URL url = new URL(endpoint);
int port = url.getPort();
String protocol = url.getProtocol();
HttpClient httpClient = APIUtil.getHttpClient(port, protocol);

CloseableHttpResponse response = executeHTTPRequest(preparePost, httpClient);
int statusCode = response.getStatusLine().getStatusCode();
String responseStr = EntityUtils.toString(response.getEntity());

Check warning on line 10430 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10427-L10430

Added lines #L10427 - L10430 were not covered by tests
if (statusCode == HttpStatus.SC_CREATED) {
return responseStr;
} else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
throw new APIManagementException("Unexpected response detected from the AI service." + responseStr,
ExceptionCodes.AI_SERVICE_INVALID_ACCESS_TOKEN);
} else {
throw new APIManagementException("Unexpected response detected from the AI service." + responseStr,

Check warning on line 10437 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10432-L10437

Added lines #L10432 - L10437 were not covered by tests
ExceptionCodes.AI_SERVICE_INVALID_RESPONSE);
}
} catch (MalformedURLException e) {
throw new APIManagementException("Invalid/malformed URL encountered. URL: " + endpoint, e);
} catch (APIManagementException | IOException e) {

Check warning on line 10442 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10439-L10442

Added lines #L10439 - L10442 were not covered by tests
throw new APIManagementException("Error encountered while connecting to service", e);
}
}

Check warning on line 10445 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10444-L10445

Added lines #L10444 - L10445 were not covered by tests
}
Loading
Loading