Skip to content

Commit

Permalink
Remove get API Id
Browse files Browse the repository at this point in the history
  • Loading branch information
piyumaldk committed Feb 19, 2025
1 parent a16b2db commit cf64191
Showing 1 changed file with 21 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class DevPortalHandlerImpl implements DevPortalHandler {
private static final Log log = LogFactory.getLog(DevPortalHandlerImpl.class);
private static final String baseUrl = getNewPortalURL();
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final Map<String, String> orgIdCache = new ConcurrentHashMap<>();
private static final Map<String, String> orgIdMap = new ConcurrentHashMap<>();
private static SSLConnectionSocketFactory sslsf;
private static volatile DevPortalHandlerImpl instance;

Expand Down Expand Up @@ -144,8 +144,7 @@ public void updateAPIMetadata(String organization, API api, String refId) throws
String apiMetaData = getApiMetaData(api);
String orgId = getOrgId(organization);
if (orgId != null) {
String apiId = getApiId(orgId, api.getId().getApiName(), api.getId().getVersion());
HttpResponseData apiPutResponseData = apiPutAction(orgId, apiId, apiDefinition, apiMetaData);
HttpResponseData apiPutResponseData = apiPutAction(orgId, refId, apiDefinition, apiMetaData);
if (apiPutResponseData.getStatusCode() == 200) {
log.info("API " + api.getId().getApiName() + " successfully updated in " + baseUrl);
} else {
Expand All @@ -162,26 +161,23 @@ public void updateAPIMetadata(String organization, API api, String refId) throws
public void unpublishAPIMetadata(String organization, API api, String refId) throws APIManagementException {
String orgId = getOrgId(organization);
if (orgId != null) {
String apiId = getApiId(orgId, api.getId().getApiName(), api.getId().getVersion());
if (apiId != null) {
HttpResponseData responseData = apiDeleteAction(orgId, apiId);
if (responseData.getStatusCode() == 200) {
log.info("API " + api.getId().getApiName() + " successfully unpublished from " + baseUrl);
} else if (responseData.getStatusCode() == 404) {
throw new APIManagementException("API " + api.getId().getApiName() + " is not available in "
+ baseUrl + " .Hence API cannot get unpublished from " + baseUrl);
} else {
throw new APIManagementException("Failed to delete API " + api.getId().getApiName() + " from "
+ baseUrl + ". Status code: " + responseData.getStatusCode());
}
HttpResponseData responseData = apiDeleteAction(orgId, refId);
if (responseData.getStatusCode() == 200) {
log.info("API " + api.getId().getApiName() + " successfully unpublished from " + baseUrl);
} else if (responseData.getStatusCode() == 404) {
throw new APIManagementException("API " + api.getId().getApiName() + " is not available in "
+ baseUrl + " .Hence API cannot get unpublished from " + baseUrl);
} else {
throw new APIManagementException("Failed to delete API " + api.getId().getApiName() + " from "
+ baseUrl + ". Status code: " + responseData.getStatusCode());
}
} else {
throw new APIManagementException("Unable to find an organization in " + baseUrl + " that matches the tenant."
+ "Hence fails to un-publish from " + baseUrl);
}
}

private static String getDefinitionForDevPortal (API api) {
private static String getDefinitionForDevPortal (API api) throws APIManagementException {
String type = getType(api.getType());
switch (type) {
case "REST":
Expand All @@ -191,17 +187,16 @@ private static String getDefinitionForDevPortal (API api) {
case "GraphQL":
return api.getGraphQLSchema();
case "SOAP":
return null;
return "TODO: SOAP";
default:
return null;
throw new APIManagementException("Cannot find a definition for the given type");
}
}

private static String getOrgId(String tenantName)
throws APIManagementException {
if (orgIdCache.containsKey(tenantName)) {
// OrgID cache hit
return orgIdCache.get(tenantName);
if (orgIdMap.containsKey(tenantName)) {
return orgIdMap.get(tenantName);
}
HttpResponseData responseData = orgGetAction(tenantName);
String orgId;
Expand All @@ -215,42 +210,13 @@ private static String getOrgId(String tenantName)
} else {
return null;
}
orgIdCache.put(tenantName, orgId);
orgIdMap.put(tenantName, orgId);
return orgId;
} catch (JsonProcessingException e) {
throw new APIManagementException("Error while processing Json response: " + e.getMessage(), e);
}
}

private static String getApiId(String orgId, String apiName, String apiVersion)
throws APIManagementException {
HttpResponseData responseData = apiGetAction(orgId, apiName, apiVersion);

if (responseData.getStatusCode() == 200) {
try {
List<Map<String, Object>> apiList = objectMapper.readValue(responseData.getResponseBody(),
new TypeReference<List<Map<String, Object>>>() {});
if (apiList.size() == 1) {
Map<String, Object> apiDetails = apiList.get(0);
return (String) apiDetails.get("apiID");
} else if (apiList.size() > 1) {
throw new APIManagementException("There are multiple APIs for the API name: " + apiName
+ " & version: " + apiVersion);
} else {
throw new APIManagementException("No API found for the API name: " + apiName + " & version: "
+ apiVersion);
}
} catch (JsonProcessingException e) {
throw new APIManagementException("Error reading API response: " + e.getMessage(), e);
}
} else if (responseData.getStatusCode() == 404) {
throw new APIManagementException("API is not available in " + baseUrl + " for the name: " + apiName +
" and version: " + apiVersion);
} else {
throw new APIManagementException("Failed to retrieve API ID. Status code: " + responseData.getStatusCode());
}
}

private static String getApiMetaData(API api) throws APIManagementException {
ApiMetaDataDTO apiMetaDataDTO = new ApiMetaDataDTO();

Expand Down Expand Up @@ -383,29 +349,9 @@ private static HttpResponseData orgGetAction(String orgRef)
}
}

private static HttpResponseData apiGetAction(String orgId, String name, String version)
throws APIManagementException {
String apiUrl = baseUrl + DevPortalProcessingConstants.API_URI;
try (CloseableHttpClient httpClient = getHttpClient()) {
URIBuilder uriBuilder = new URIBuilder(apiUrl)
.addParameter("name", name)
.addParameter("version", version);
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.setHeader("organization", orgId);

try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());
return new HttpResponseData(statusCode, responseBody);
}
} catch (IOException | URISyntaxException e) {
throw new APIManagementException("Error while API search in " + baseUrl + ": " + e.getMessage(), e);
}
}

private static HttpResponseData apiDeleteAction(String orgId, String apiId)
private static HttpResponseData apiDeleteAction(String orgId, String refId)
throws APIManagementException {
String apiUrl = baseUrl + DevPortalProcessingConstants.API_URI + "/" + apiId;
String apiUrl = baseUrl + DevPortalProcessingConstants.API_URI + "/" + refId;

try (CloseableHttpClient httpClient = getHttpClient()) {
HttpDelete httpDelete = new HttpDelete(apiUrl);
Expand Down Expand Up @@ -443,10 +389,10 @@ private static HttpResponseData apiPostAction(String orgId, String apiMetadata,
}
}

private static HttpResponseData apiPutAction(String orgId, String apiId, String apiDefinition,
private static HttpResponseData apiPutAction(String orgId, String refId, String apiDefinition,
String apiMetadata)
throws APIManagementException {
String apiUrl = baseUrl + DevPortalProcessingConstants.API_URI + "/" + apiId;
String apiUrl = baseUrl + DevPortalProcessingConstants.API_URI + "/" + refId;
try (CloseableHttpClient httpClient = getHttpClient()) {
HttpPut httpPut = new HttpPut(apiUrl);
httpPut.setHeader("organization", orgId);
Expand Down

0 comments on commit cf64191

Please sign in to comment.