#!/usr/bin/env python3 import requests import json import os # Helper file to facilitate calls to the greatschools.org API # Endpoint: nearby-schools def get_nearby_schools(key: str, lat: str, lon: str, dist: str): ''' Returns a dictionary of schools received from the nearby schools endpoint. Parameters: key (str): API key lat (str): latitude lon (str): longitude dist (str): radius of search Returns: schools (dict): Collated response from API ''' url = 'https://gs-api.greatschools.org/nearby-schools' params = { 'lat': lat, 'lon': lon, 'school_type': "public", 'distance': dist, } headers = { "x-api-key": key } # Construct loop to collate pages schools = [] count = 0 request_limit = 50 non_empty = True while count <= request_limit and non_empty: params['page'] = str(count) r = requests.get(url=url, params=params, headers=headers).json() if len(r['schools']) == 0: non_empty = False else: for school in r['schools']: schools.append(school) count = count + 1 return schools # Endpoint: demographics def get_demographics(key: str, universal_id: str): ''' Returns a dictionary of schools received from the nearby schools endpoint. Parameters: key (str): API key lat (str): latitude lon (str): longitude dist (str): radius of search Returns: schools (dict): Collated response from API ''' url = f'https://gs-api.greatschools.org/schools/' + '{universal_id}' + '/metrics' params = { } headers = { "x-api-key": key } # make request r = requests.get(url=url, params=params, headers=headers).json() print(r)