mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Minor fixes in api requests
This commit is contained in:
parent
7e94f152ea
commit
cbb03d4d1f
@ -84,6 +84,11 @@
|
||||
<artifactId>okhttp</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.orbit.com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@ -54,7 +54,7 @@ public interface PublisherRESTAPIServices {
|
||||
JSONObject getApis(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo)
|
||||
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
||||
|
||||
API createAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api)
|
||||
JSONObject addAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api)
|
||||
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
||||
|
||||
boolean updateApi(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api)
|
||||
|
||||
@ -55,7 +55,8 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
||||
private static final Gson gson = new Gson();
|
||||
private static final String host = System.getProperty(Constants.IOT_CORE_HOST);
|
||||
private static final String port = System.getProperty(Constants.IOT_CORE_HTTPS_PORT);
|
||||
private static final String endPointPrefix = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + Constants.COLON + port;
|
||||
private static final String endPointPrefix = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
||||
+ Constants.COLON + port;
|
||||
|
||||
@Override
|
||||
public JSONObject getScopes(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo)
|
||||
@ -127,7 +128,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
||||
throw new BadRequestException(msg);
|
||||
} else if (HttpStatus.SC_NOT_FOUND == response.code()) {
|
||||
String msg = "Shared scope key not found";
|
||||
log.error(msg);
|
||||
log.info(msg);
|
||||
return false;
|
||||
} else {
|
||||
String msg = "Response : " + response.code() + response.body();
|
||||
@ -144,7 +145,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
||||
public boolean addNewSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope)
|
||||
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
||||
|
||||
String addNewSharedScopeEndPoint = endPointPrefix + Constants.SCOPE_API_ENDPOINT + scope.getId();
|
||||
String addNewSharedScopeEndPoint = endPointPrefix + Constants.SCOPE_API_ENDPOINT;
|
||||
|
||||
ScopeUtils scopeUtil = new ScopeUtils();
|
||||
scopeUtil.setKey(scope.getKey());
|
||||
@ -163,7 +164,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
||||
|
||||
try {
|
||||
Response response = client.newCall(request).execute();
|
||||
if (HttpStatus.SC_OK == response.code()) {
|
||||
if (HttpStatus.SC_CREATED == response.code()) {
|
||||
return true;
|
||||
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
||||
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
||||
@ -177,7 +178,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
} else {
|
||||
String msg = "Response : " + response.code() + response.body();
|
||||
String msg = "Response : " + response.code() + response.message();
|
||||
throw new UnexpectedResponseException(msg);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@ -312,14 +313,22 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
||||
}
|
||||
|
||||
@Override
|
||||
public API createAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api)
|
||||
public JSONObject addAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api)
|
||||
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
||||
|
||||
String creatAPIEndPoint = endPointPrefix + Constants.API_ENDPOINT;
|
||||
String addAPIEndPoint = endPointPrefix + Constants.API_ENDPOINT;
|
||||
|
||||
RequestBody requestBody = RequestBody.create(JSON, String.valueOf(api));
|
||||
APIIdentifier apiIdentifier = api.getId();
|
||||
String apiString = "{\n" +
|
||||
" \"name\":\"" + apiIdentifier.getName().replace(Constants.SPACE, Constants.EMPTY_STRING) + "\",\n" +
|
||||
" \"description\":\"" + api.getDescription() + "\",\n" +
|
||||
" \"context\":\"" + api.getContext() + "\",\n" +
|
||||
" \"version\":\"" + apiIdentifier.getVersion() + "\"\n" +
|
||||
"}";
|
||||
|
||||
RequestBody requestBody = RequestBody.create(JSON, apiString);
|
||||
Request request = new Request.Builder()
|
||||
.url(creatAPIEndPoint)
|
||||
.url(addAPIEndPoint)
|
||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
||||
+ accessTokenInfo.getAccess_token())
|
||||
.post(requestBody)
|
||||
@ -328,21 +337,21 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
||||
try {
|
||||
Response response = client.newCall(request).execute();
|
||||
if (HttpStatus.SC_CREATED == response.code()) {
|
||||
return gson.fromJson(response.body().string(), API.class);
|
||||
|
||||
JSONObject jsonObject = new JSONObject(response.body().string());
|
||||
return jsonObject;
|
||||
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
||||
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
||||
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
||||
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
||||
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
||||
//TODO: max attempt count
|
||||
return createAPI(apiApplicationKey, refreshedAccessToken, api);
|
||||
return addAPI(apiApplicationKey, refreshedAccessToken, api);
|
||||
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
||||
String msg = "Bad Request, Invalid scope object";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
} else {
|
||||
String msg = "Response : " + response.code() + response.body();
|
||||
String msg = "Response status : " + response.code() + " Response message : " + response.message();
|
||||
throw new UnexpectedResponseException(msg);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@ -568,11 +577,12 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
||||
String uuid, String action)
|
||||
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
||||
|
||||
String changeStatusEndPoint = endPointPrefix + Constants.API_ENDPOINT + "change-lifecycle?apiId=" + uuid + "&action=" + action;
|
||||
String changeAPIStatusEndPoint = endPointPrefix + Constants.API_ENDPOINT + "change-lifecycle?apiId=" + uuid
|
||||
+ "&action=" + action;
|
||||
|
||||
RequestBody requestBody = RequestBody.create(JSON, Constants.EMPTY_STRING);
|
||||
Request request = new Request.Builder()
|
||||
.url(changeStatusEndPoint)
|
||||
.url(changeAPIStatusEndPoint)
|
||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
||||
+ accessTokenInfo.getAccess_token())
|
||||
.post(requestBody)
|
||||
@ -1009,7 +1019,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
||||
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
||||
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
||||
//TODO: max attempt count
|
||||
return addDocumentationContent(apiApplicationKey, refreshedAccessToken, api,docId ,docContent);
|
||||
return addDocumentationContent(apiApplicationKey, refreshedAccessToken, api, docId, docContent);
|
||||
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
||||
String msg = "Bad Request, Invalid scope object";
|
||||
log.error(msg);
|
||||
|
||||
@ -34,7 +34,7 @@ public final class Constants {
|
||||
public static final String OAUTH_TOKEN_TYPE = "token_type";
|
||||
public static final String REFRESH_TOKEN_GRANT_TYPE = "refresh_token";
|
||||
public static final String SCOPE_PARAM_NAME = "scope";
|
||||
public static final String SCOPES = "apim:api_create apim:api_view apim:shared_scope_manage";
|
||||
public static final String SCOPES = "apim:api_create apim:api_view apim:shared_scope_manage apim:api_import_export apim:api_publish";
|
||||
public static final String DCR_END_POINT = "WorkflowConfigurations.DCREndPoint";
|
||||
public static final String TOKE_END_POINT = "WorkflowConfigurations.TokenEndPoint";
|
||||
public static final String ADAPTER_CONF_KEEP_ALIVE = "keepAlive";
|
||||
@ -58,6 +58,7 @@ public final class Constants {
|
||||
public static final String SCHEME_SEPARATOR = "://";
|
||||
public static final String COLON = ":";
|
||||
public static final String QUERY_KEY_VALUE_SEPARATOR = "=";
|
||||
public static final String SPACE = " ";
|
||||
public static final String IOT_CORE_HOST = "iot.core.host";
|
||||
public static final String IOT_CORE_HTTPS_PORT = "iot.core.https.port";
|
||||
public static final String GET_ALL_SCOPES = "/api/am/publisher/v2/scopes?limit=1000";
|
||||
|
||||
@ -62,11 +62,11 @@ public class ScopeUtils {
|
||||
|
||||
public String toJSON() {
|
||||
String jsonString = "{\n" +
|
||||
" \"name\":\" " + key + "\",\n" +
|
||||
" \"displayName\":\" " + name + "\",\n" +
|
||||
" \"description\":\" " + description + " \",\n" +
|
||||
" \"name\":\"" + key + "\",\n" +
|
||||
" \"displayName\":\"" + name + "\",\n" +
|
||||
" \"description\":\"" + description + "\",\n" +
|
||||
" \"bindings\":[\n" +
|
||||
" \" " + roles + " \"\n" +
|
||||
" \"" + roles + "\"\n" +
|
||||
" ]\n" +
|
||||
"}";
|
||||
return jsonString;
|
||||
|
||||
@ -167,7 +167,8 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
||||
if (!apiFound) {
|
||||
// add new scopes as shared scopes
|
||||
for (ApiScope apiScope : apiConfig.getScopes()) {
|
||||
if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, apiScope.getKey())) {
|
||||
if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo,
|
||||
apiScope.getKey())) {
|
||||
Scope scope = new Scope();
|
||||
scope.setName(apiScope.getName());
|
||||
scope.setDescription(apiScope.getDescription());
|
||||
@ -178,12 +179,12 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
||||
}
|
||||
API api = getAPI(apiConfig, true);
|
||||
api.setId(apiIdentifier);
|
||||
API createdAPI = publisherRESTAPIServices.createAPI(apiApplicationKey, accessTokenInfo , api); // add api
|
||||
JSONObject createdAPI = publisherRESTAPIServices.addAPI(apiApplicationKey, accessTokenInfo, api); // add api
|
||||
if (apiConfig.getEndpointType() != null && "WS".equals(apiConfig.getEndpointType())) {
|
||||
publisherRESTAPIServices.saveAsyncApiDefinition(apiApplicationKey, accessTokenInfo,
|
||||
api.getUuid(), apiConfig.getAsyncApiDefinition());
|
||||
}
|
||||
if (CREATED_STATUS.equals(createdAPI.getStatus())) {
|
||||
if (CREATED_STATUS.equals(createdAPI.getString("lifeCycleStatus"))) {
|
||||
// if endpoint type "dynamic" and then add in sequence
|
||||
if ("dynamic".equals(apiConfig.getEndpointType())) {
|
||||
Mediation mediation = new Mediation();
|
||||
@ -192,12 +193,13 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
||||
mediation.setType("in");
|
||||
mediation.setGlobal(false);
|
||||
publisherRESTAPIServices.addApiSpecificMediationPolicy(apiApplicationKey,
|
||||
accessTokenInfo, createdAPI.getUuid(), mediation);
|
||||
accessTokenInfo, createdAPI.getString("id"), mediation);
|
||||
}
|
||||
publisherRESTAPIServices.changeLifeCycleStatus(apiApplicationKey, accessTokenInfo, createdAPI.getUuid(), PUBLISH_ACTION);
|
||||
publisherRESTAPIServices.changeLifeCycleStatus(apiApplicationKey, accessTokenInfo,
|
||||
createdAPI.getString("id"), PUBLISH_ACTION);
|
||||
|
||||
APIRevision apiRevision = new APIRevision();
|
||||
apiRevision.setApiUUID(createdAPI.getUuid());
|
||||
apiRevision.setApiUUID(createdAPI.getString("id"));
|
||||
apiRevision.setDescription("Initial Revision");
|
||||
String apiRevisionId = publisherRESTAPIServices.addAPIRevision(apiApplicationKey,
|
||||
accessTokenInfo, apiRevision).getRevisionUUID();
|
||||
@ -210,7 +212,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
||||
List<APIRevisionDeployment> apiRevisionDeploymentList = new ArrayList<>();
|
||||
apiRevisionDeploymentList.add(apiRevisionDeployment);
|
||||
publisherRESTAPIServices.deployAPIRevision(apiApplicationKey, accessTokenInfo,
|
||||
createdAPI.getUuid(), apiRevisionId, apiRevisionDeploymentList);
|
||||
createdAPI.getString("id"), apiRevisionId, apiRevisionDeploymentList);
|
||||
}
|
||||
} else {
|
||||
if (WebappPublisherConfig.getInstance().isEnabledUpdateApi()) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user