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)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
JSONObject getDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
Documentation[] getDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
String uuid)
throws APIServicesException, BadRequestException, UnexpectedResponseException;

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

@ -24,16 +24,21 @@ public class Documentation {
private static final long serialVersionUID = 1L;
private String id;
private String documentId;
private DocumentationType type;
private String name;
private DocumentationType type;
private String summary;
private DocumentSourceType sourceType;
private String sourceUrl;
private DocumentVisibility visibility;
private Date lastUpdated;
private String fileName;
private String filePath;
private Date createdDate;
private String inlineContent;
private String otherTypeName;
private DocumentVisibility visibility;
private String createdTime;
private Date createdDate;
private String createdBy;
private Date lastUpdatedTime;
private String lastUpdatedBy;
public String getOtherTypeName() {
return this.otherTypeName;
@ -113,12 +118,12 @@ public class Documentation {
return result;
}
public Date getLastUpdated() {
return this.lastUpdated;
public Date getLastUpdatedTime() {
return this.lastUpdatedTime;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
public void setLastUpdatedTime(Date lastUpdatedTime) {
this.lastUpdatedTime = lastUpdatedTime;
}
public String getId() {
@ -145,6 +150,46 @@ public class Documentation {
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 {
OWNER_ONLY("owner_only"),
PRIVATE("private"),

@ -397,27 +397,27 @@ public class APIPublisherServiceImpl implements APIPublisherService {
apiDocumentation.setVisibility(Documentation.DocumentVisibility.API_LEVEL);
apiDocumentation.setSourceType(Documentation.DocumentSourceType.MARKDOWN);
apiDocumentation.setCreatedDate(new Date());
apiDocumentation.setLastUpdated(new Date());
apiDocumentation.setLastUpdatedTime(new Date());
apiDocumentation.setSummary(apiConfig.getApiDocumentationSummary());
apiDocumentation.setOtherTypeName(null);
JSONArray documentList = (JSONArray) publisherRESTAPIServices.getDocumentations(apiApplicationKey,
accessTokenInfo, apiUuid).get("list");
Documentation[] documentList = publisherRESTAPIServices.getDocumentations(apiApplicationKey,
accessTokenInfo, apiUuid);
if (documentList.length() > 0) {
for (int i = 0; i < documentList.length(); i++) {
JSONObject existingDoc = documentList.getJSONObject(i);
if (existingDoc.getString("name").equals(apiConfig.getApiDocumentationName())
&& existingDoc.getString("type").equals(Documentation.DocumentationType.HOWTO.name())) {
if (documentList.length > 0) {
for (int i = 0; i < documentList.length; i++) {
Documentation existingDoc = documentList[i];
if (existingDoc.getName().equals(apiConfig.getApiDocumentationName())
&& existingDoc.getType().equals(Documentation.DocumentationType.HOWTO.name())) {
publisherRESTAPIServices.deleteDocumentations(apiApplicationKey, accessTokenInfo,
apiUuid, existingDoc.getString("documentId"));
apiUuid, existingDoc.getDocumentId());
}
}
} else {
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);
publisherRESTAPIServices.addDocumentationContent(apiApplicationKey, accessTokenInfo, apiUuid,

Loading…
Cancel
Save