All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evotext.clever.CleverTeachersClient Maven / Gradle / Ivy

Go to download

The Clever-Java project is a Java wrapper for the Clever.com API for keeping educational applications rostered and up-to-date.

There is a newer version: 2.0.0
Show newest version
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.JSONException;
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.Section;
import com.evotext.clever.model.Student;
import com.evotext.clever.model.Teacher;
import com.evotext.clever.requests.ListRequest;
import com.evotext.clever.util.Connect;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * The CleverTeachersClient class is used to execute Clever's "Teachers" API end points.
 *  
 * @author brendanclemenzi
 */
public class CleverTeachersClient extends CleverClient
{
    private String m_districtOAuthToken;
    
    /**
     * 
     */
    public CleverTeachersClient(String districtOAuthToken)
    {
        this.m_districtOAuthToken = districtOAuthToken;
    }
    
    /**
     * 
     * @param districtOAuthToken
     * @param teacherId
     * @return
     * @throws Exception
     */
    public Teacher getTeacher(String teacherId) throws Exception
    {
        StringBuffer fullApiUrl = new StringBuffer();
        fullApiUrl.append(getBaseUrl());
        fullApiUrl.append("teachers/" + teacherId);
        
        Map parameters = new HashMap();
                
        JSONObject responseJSON = get(fullApiUrl.toString(), this.m_districtOAuthToken, parameters);
        
        ObjectMapper mapper = new ObjectMapper();
        
        Teacher objectValue = mapper.readValue(responseJSON.getString("data"),  Teacher.class);
        
        return objectValue;
    }
    
    /**
     * 
     * @throws Exception
     */
    public BigInteger countTeacherSections(String teacherId) throws Exception
    {
        StringBuffer fullApiUrl = new StringBuffer();
        fullApiUrl.append(getBaseUrl());
        fullApiUrl.append("teachers/");
        fullApiUrl.append(teacherId);
        fullApiUrl.append("/sections");
        
        Map parameters = new HashMap();
        parameters.put("count", true);
                
        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 teacherId
     * @param limit
     * @param startingAfter
     * @return
     * @throws Exception
     */
    public List
getTeacherSections(String teacherId, int limit, String startingAfter, String endingBefore) throws Exception { StringBuffer fullApiUrl = new StringBuffer(); fullApiUrl.append(getBaseUrl()); fullApiUrl.append("teachers/"); fullApiUrl.append(teacherId); 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 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 districtId * @param limit * @param startingAfter * @return * @throws Exception */ public List getTeacherStudents(String teacherId, int limit, String startingAfter, String endingBefore) throws Exception { StringBuffer fullApiUrl = new StringBuffer(); fullApiUrl.append(getBaseUrl()); fullApiUrl.append("teachers/"); fullApiUrl.append(teacherId); 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 getTeachers(int limit, String startingAfter, String endingBefore) throws Exception { StringBuffer fullApiUrl = new StringBuffer(); fullApiUrl.append(getBaseUrl()); 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 getAllTeachers() throws Exception { StringBuffer fullApiUrl = new StringBuffer(); fullApiUrl.append(getBaseUrl()); 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(); Data data = mapper.readValue(responseJSON.toString(), Data.class); List linkList = data.getLinks(); 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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy