Created
September 8, 2015 17:49
-
-
Save felipebizz/febcd932174a67a0152b to your computer and use it in GitHub Desktop.
Upload via REST to Nuxeo with python
This file contains 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
''' | |
Instalando as depedencias da POC: | |
sudo pip install requests | |
sudo pip install requests_toolbelt | |
''' | |
import os | |
import json | |
import requests | |
import logging | |
log_upload_rest = logging.getLogger('upload_rest') | |
from requests_toolbelt.multipart.encoder import MultipartEncoder | |
### | |
### Variaveis que precisam ser mudadas | |
### | |
file_path = '/home/felipe/arizona.png' | |
file_uuid = '10' | |
file_name = os.path.basename(file_path) | |
nuxeo_url = 'http://192.168.175.93:8080/nuxeo' | |
nuxeo_user = 'Administrator' | |
nuxeo_password = 'Administrator' | |
timeout_seconds = 10 | |
### Upload do arquivo para o nuxeo, sinalizando cm um Batch-Id | |
binary_content = open(file_path, 'rb') | |
with binary_content: | |
multipart = MultipartEncoder([ | |
('file', (os.path.basename(file_path), | |
binary_content, | |
'application/octet-stream'))]) | |
upload_headers = { | |
"Content-Type": multipart.content_type, | |
"X-Batch-Id": file_uuid, | |
"X-File-Idx": "0", | |
"X-File-Name": file_name | |
} | |
upload_response = requests.post( | |
nuxeo_url + '/site/automation/batch/upload', | |
auth=(nuxeo_user, nuxeo_password), | |
data=multipart, | |
headers=upload_headers, | |
allow_redirects=False, | |
timeout=timeout_seconds) | |
print 'POST: /site/automation/batch/upload: %s' % str(upload_response) | |
print 'POST form multipart/form-data: %s' % multipart.to_string() | |
if upload_response.status_code != 200: | |
error_message = 'Error: upload_asset request error '\ | |
'(received http code different of 200): {0}\n{1}\nm: {2}'.\ | |
format(str(upload_response), upload_response.text, multipart.to_string()) | |
print error_message | |
print '\n' | |
### | |
### Verificando o conteudo do upload | |
### | |
checkfile_response = requests.get( | |
nuxeo_url + '/site/automation/batch/files/%s' % file_uuid, | |
auth=(nuxeo_user, nuxeo_password), | |
allow_redirects=False, | |
timeout=timeout_seconds) | |
print 'GET: /site/automation/batch/files/%s: %s' % (file_uuid, str(checkfile_response)) | |
if checkfile_response.status_code != 200: | |
error_message = 'Error: checkfile_response request error '\ | |
'(received http code different of 200): {0}\n{1}'.\ | |
format(str(upload_response), checkfile_response.text) | |
print error_message | |
else: | |
checkfile_json_response = checkfile_response.json() | |
if len(checkfile_json_response) < 1 \ | |
or not 'name' in checkfile_json_response[0].keys() \ | |
or checkfile_json_response[0]['name'] != file_name: | |
print "Error on check upload of file" | |
else: | |
print 'Upload - OK (%s)' % str(checkfile_json_response[0]) | |
print '\n' | |
### | |
### Criar o documento do tipo arquivo e anexar o blob que fizemos upload nele. | |
### | |
attachblob_headers = {"Content-Type": "application/json"} | |
attachblob_payload = { | |
"entity-type": "document", | |
"name": file_name, | |
"type": "File", | |
"properties": { | |
"dc:title": file_name, | |
"file:content": { | |
"upload-batch": file_uuid, | |
"upload-fileId": "0" | |
} | |
} | |
} | |
attachblob_response = requests.post( | |
nuxeo_url + '/api/v1/path/default-domain/workspaces', | |
#nuxeo_url + '/api/v1/repo/arizona/path/files', | |
auth=(nuxeo_user, nuxeo_password), | |
headers=attachblob_headers, | |
data=json.dumps(attachblob_payload), | |
allow_redirects=False, | |
timeout=timeout_seconds) | |
print 'POST: /api/v1/path/arizona/files: %s' % str(attachblob_response) | |
if attachblob_response.status_code != 201: | |
error_message = 'Error: attachblob_response request error '\ | |
'(received http code different of 200): {0}\n{1}'.\ | |
format(str(attachblob_response), attachblob_response.text) | |
print error_message | |
else: | |
print attachblob_response.text | |
print '\n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment