edu.ksu.canvas.impl.AccountImpl 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.AccountReader;
import edu.ksu.canvas.interfaces.CanvasWriter;
import edu.ksu.canvas.model.Account;
import edu.ksu.canvas.net.Response;
import edu.ksu.canvas.net.RestClient;
import edu.ksu.canvas.oauth.OauthToken;
import edu.ksu.canvas.requestOptions.GetSubAccountsOptions;
import edu.ksu.canvas.requestOptions.ListAccountOptions;
import edu.ksu.canvas.util.CanvasURLBuilder;
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.Optional;
public class AccountImpl extends BaseImpl implements AccountReader {
private static final Logger LOG = LoggerFactory.getLogger(AccountImpl.class);
public AccountImpl(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 getSingleAccount(String accountId) throws IOException {
LOG.debug("getting account " + accountId);
String url = buildCanvasUrl("accounts/" + accountId, Collections.emptyMap());
Response response = canvasMessenger.getSingleResponseFromCanvas(oauthToken, url);
if (response.getErrorHappened() || response.getResponseCode() != 200) {
return Optional.empty();
}
return responseParser.parseToObject(Account.class, response);
}
@Override
public List listAccounts(ListAccountOptions options) throws IOException {
LOG.debug("Listing accounts for current user ");
String url = buildCanvasUrl("accounts", options.getOptionsMap());
return getListFromCanvas(url);
}
@Override
public List getSubAccounts(GetSubAccountsOptions options) throws IOException {
LOG.debug("Getting list of sub-accounts for account " + options.getAccountId());
String url = buildCanvasUrl("accounts/" + options.getAccountId() + "/sub_accounts", options.getOptionsMap());
return getListFromCanvas(url);
}
@Override
public List listAccountsForCourseAdmins() throws IOException {
LOG.debug("Getting list of accounts by admin course enrollments");
String url = buildCanvasUrl("course_accounts", Collections.emptyMap());
return getListFromCanvas(url);
}
@Override
protected Type listType() {
return new TypeToken>(){}.getType();
}
@Override
protected Class objectType() {
return Account.class;
}
}