Created
February 20, 2015 14:36
-
-
Save pavelk2/c5c7ca03741046957630 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 requests | |
import json | |
class Flower: | |
DEFAULT_API_URL = 'https://api.crowdflower.com/v1/' | |
DEFAULT_API_HEADERS = {'content-type': 'application/json'} | |
def __init__(self, api_key, api_url = DEFAULT_API_URL, api_headers = DEFAULT_API_HEADERS): | |
self.api_key = api_key | |
self.api_url = api_url | |
self.api_headers = api_headers | |
def _getRequestUrl(self, type, id): | |
extention = '' | |
if type == 'job': | |
extention = 'jobs/'+str(id)+'.json' | |
if type == 'units': | |
extention = 'jobs/'+str(id)+'/units.json' | |
return self.api_url + extention + "?key=" + self.api_key | |
def updateJob(self,job_id, attrs): | |
request_url = self._getRequestUrl('job',job_id) | |
data = { | |
'job':attrs | |
} | |
return requests.put(request_url, data = json.dumps(data), headers=self.api_headers) | |
def uploadUnit(self,job_id, data): | |
request_url = self._getRequestUrl('units',job_id) | |
data = { | |
'unit':{ | |
'data':data | |
} | |
} | |
return requests.post(request_url, data = json.dumps(data), headers=self.api_headers) | |
def parseWebhook(self, response_data): | |
if response_data['signal'] == 'unit_complete': | |
data = json.loads(response_data['payload']) | |
return data['results']['judgements'] | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment