edu.ksu.canvas.impl.ExternalToolImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of canvas-api Show documentation
Show all versions of canvas-api Show documentation
A native Java library to talk to the Canvas REST API
package edu.ksu.canvas.impl;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import edu.ksu.canvas.interfaces.ExternalToolReader;
import edu.ksu.canvas.interfaces.ExternalToolWriter;
import edu.ksu.canvas.model.ExternalTool;
import edu.ksu.canvas.net.Response;
import edu.ksu.canvas.net.RestClient;
import edu.ksu.canvas.oauth.OauthToken;
import edu.ksu.canvas.requestOptions.ListExternalToolsOptions;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class ExternalToolImpl extends BaseImpl implements ExternalToolReader, ExternalToolWriter {
private static final Logger LOG = LoggerFactory.getLogger(ExternalToolImpl.class);
public ExternalToolImpl(String canvasBaseUrl, Integer apiVersion, OauthToken oauthToken, RestClient restClient,
int connectTimeout, int readTimeout, Integer paginationPageSize, Boolean serializeNulls) {
super(canvasBaseUrl, apiVersion, oauthToken, restClient, connectTimeout, readTimeout,
paginationPageSize, serializeNulls);
}
@Override
public Optional getExternalToolInCourse(String courseId, Integer toolId) throws IOException {
return getExternalTool("courses", courseId, toolId);
}
@Override
public Optional getExternalToolInAccount(String accountId, Integer toolId) throws IOException {
return getExternalTool("accounts", accountId, toolId);
}
private Optional getExternalTool(String objectType, String objectId, Integer toolId) throws IOException {
LOG.debug("Getting external tool " + toolId + " from " + objectType + " " + objectId);
if(StringUtils.isBlank(objectId) || toolId == null) {
throw new IllegalArgumentException("course/account ID and tool ID cannot be blank");
}
String url = buildCanvasUrl(objectType + "/" + objectId + "/external_tools/" + toolId, Collections.emptyMap());
return getFromCanvas(url);
}
@Override
public List listExternalToolsInAccount(ListExternalToolsOptions options) throws IOException {
return listExternalTools("accounts", options.getId(), options.getOptionsMap());
}
@Override
public List listExternalToolsInCourse(ListExternalToolsOptions options) throws IOException {
return listExternalTools("courses", options.getId(), options.getOptionsMap());
}
@Override
public List listExternalToolsInGroup(ListExternalToolsOptions options) throws IOException {
return listExternalTools("groups", options.getId(), options.getOptionsMap());
}
private List listExternalTools(String objectType, String objectId, Map> optionsMap) throws IOException {
LOG.debug("Getting list of external tools from " + objectType + ": " + objectId);
String url = buildCanvasUrl(objectType + "/" + objectId + "/external_tools", optionsMap);
return getListFromCanvas(url);
}
@Override
public Optional createExternalToolInCourse(String courseId, ExternalTool tool) throws IOException {
LOG.debug("Creating external tool \"" + tool.getName() + "\" in course " + courseId);
String url = buildCanvasUrl("courses/" + courseId + "/external_tools", Collections.emptyMap());
return createExternalTool(url, tool);
}
@Override
public Optional createExternalToolInAccount(String accountId, ExternalTool tool) throws IOException {
LOG.debug("Creating external tool \"" + tool.getName() + "\" in account " + accountId);
String url = buildCanvasUrl("accounts/" + accountId + "/external_tools", Collections.emptyMap());
return createExternalTool(url, tool);
}
private Optional createExternalTool(String url, ExternalTool tool) throws IOException {
ensureToolValidForCreation(tool);
Gson gson = GsonResponseParser.getDefaultGsonParser(serializeNulls);
JsonObject toolJson = gson.toJsonTree(tool).getAsJsonObject();
Response response = canvasMessenger.sendJsonPostToCanvas(oauthToken, url, toolJson);
return responseParser.parseToObject(ExternalTool.class, response);
}
@Override
public Optional editExternalToolInCourse(String courseId, ExternalTool tool) throws IOException {
LOG.debug("Editing external tool \"" + tool.getName() + "\" in course " + courseId);
String url = buildCanvasUrl("courses/" + courseId + "/external_tools/" + tool.getId(), Collections.emptyMap());
return editExternalTool(url, tool);
}
@Override
public Optional editExternalToolInAccount(String accountId, ExternalTool tool) throws IOException {
LOG.debug("Editing external tool \"" + tool.getName() + "\" in course " + accountId);
String url = buildCanvasUrl("accounts/" + accountId + "/external_tools/" + tool.getId(), Collections.emptyMap());
return editExternalTool(url, tool);
}
private Optional editExternalTool(String url, ExternalTool tool) throws IOException {
if(tool.getId() == null) {
throw new IllegalArgumentException("Tool being edited must have a tool ID");
}
Gson gson = GsonResponseParser.getDefaultGsonParser(serializeNulls);
JsonObject toolJson = gson.toJsonTree(tool).getAsJsonObject();
Response response = canvasMessenger.sendJsonPutToCanvas(oauthToken, url, toolJson);
return responseParser.parseToObject(ExternalTool.class, response);
}
public Optional deleteExternalToolInCourse(String courseId, Integer toolId) throws IOException {
LOG.debug("Deleting external tool " + toolId + " from course " + courseId);
String url = buildCanvasUrl("courses/" + courseId + "/external_tools/" + toolId, Collections.emptyMap());
Response response = canvasMessenger.deleteFromCanvas(oauthToken, url, Collections.emptyMap());
return responseParser.parseToObject(ExternalTool.class, response);
}
public Optional deleteExternalToolInAccount(String accountId, Integer toolId) throws IOException {
LOG.debug("Deleting external tool " + toolId + " from account " + accountId);
String url = buildCanvasUrl("accounts/" + accountId + "/external_tools/" + toolId, Collections.emptyMap());
Response response = canvasMessenger.deleteFromCanvas(oauthToken, url, Collections.emptyMap());
return responseParser.parseToObject(ExternalTool.class, response);
}
/**
* Ensure that a tool object is valid for creation. The API requires certain fields to be filled out.
* Throws an IllegalArgumentException if the conditions are not met.
* @param tool The external tool object we are trying to create
*/
private void ensureToolValidForCreation(ExternalTool tool) {
//check for the unconditionally required fields
if(StringUtils.isAnyBlank(tool.getName(), tool.getPrivacyLevel(), tool.getConsumerKey(), tool.getSharedSecret())) {
throw new IllegalArgumentException("External tool requires all of the following for creation: name, privacy level, consumer key, shared secret");
}
//check that there is either a URL or a domain. One or the other is required
if(StringUtils.isBlank(tool.getUrl()) && StringUtils.isBlank(tool.getDomain())) {
throw new IllegalArgumentException("External tool requires either a URL or domain for creation");
}
}
@Override
protected Type listType() {
return new TypeToken>(){}.getType();
}
@Override
protected Class objectType() {
return ExternalTool.class;
}
}