Skip to content

Instantly share code, notes, and snippets.

@timabell
Last active March 12, 2019 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timabell/16926c828ca33a178f0f0adc5dae4f76 to your computer and use it in GitHub Desktop.
Save timabell/16926c828ca33a178f0f0adc5dae4f76 to your computer and use it in GitHub Desktop.
Quick script for test the provider/course api exposed by https://github.com/DFE-Digital/manage-courses-backend/
#!/usr/bin/env python
# iterate through providers
# usage, env vars to override defaults:
# token=authtoken urlbase="http://someserver/" endpoint="api/v1/2019/providers" startat="?changed_since=2019-03-11T15%3A47%3A57.548105Z&per_page=100" ./test-ucas-endpoint.py
# https://gist.github.com/timabell/16926c828ca33a178f0f0adc5dae4f76
# MIT License
# Copyright (c) 2018 Department for Education - Digital
import urllib2
import os
import pdb
import json
endpoint = os.environ.get('endpoint') or "api/v1/2019/providers"
urlbase = os.environ.get('urlbase') or "http://localhost:3001/"
startat = os.environ.get('startat') or ""
url=urlbase+endpoint+startat
def getpage(url):
req = urllib2.Request(url)
token = os.environ.get('token') or "bats"
auth = 'Bearer %s' % (token)
req.add_header('Authorization', auth)
print "get: " + url
r = urllib2.urlopen(req)
body = r.read()
result = json.loads(body)
info = r.info()
nextlinkheader = info['Link']
nextlink = nextlinkheader.split(";")[0]
# pdb.set_trace()
return result, nextlink
page = 1
count = 1
nextlink = url
while count > 0:
print "------------"
print "page %d" % page
print "------------"
result, nextlink = getpage(nextlink)
count = len(result)
print "%d records returned" % count
print "next link: " + nextlink
page = page + 1
@defong
Copy link

defong commented Feb 20, 2019

Moving away from bash, excellent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment