Tuesday, 16 April 2019

Retrieve the list of active Projects available in SDL WorldServer using the REST API

In this article, I would like to present a method for retrieving the list of active Projects in SDL WorldServer using the REST API.

The method takes two parameters:
  1. wsBaseUrl
  2. token
The wsBaseUrl must be the <serverURL>:<portnumber> where your WorldServer instance is running.

The second parameter is a security token, which can be retrieved by using the SDL WorldServer REST API as explained here.

The method returns a List of Project objects, one for each active project, containing a subset of the JSON response data.

public class Project {
private int id;
private int projectGroupId;
private String name;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getProjectGroupId() {
return projectGroupId;
}
public void setProjectGroupId(int projectGroupId) {
this.projectGroupId = projectGroupId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
view raw Project.java hosted with ❤ by GitHub
public static List<Project> getActiveProjects(String wsBaseUrl, String token) throws IOException, URISyntaxException {
List<Project> activeProjects = new ArrayList<>();
URI getUri = new URIBuilder(wsBaseUrl + "/ws-api/v1/projects")
.addParameter("token", token)
.addParameter("onlyActive", "true")
.build();
JsonObject jsonObject = getItems(getUri);
JsonArray items = jsonObject.getAsJsonArray("items");
Iterator<JsonElement> itemIterator = items.iterator();
while (itemIterator.hasNext()) {
JsonObject item = itemIterator.next().getAsJsonObject();
Project project = new Project();
project.setId(item.getAsJsonPrimitive("id").getAsInt());
project.setProjectGroupId(item.getAsJsonPrimitive("projectGroupId").getAsInt());
project.setName(item.getAsJsonPrimitive("name").getAsString());
project.setDescription(item.getAsJsonPrimitive("description").getAsString());
activeProjects.add(project);
}
return activeProjects;
}

No comments:

Post a Comment