From 572650d26a79969b3ad7732499dc3e03a694c2fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Mercader?= Date: Thu, 10 Mar 2011 09:45:16 +0000 Subject: [PATCH] Use urllib2 and better error handling --- ckanext/harvest/controllers/view.py | 40 +++++++++++++++++++---------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/ckanext/harvest/controllers/view.py b/ckanext/harvest/controllers/view.py index aa3d659..c84e68f 100644 --- a/ckanext/harvest/controllers/view.py +++ b/ckanext/harvest/controllers/view.py @@ -1,5 +1,5 @@ import simplejson as json -import urllib +import urllib2 import ckan.lib.helpers as h from ckan.lib.base import BaseController, c, g, request, \ response, session, render, config, abort @@ -7,43 +7,57 @@ from ckan.lib.base import BaseController, c, g, request, \ class ViewController(BaseController): - api_url = config.get('ckan.api_url', '/').rstrip('/')+'/api/2/rest' + api_url = config.get('ckan.api_url', '/').rstrip('/')+'/api/2/rest' + def _request_document(self,url,data = False): + try: + doc = urllib2.urlopen(url).read() + return doc + except urllib2.URLError: + #TODO: log? + abort(500) + def index(self): """ TODO: error handling """ # Request all harvesting sources sources_url = self.api_url + '/harvestsource' - r = urllib.urlopen(sources_url).read() - sources_ids = json.loads(r) + doc = self._request_document(sources_url) + sources_ids = json.loads(doc) + source_url = sources_url + '/%s' sources = [] for source_id in sources_ids: - r = urllib.urlopen(source_url % source_id).read() - sources.append(json.loads(r)) - + doc = self._request_document(source_url % source_id) + sources.append(json.loads(doc)) + c.sources = sources return render('ckanext/harvest/index.html') - + def create(self): # This is the DGU form API, so we don't use self.api_url form_url = config.get('ckan.api_url', '/').rstrip('/') + \ '/api/2/form/harvestsource/create' + if request.method == 'GET': - c.form = urllib.urlopen(form_url).read() + c.form = self._request_document(form_url) return render('ckanext/harvest/create.html') if request.method == 'POST': + """ + TODO: Authz + """ + #raw_post_data = request.environ['wsgi.input'].read(int(request.environ['CONTENT_LENGTH'])) raw_post_data = request.environ['wsgi.input'].read() - r = urllib.urlopen(form_url,raw_post_data) + r = urllib2.urlopen(form_url,raw_post_data) return str(r.getcode()) def show(self,id): - sources_url = self.api_url + '/harvestsource/%s' % id - r = urllib.urlopen(sources_url).read() - c.source = json.loads(r) + sources_url = self.api_url + '/harvestsource/%s' % id + doc = self._request_document(sources_url) + c.source = json.loads(doc) return render('ckanext/harvest/show.html')