Use urllib2 and better error handling

This commit is contained in:
Adrià Mercader 2011-03-10 09:45:16 +00:00
parent bc8c69b015
commit 572650d26a
1 changed files with 27 additions and 13 deletions

View File

@ -1,5 +1,5 @@
import simplejson as json import simplejson as json
import urllib import urllib2
import ckan.lib.helpers as h import ckan.lib.helpers as h
from ckan.lib.base import BaseController, c, g, request, \ from ckan.lib.base import BaseController, c, g, request, \
response, session, render, config, abort response, session, render, config, abort
@ -9,20 +9,29 @@ 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): def index(self):
""" """
TODO: error handling TODO: error handling
""" """
# Request all harvesting sources # Request all harvesting sources
sources_url = self.api_url + '/harvestsource' 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' source_url = sources_url + '/%s'
sources = [] sources = []
for source_id in sources_ids: for source_id in sources_ids:
r = urllib.urlopen(source_url % source_id).read() doc = self._request_document(source_url % source_id)
sources.append(json.loads(r)) sources.append(json.loads(doc))
c.sources = sources c.sources = sources
return render('ckanext/harvest/index.html') return render('ckanext/harvest/index.html')
@ -31,19 +40,24 @@ class ViewController(BaseController):
# This is the DGU form API, so we don't use self.api_url # This is the DGU form API, so we don't use self.api_url
form_url = config.get('ckan.api_url', '/').rstrip('/') + \ form_url = config.get('ckan.api_url', '/').rstrip('/') + \
'/api/2/form/harvestsource/create' '/api/2/form/harvestsource/create'
if request.method == 'GET': if request.method == 'GET':
c.form = urllib.urlopen(form_url).read() c.form = self._request_document(form_url)
return render('ckanext/harvest/create.html') return render('ckanext/harvest/create.html')
if request.method == 'POST': 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(int(request.environ['CONTENT_LENGTH']))
raw_post_data = request.environ['wsgi.input'].read() 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()) return str(r.getcode())
def show(self,id): def show(self,id):
sources_url = self.api_url + '/harvestsource/%s' % id sources_url = self.api_url + '/harvestsource/%s' % id
r = urllib.urlopen(sources_url).read() doc = self._request_document(sources_url)
c.source = json.loads(r) c.source = json.loads(doc)
return render('ckanext/harvest/show.html') return render('ckanext/harvest/show.html')