def upload(api_key, api_secret, project_id, filename, file): timestamp, dev_hash = _authenticate(api_secret) form = MultiPartForm() form.add_field('api_key', api_key) form.add_field('timestamp', timestamp) form.add_field('dev_hash', dev_hash) form.add_field('is_keeping_all_strings', 'true') form.add_field('file_format', 'IOS_STRINGS') form.add_file('file', filename, fileHandle=file) body = str(form) request = urllib2.Request( 'https://platform.api.onesky.io/1/projects/%s/files' % (project_id)) request.add_header('content-type', form.get_content_type()) request.add_header('content-length', len(body)) request.add_data(body) try: response = urllib2.urlopen(request) #print response.read() except urllib2.HTTPError as e: raise Exception(e.read())
def add_file(self, fields, filename, content, content_type='application/octet-stream', commit=True): """ Post the content from `file_handle` to Solr Cell's ExtractingRequestHandler. The keys in the params will be encoded into the request url and the data from file_handle will posted as a multipart content. """ start_time = time.time() self.log.debug("Starting to build add request...") form = MultiPartForm() # user speicified fields like `id` should be translated into # `literal.id` first, then set it as part of the form-data. for key in fields.iterkeys(): form.add_field('literal.%s' % key, fields[key]) if commit: form.add_field('commit', 'true') form.add_file('file', filename, content, content_type) request = urllib2.Request(self.url + 'update/extract') body = str(form) request.add_header('Content-type', form.get_content_type()) request.add_header('Content-length', len(body)) request.add_data(body) try: start_time = time.time() response = urllib2.urlopen(request).read() end_time = time.time() self.log.info("Finished '%s' (%s) with body '%s' in %0.3f seconds." % (filename, 'POST', str(body)[:10], end_time - start_time)) except AttributeError: # For httplib2. error_message = "Failed to connect to server at '%s'. Are you sure '%s' is correct? Checking it in a browser might help..." % (url, self.base_url) self.log.error(error_message) raise SolrError(error_message)
def send_request(tmpzip, data, url, interface="Command Line"): """ Encodes all the data into a Http request where the album will be uploaded it returns the response as a string. If there is a server error, it will return the HTML of that error. If it's a success, the server returns "#### bytes recieved from server" """ tmpzip.seek(0) mpform = MultiPartForm() mpform.add_field('artist', str(data['artist'])) mpform.add_field('album', str(data['album'])) mpform.add_field('meta', str(data['meta'] or "")) mpform.add_field('date', str(data['date'])) mpform.add_field('password', str(data['password'])) mpform.add_field('profile', str(data['profile'])) mpform.add_file('file', 'album.zip', tmpzip) body = str(mpform) request = urllib2.Request(url + "/upload") ua = USER_AGENT.format(interface=interface) request.add_header('User-agent', ua) request.add_header('Content-type', mpform.get_content_type()) request.add_header('Content-length', len(body)) request.add_data(body) try: return urllib2.urlopen(request).read() except Exception, e: return e.read()