okfn / ckanclient-deprecated

DEPRECATED - please see https://github.com/ckan/ckanapi. [Python client library for CKAN]
http://pypi.python.org/pypi/ckanclient
25 stars 17 forks source link

upload_file method does not support non-ASCII content #12

Open pudo opened 11 years ago

pudo commented 11 years ago

The upload_file method uses a faulty snippet of ActiveState code that will choke on any non-ASCII (or, god forbid, binary) content.

I'm using requests.post() as a workaround for now, but it would be nice for this functionality to actually work.

pudo commented 11 years ago

fwiw this is my workaround:

570 ts = datetime.isoformat(datetime.now()).replace(':','').split('.')[0] 571 norm_name = os.path.basename(file_path).replace(' ', '-') 572 file_key = os.path.join(ts, norm_name) 573 574 auth_dict = self.storage_auth_get('/form/'+file_key, {}) 575 #print auth_dict 576 577 h = urlparse.urlparse(self.base_location).netloc 578 u = urlparse.urlparse(auth_dict['action']) 579 import requests 580 url = 'http://' + h + u.path 581 res = requests.post(url, 582 data={'key': file_key}, 583 files={'file': (os.path.basename(file_key), open(file_path, 'rb'))}) 584 errcode = res.status_code 585 errmsg = '' 586 if errcode == 200: 587 return 'http://%s/storage/f/%s' % (u.netloc, file_key), '' 588 else: 589 return '', errmsg