Map JsonObject to dto classes in publisher API documentation

master
Pasindu Rupasinghe 1 year ago
parent fa37fa33cc
commit 1325780e3b

@ -94,7 +94,7 @@ public interface PublisherRESTAPIServices {
APIRevision apiRevision, String uuid) APIRevision apiRevision, String uuid)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
JSONObject getDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Documentation[] getDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
String uuid) String uuid)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;

@ -1041,7 +1041,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public JSONObject getDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid) public Documentation[] getDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getDocumentationsEndPoint = endPointPrefix + Constants.API_ENDPOINT + uuid + "/documents?limit=1000"; String getDocumentationsEndPoint = endPointPrefix + Constants.API_ENDPOINT + uuid + "/documents?limit=1000";
@ -1056,8 +1056,8 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
try { try {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
if (HttpStatus.SC_OK == response.code()) { if (HttpStatus.SC_OK == response.code()) {
JSONObject jsonObject = new JSONObject(response.body().string()); JSONArray documentList = (JSONArray) new JSONObject(response.body().string()).get("list");
return jsonObject; return gson.fromJson(documentList.toString(), Documentation[].class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices. AccessTokenInfo refreshedAccessToken = apiApplicationServices.
@ -1127,17 +1127,16 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
String addNewScope = endPointPrefix + Constants.API_ENDPOINT + uuid + "/documents"; String addNewScope = endPointPrefix + Constants.API_ENDPOINT + uuid + "/documents";
String document = "{\n" + JSONObject payload = new JSONObject();
" \"name\": \"" + documentation.getName() + "\",\n" + payload.put("name", documentation.getName());
" \"type\": \"" + documentation.getType() + "\",\n" + payload.put("type", documentation.getType());
" \"summary\": \"" + documentation.getSummary() + "\",\n" + payload.put("summary", documentation.getSummary());
" \"sourceType\": \"" + documentation.getSourceType() + "\",\n" + payload.put("sourceType", documentation.getSourceType());
" \"inlineContent\": \"" + documentation.getSourceType() + "\",\n" + payload.put("inlineContent", documentation.getSourceType());
" \"visibility\": \"" + documentation.getVisibility() + "\",\n" + payload.put("visibility", documentation.getVisibility());
" \"createdBy\": \"admin\"\n" + payload.put("createdBy", documentation.getCreatedBy());
"}";
RequestBody requestBody = RequestBody.create(JSON, payload.toString());
RequestBody requestBody = RequestBody.create(JSON, document);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(addNewScope) .url(addNewScope)
.addHeader(Constants.HEADER_CONTENT_TYPE, Constants.APPLICATION_JSON) .addHeader(Constants.HEADER_CONTENT_TYPE, Constants.APPLICATION_JSON)

@ -24,16 +24,21 @@ public class Documentation {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String id; private String id;
private String documentId; private String documentId;
private DocumentationType type;
private String name; private String name;
private DocumentationType type;
private String summary; private String summary;
private DocumentSourceType sourceType; private DocumentSourceType sourceType;
private String sourceUrl; private String sourceUrl;
private DocumentVisibility visibility; private String fileName;
private Date lastUpdated;
private String filePath; private String filePath;
private Date createdDate; private String inlineContent;
private String otherTypeName; private String otherTypeName;
private DocumentVisibility visibility;
private String createdTime;
private Date createdDate;
private String createdBy;
private Date lastUpdatedTime;
private String lastUpdatedBy;
public String getOtherTypeName() { public String getOtherTypeName() {
return this.otherTypeName; return this.otherTypeName;
@ -113,12 +118,12 @@ public class Documentation {
return result; return result;
} }
public Date getLastUpdated() { public Date getLastUpdatedTime() {
return this.lastUpdated; return this.lastUpdatedTime;
} }
public void setLastUpdated(Date lastUpdated) { public void setLastUpdatedTime(Date lastUpdatedTime) {
this.lastUpdated = lastUpdated; this.lastUpdatedTime = lastUpdatedTime;
} }
public String getId() { public String getId() {
@ -145,6 +150,46 @@ public class Documentation {
this.createdDate = createdDate; this.createdDate = createdDate;
} }
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getInlineContent() {
return inlineContent;
}
public void setInlineContent(String inlineContent) {
this.inlineContent = inlineContent;
}
public String getCreatedTime() {
return createdTime;
}
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getLastUpdatedBy() {
return lastUpdatedBy;
}
public void setLastUpdatedBy(String lastUpdatedBy) {
this.lastUpdatedBy = lastUpdatedBy;
}
public static enum DocumentVisibility { public static enum DocumentVisibility {
OWNER_ONLY("owner_only"), OWNER_ONLY("owner_only"),
PRIVATE("private"), PRIVATE("private"),

@ -397,27 +397,27 @@ public class APIPublisherServiceImpl implements APIPublisherService {
apiDocumentation.setVisibility(Documentation.DocumentVisibility.API_LEVEL); apiDocumentation.setVisibility(Documentation.DocumentVisibility.API_LEVEL);
apiDocumentation.setSourceType(Documentation.DocumentSourceType.MARKDOWN); apiDocumentation.setSourceType(Documentation.DocumentSourceType.MARKDOWN);
apiDocumentation.setCreatedDate(new Date()); apiDocumentation.setCreatedDate(new Date());
apiDocumentation.setLastUpdated(new Date()); apiDocumentation.setLastUpdatedTime(new Date());
apiDocumentation.setSummary(apiConfig.getApiDocumentationSummary()); apiDocumentation.setSummary(apiConfig.getApiDocumentationSummary());
apiDocumentation.setOtherTypeName(null); apiDocumentation.setOtherTypeName(null);
JSONArray documentList = (JSONArray) publisherRESTAPIServices.getDocumentations(apiApplicationKey, Documentation[] documentList = publisherRESTAPIServices.getDocumentations(apiApplicationKey,
accessTokenInfo, apiUuid).get("list"); accessTokenInfo, apiUuid);
if (documentList.length() > 0) { if (documentList.length > 0) {
for (int i = 0; i < documentList.length(); i++) { for (int i = 0; i < documentList.length; i++) {
JSONObject existingDoc = documentList.getJSONObject(i); Documentation existingDoc = documentList[i];
if (existingDoc.getString("name").equals(apiConfig.getApiDocumentationName()) if (existingDoc.getName().equals(apiConfig.getApiDocumentationName())
&& existingDoc.getString("type").equals(Documentation.DocumentationType.HOWTO.name())) { && existingDoc.getType().equals(Documentation.DocumentationType.HOWTO.name())) {
publisherRESTAPIServices.deleteDocumentations(apiApplicationKey, accessTokenInfo, publisherRESTAPIServices.deleteDocumentations(apiApplicationKey, accessTokenInfo,
apiUuid, existingDoc.getString("documentId")); apiUuid, existingDoc.getDocumentId());
} }
} }
} else { } else {
log.info("There is no any existing api documentation."); log.info("There is no any existing api documentation.");
} }
io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIInfo.Documentation createdDoc = publisherRESTAPIServices.addDocumentation(apiApplicationKey, accessTokenInfo, Documentation createdDoc = publisherRESTAPIServices.addDocumentation(apiApplicationKey, accessTokenInfo,
apiUuid, apiDocumentation); apiUuid, apiDocumentation);
publisherRESTAPIServices.addDocumentationContent(apiApplicationKey, accessTokenInfo, apiUuid, publisherRESTAPIServices.addDocumentationContent(apiApplicationKey, accessTokenInfo, apiUuid,

Loading…
Cancel
Save