def update(self, props): params = [('job' + key, value) for key, value in to_params(props)] logger.debug('Updating Job#%d: %r', self.id, params) res = self._connection.request('/jobs/%s' % self.id, method='PUT', params=params) # reset cached properties self._properties = {} return res
def update(self, props): params = rails_params({'job': props}) logger.debug('Updating Job[%d]: %r', self.id, params) res = self._connection.request('/jobs/%s' % self.id, method='PUT', params=params) # reset cached properties self._cache.remove(keyfunc(self, 'properties')) return res
def upload(self, units): headers = {'Content-Type': 'application/json'} data = '\n'.join(json.dumps(unit) for unit in units) logger.debug('Uploading data to Job[%d]: %s', self.id, data) res = self._connection.request('/jobs/%s/upload' % self.id, method='POST', headers=headers, data=data) # reset cached units self._cache_flush('units') return res
def upload(self, units): headers = {'Content-Type': 'application/json'} data = '\n'.join(json.dumps(unit) for unit in units) logger.debug('Upload data: {}'.format(data)) res = self._connection.request('/jobs/%s/upload' % self.id, method='POST', headers=headers, data=data) # reset cached units self._cache.remove(keyfunc(self, 'units')) return res
def upload(self, units): ''' TODO: allow setting Job parameters at the same time ''' headers = {'Content-Type': 'application/json'} # N.b.: CF expects newline-separated JSON, not actual JSON # e.g., this would fail with a status 500: kwargs['data'] = json.dumps(data) data = '\n'.join(json.dumps(unit) for unit in units) logger.debug('Upload data: {}'.format(data)) job_response = self.request('/jobs/upload', method='POST', headers=headers, data=data) job = Job(job_response['id'], self) job._properties = job_response # bust cache of job_ids self._cache.remove(keyfunc(self, 'job_ids')) return job
def update(self, props): params = rails_params({'job': props}) logger.debug('Updating Job[%d]: %r', self.id, params) try: res = self._connection.request('/jobs/%s' % self.id, method='PUT', params=params) except CrowdFlowerError, exc: # CrowdFlower sometimes likes to redirect the PUT to a non-API page, # which will raise an error (406 Not Accepted), but we can just # ignore the error since it comes after the update is complete. # This is kind of a hack, since we sometimes want to follow redirects # (e.g., with downloads), but following redirects is more properly # not the default if exc.response.status_code != 406: logger.info('Ignoring 406 "Not Accepted" error: %r', exc); else: raise
def update(self, props): params = rails_params({'job': props}) logger.debug('Updating Job[%d]: %r', self.id, params) try: res = self._connection.request('/jobs/%s' % self.id, method='PUT', params=params) except CrowdFlowerError, exc: # CrowdFlower sometimes likes to redirect the PUT to a non-API page, # which will raise an error (406 Not Accepted), but we can just # ignore the error since it comes after the update is complete. # This is kind of a hack, since we sometimes want to follow redirects # (e.g., with downloads), but following redirects is more properly # not the default if exc.response.status_code != 406: logger.info('Ignoring 406 "Not Accepted" error: %r', exc) else: raise
def send_request(self, req): ''' returns requests.Response object raise ''' # requests gotcha: even if send through the session, request.prepare() # does not get merged with the session's attributes, so we have to call # session.prepare_request(...) # merge params with the api key req.params = to_key_val_list(req.params) + [('key', self.api_key)] prepared_req = self._session.prepare_request(req) logger.debug('req.params {} prepare_req {}'.format(req.params, prepared_req)) res = self._session.send(prepared_req) if res.status_code != 200: # CrowdFlower responds with a '202 Accepted' when we request a bulk # download which has not yet been generated, which means we simply # have to wait and try again raise CrowdFlowerError(req, res) return res
def send_request(self, req): ''' returns requests.Response object raise ''' # requests gotcha: even if send through the session, request.prepare() # does not get merged with the session's attributes, so we have to call # session.prepare_request(...) # merge params with the api key req.params = to_key_val_list(req.params) + [('key', self.api_key)] prepared_req = self._session.prepare_request(req) logger.debug('Request params: {}'.format(req.params)) res = self._session.send(prepared_req) if res.status_code != 200: # CrowdFlower responds with a '202 Accepted' when we request a bulk # download which has not yet been generated, which means we simply # have to wait and try again raise CrowdFlowerError(req, res) return res