edu.ksu.canvas.impl.UserImpl 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
The newest version!
package edu.ksu.canvas.impl;
import com.google.gson.reflect.TypeToken;
import edu.ksu.canvas.constants.CanvasConstants;
import edu.ksu.canvas.exception.InvalidOauthTokenException;
import edu.ksu.canvas.interfaces.UserReader;
import edu.ksu.canvas.interfaces.UserWriter;
import edu.ksu.canvas.model.User;
import edu.ksu.canvas.net.Response;
import edu.ksu.canvas.net.RestClient;
import edu.ksu.canvas.oauth.OauthToken;
import edu.ksu.canvas.requestOptions.CreateUserOptions;
import edu.ksu.canvas.requestOptions.GetUsersInAccountOptions;
import edu.ksu.canvas.requestOptions.GetUsersInCourseOptions;
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 UserImpl extends BaseImpl implements UserReader, UserWriter{
private static final Logger LOG = LoggerFactory.getLogger(UserImpl.class);
public UserImpl(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 createUser(User user) throws InvalidOauthTokenException, IOException {
return createUser(user, new CreateUserOptions());
}
@Override
public Optional createUser(User user, CreateUserOptions options) throws InvalidOauthTokenException, IOException {
String createdUrl = buildCanvasUrl("accounts/" + CanvasConstants.ACCOUNT_ID + "/users", Collections.emptyMap());
LOG.debug("create URl for user creation : {}", createdUrl);
Map> parameterMap = options.getOptionsMap();
parameterMap.putAll(user.toPostMap(serializeNulls));
Response response = canvasMessenger.sendToCanvas(oauthToken, createdUrl, parameterMap);
if (response.getErrorHappened() || (response.getResponseCode() != 200)) {
LOG.debug("Failed to create user, error message: {}", response);
return Optional.empty();
}
return responseParser.parseToObject(User.class, response);
}
@Override
public Optional updateUser(User user) throws InvalidOauthTokenException, IOException {
if(user == null || user.getId() == 0) {
throw new IllegalArgumentException("User to update must not be null and have a Canvas ID assigned");
}
LOG.debug("updating user in Canvas: {}", user.getId());
String url = buildCanvasUrl("users/" + String.valueOf(user.getId()), Collections.emptyMap());
//I tried to use the JSON POST method but Canvas throws odd permission errors when trying to serialize the user
//object, even for an admin user. I suspect some field within the User object can't be updated and instead of
//ignoring it like most calls, the user call throws a permission error. So I had to fall back to sending a form POST
//since it only serializes fields with a @CanvasField annotation
Response response = canvasMessenger.putToCanvas(oauthToken, url, user.toPostMap(serializeNulls));
return responseParser.parseToObject(User.class, response);
}
@Override
public List getUsersInCourse(GetUsersInCourseOptions options) throws IOException {
LOG.debug("Retrieving users in course {}", options.getCourseId());
String url = buildCanvasUrl("courses/" + options.getCourseId() + "/users", options.getOptionsMap());
return getListFromCanvas(url);
}
@Override
public Optional showUserDetails(String userIdentifier) throws IOException {
LOG.debug("Retrieving details for user {}", userIdentifier);
String url = buildCanvasUrl("users/" + userIdentifier, Collections.emptyMap());
Response response = canvasMessenger.getSingleResponseFromCanvas(oauthToken, url);
return responseParser.parseToObject(User.class, response);
}
@Override
public List getUsersInAccount(GetUsersInAccountOptions options) throws IOException {
LOG.debug("Retrieving users for account {}", options.getAccountId());
String url = buildCanvasUrl("accounts/" + options.getAccountId() + "/users", options.getOptionsMap());
return getListFromCanvas(url);
}
@Override
protected Type listType() {
return new TypeToken>() {
}.getType();
}
@Override
protected Class objectType() {
return User.class;
}
}