edu.ksu.canvas.impl.QuizImpl 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.reflect.TypeToken;
import edu.ksu.canvas.interfaces.QuizReader;
import edu.ksu.canvas.interfaces.QuizWriter;
import edu.ksu.canvas.model.assignment.Quiz;
import edu.ksu.canvas.net.Response;
import edu.ksu.canvas.net.RestClient;
import edu.ksu.canvas.oauth.OauthToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class QuizImpl extends BaseImpl implements QuizReader, QuizWriter {
private static final Logger LOG = LoggerFactory.getLogger(QuizReader.class);
public QuizImpl(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 getSingleQuiz(String courseId, String quizId) throws IOException {
LOG.debug("Retrieving single quiz " + quizId + " in course " + courseId);
String url = buildCanvasUrl("courses/" + courseId + "/quizzes/" + quizId, Collections.emptyMap());
Response response = canvasMessenger.getSingleResponseFromCanvas(oauthToken, url);
return responseParser.parseToObject(Quiz.class, response);
}
@Override
public List getQuizzesInCourse(String courseId) throws IOException {
LOG.debug("Getting quizzes for course " + courseId);
String url = buildCanvasUrl("courses/" + courseId + "/quizzes", Collections.emptyMap());
List responses = canvasMessenger.getFromCanvas(oauthToken, url);
return parseQuizList(responses);
}
@Override
public Optional updateQuiz(Quiz quiz, String courseId) throws IOException {
LOG.debug("Updating quiz " + quiz.getId() + " in course " + courseId);
String url = buildCanvasUrl("courses/" + courseId + "/quizzes/" + quiz.getId(), Collections.emptyMap());
Response response = canvasMessenger.sendJsonPutToCanvas(oauthToken, url,quiz.toJsonObject(serializeNulls));
return responseParser.parseToObject(Quiz.class, response);
}
private List parseQuizList(final List responses) {
return responses.stream().
map(this::parseQuizList).
flatMap(Collection::stream).
collect(Collectors.toList());
}
private List parseQuizList(final Response response) {
Type listType = new TypeToken>(){}.getType();
return GsonResponseParser.getDefaultGsonParser(serializeNulls).fromJson(response.getContent(), listType);
}
@Override
protected Type listType() {
return new TypeToken>(){}.getType();
}
@Override
protected Class objectType() {
return Quiz.class;
}
}