com.evotext.clever.CleverSchoolsClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of clever-java Show documentation
Show all versions of clever-java Show documentation
The Clever-Java project is a Java wrapper for the Clever.com API for keeping educational applications rostered and up-to-date.
package com.evotext.clever;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.evotext.clever.model.Data;
import com.evotext.clever.model.Link;
import com.evotext.clever.model.Paging;
import com.evotext.clever.model.School;
import com.evotext.clever.model.Section;
import com.evotext.clever.model.Student;
import com.evotext.clever.model.Teacher;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* The CleverSchoolsClient class is used to execute Clever's "Schools" API end points.
*
* @author brendanclemenzi
*/
public class CleverSchoolsClient extends CleverClient
{
private String m_districtOAuthToken;
/**
*
*/
public CleverSchoolsClient(String districtOAuthToken)
{
this.m_districtOAuthToken = districtOAuthToken;
}
/**
*
* @param schoolId
* @return
* @throws Exception
*/
public School getSchool(String schoolId) throws Exception
{
StringBuffer fullApiUrl = new StringBuffer();
fullApiUrl.append(getBaseUrl());
fullApiUrl.append("schools/" + schoolId);
Map parameters = new HashMap();
JSONObject responseJSON = get(fullApiUrl.toString(), this.m_districtOAuthToken, parameters);
ObjectMapper mapper = new ObjectMapper();
School objectValue = mapper.readValue(responseJSON.getString("data"), School.class);
return objectValue;
}
/**
*
* @throws Exception
*/
public BigInteger countSchoolTeachers(String schoolId) throws Exception
{
StringBuffer fullApiUrl = new StringBuffer();
fullApiUrl.append(getBaseUrl());
fullApiUrl.append("schools/");
fullApiUrl.append(schoolId);
fullApiUrl.append("/teachers");
Map parameters = new HashMap();
parameters.put("limit", 1);
JSONObject responseJSON = get(fullApiUrl.toString(), this.m_districtOAuthToken, parameters);
ObjectMapper mapper = new ObjectMapper();
Paging pagingValue = mapper.readValue(responseJSON.getString("paging"), Paging.class);
BigInteger recordCount = new BigInteger(pagingValue.getTotal());
return recordCount;
}
/**
*
* @throws Exception
*/
public List getSchoolTeachers(String schoolId, int limit, String startingAfter, String endingBefore) throws Exception
{
StringBuffer fullApiUrl = new StringBuffer();
fullApiUrl.append(getBaseUrl());
fullApiUrl.append("schools/");
fullApiUrl.append(schoolId);
fullApiUrl.append("/teachers");
Map parameters = new HashMap();
parameters.put("limit", limit);
if(StringUtils.isNotEmpty(startingAfter))
{
parameters.put("starting_after", startingAfter);
}
if(StringUtils.isNotEmpty(endingBefore))
{
parameters.put("ending_before", endingBefore);
}
JSONObject responseJSON = get(fullApiUrl.toString(), this.m_districtOAuthToken, parameters);
JSONArray dataJSON = responseJSON.getJSONArray("data");
ObjectMapper mapper = new ObjectMapper();
List objectList = new ArrayList();
for(int i=0; i getAllSchoolTeachers(String schoolId) throws Exception
{
StringBuffer fullApiUrl = new StringBuffer();
fullApiUrl.append(getBaseUrl());
fullApiUrl.append("schools/");
fullApiUrl.append(schoolId);
fullApiUrl.append("/teachers");
List objectList = new ArrayList();
Map parameters = new HashMap();
parameters.put("limit", 100); // Sets a transaction limit of 100 teachers at a time coming over the line for each page.
String uri = fullApiUrl.toString();
boolean keepGoing = true;
while (keepGoing)
{
keepGoing = false;
JSONObject responseJSON = get(uri, this.m_districtOAuthToken, parameters);
ObjectMapper mapper = new ObjectMapper();
List linkList = this.getLinkList(responseJSON);
for (Link link : linkList)
{
if (StringUtils.equals(link.getRel(), "next"))
{
keepGoing = true;
uri = getApiUrl() + link.getUri();
}
}
JSONArray dataJSON = responseJSON.getJSONArray("data");
for(int i=0; i parameters = new HashMap();
parameters.put("limit", 1);
JSONObject responseJSON = get(fullApiUrl.toString(), this.m_districtOAuthToken, parameters);
ObjectMapper mapper = new ObjectMapper();
Paging pagingValue = mapper.readValue(responseJSON.getString("paging"), Paging.class);
BigInteger recordCount = new BigInteger(pagingValue.getTotal());
return recordCount;
}
/**
*
* @param districtOAuthToken
* @param schoolId
* @param limit
* @param startingAfter
* @return
* @throws Exception
*/
public List getSchoolStudents(String schoolId, int limit, String startingAfter, String endingBefore) throws Exception
{
StringBuffer fullApiUrl = new StringBuffer();
fullApiUrl.append(getBaseUrl());
fullApiUrl.append("schools/");
fullApiUrl.append(schoolId);
fullApiUrl.append("/students");
Map parameters = new HashMap();
parameters.put("limit", limit);
if(StringUtils.isNotEmpty(startingAfter))
{
parameters.put("starting_after", startingAfter);
}
if(StringUtils.isNotEmpty(endingBefore))
{
parameters.put("ending_before", endingBefore);
}
JSONObject responseJSON = get(fullApiUrl.toString(), this.m_districtOAuthToken, parameters);
JSONArray dataJSON = responseJSON.getJSONArray("data");
ObjectMapper mapper = new ObjectMapper();
List objectList = new ArrayList();
for(int i=0; i getAllSchoolStudents(String schoolId) throws Exception
{
StringBuffer fullApiUrl = new StringBuffer();
fullApiUrl.append(getBaseUrl());
fullApiUrl.append("schools/");
fullApiUrl.append(schoolId);
fullApiUrl.append("/students");
List objectList = new ArrayList();
Map parameters = new HashMap();
parameters.put("limit", 100); // Sets a transaction limit of 100 teachers at a time coming over the line for each page.
String uri = fullApiUrl.toString();
boolean keepGoing = true;
while (keepGoing)
{
keepGoing = false;
JSONObject responseJSON = get(uri, this.m_districtOAuthToken, parameters);
ObjectMapper mapper = new ObjectMapper();
List linkList = this.getLinkList(responseJSON);
for (Link link : linkList)
{
if (StringUtils.equals(link.getRel(), "next"))
{
keepGoing = true;
uri = getApiUrl() + link.getUri();
}
}
JSONArray dataJSON = responseJSON.getJSONArray("data");
for(int i=0; i parameters = new HashMap();
parameters.put("limit", 1);
JSONObject responseJSON = get(fullApiUrl.toString(), this.m_districtOAuthToken, parameters);
ObjectMapper mapper = new ObjectMapper();
Paging pagingValue = mapper.readValue(responseJSON.getString("paging"), Paging.class);
BigInteger recordCount = new BigInteger(pagingValue.getTotal());
return recordCount;
}
/**
*
* @param districtOAuthToken
* @param schoolId
* @param limit
* @param startingAfter
* @return
* @throws Exception
*/
public List getSchoolSections(String schoolId, int limit, String startingAfter, String endingBefore) throws Exception
{
StringBuffer fullApiUrl = new StringBuffer();
fullApiUrl.append(getBaseUrl());
fullApiUrl.append("schools/");
fullApiUrl.append(schoolId);
fullApiUrl.append("/sections");
Map parameters = new HashMap();
parameters.put("limit", limit);
if(StringUtils.isNotEmpty(startingAfter))
{
parameters.put("starting_after", startingAfter);
}
if(StringUtils.isNotEmpty(endingBefore))
{
parameters.put("ending_before", endingBefore);
}
JSONObject responseJSON = get(fullApiUrl.toString(), this.m_districtOAuthToken, parameters);
JSONArray dataJSON = responseJSON.getJSONArray("data");
ObjectMapper mapper = new ObjectMapper();
List objectList = new ArrayList();
for(int i=0; i getAllSchoolSections(String schoolId) throws Exception
{
StringBuffer fullApiUrl = new StringBuffer();
fullApiUrl.append(getBaseUrl());
fullApiUrl.append("schools/");
fullApiUrl.append(schoolId);
fullApiUrl.append("/sections");
List objectList = new ArrayList();
Map parameters = new HashMap();
parameters.put("limit", 100); // Sets a transaction limit of 100 teachers at a time coming over the line for each page.
String uri = fullApiUrl.toString();
boolean keepGoing = true;
while (keepGoing)
{
keepGoing = false;
JSONObject responseJSON = get(uri, this.m_districtOAuthToken, parameters);
ObjectMapper mapper = new ObjectMapper();
List linkList = this.getLinkList(responseJSON);
for (Link link : linkList)
{
if (StringUtils.equals(link.getRel(), "next"))
{
keepGoing = true;
uri = getApiUrl() + link.getUri();
}
}
JSONArray dataJSON = responseJSON.getJSONArray("data");
for(int i=0; i