Last active
August 29, 2015 14:20
-
-
Save heyitsjames/994ab1b98ed8f48b1ba5 to your computer and use it in GitHub Desktop.
Clever Sandbox API Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
import statistics | |
class CleverAPI(): | |
AUTH_HEADER = {'Authorization': 'Bearer DEMO_TOKEN'} | |
CLEVER_URL = 'https://api.clever.com' | |
def __init__(self, auth_header=None): | |
if auth_header: | |
self.AUTH_HEADER = auth_header | |
def http_get(self, route): | |
request = requests.get( | |
'{0}/{1}'.format(self.CLEVER_URL, route), | |
headers=self.AUTH_HEADER) | |
return json.loads(request.text) | |
def get_students_in_section(self, section_id): | |
return self.http_get( | |
'/v1.1/sections/{0}/students'.format(section_id))['data'] | |
def get_all_sections(self, url='/v1.1/sections'): | |
response = self.http_get(url) | |
last_link = response['links'][-1] | |
if last_link['rel'] == 'prev': | |
return response['data'] | |
else: | |
return response['data'] + self.get_all_sections( | |
url=last_link['uri']) | |
def get_avg_num_students_per_section(self): | |
return statistics.mean( | |
[len(self.get_students_in_section(section['data']['id'])) | |
for section in self.get_all_sections()]) | |
# How to test (Python 3.4) | |
api = CleverAPI() | |
print(api.get_avg_num_students_per_section()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment