Skip to content
papajohn edited this page Sep 29, 2014 · 3 revisions

An example of how to grab data from the server if your @berkeley.edu account has staff access to a class.

Assume, e.g., that you want all submissions for a list of email accounts from your section.

First, download a copy of ok and place it along with your analysis script. You can authenticate with

import sys
import zipimport

sys.path.insert(0, 'ok')
from ok import authenticate
TOKEN = authenticate()

Next, you need to decide which fields to keep from the data you request. You can specify a dictionary to do so. For example, the FIELDS dictionary below specifies fields from a submission and its subfields.

FIELDS = {'created':'true',
          'id':'true',
          'submitter': {'id':'true'},
          'messages': 'true',
          'assignment': {'name':'proj1'}}

You can see the fields of a model in model.py

https://github.com/Cal-CS-61A-Staff/ok/blob/master/server/app/models.py

Next, specify an endpoint

ENDPOINT = 'http://ok-server.appspot.com/api/v1/submission'

Next, choose the parameters of your query, which can be found in api.py in each sub-class of an APIResource. For example, the following params grab the first 100 submissions (in however the server decides to order them).

params = {'access_token': TOKEN,
          'format': 'json',
          'fields': json.dumps(FIELDS),
          'page': 1,
          'num_page': 100 }

Pagination is required or the server will run out of memory. Please only request 1000 at a time, using the num_page parameter. You must request pages in order, or you will not get the data you want. Pagination is implemeneted through memcache, a temporary time-limited data store. In order to query for page n, you must have already requested page n-1.

In addition, you can specify additional constraints to specify your query.

params.update({'submitter': 'denero@berkeley.edu'})

Finally, you can make a query:

data = requests.get(ENDPOINT, params=params).json()['data']

The response will have data, message, and status. If status is not 200, then message will tell you the error that has occurred. (Internal server error as a message means that you should file a bug.)

For example, the response that comes back for https://ok-server.appspot.com/api/v1/user/denero@berkeley.edu is

{
"data": {
    "courses": [], 
    "email": "denero@berkeley.edu", 
    "first_name": null, 
    "id": "denero@berkeley.edu", 
    "last_name": null, 
    "login": null, 
    "role": "admin"
}, 
"message": "", 
"status": 200
}