From 05094090afe15eb81783233fc0a009cbd34833ed Mon Sep 17 00:00:00 2001 From: Konrad Reiche Date: Wed, 22 May 2013 16:46:14 +0200 Subject: [PATCH] Change type of the API version to integer The CKAN logic uses integers when dealing with the API version, e.g. making checks which API version is in use. Currently, the harvester uses strings to identify the API version. Instead of dealing with type conversion the harvester could use integers directly. This commit fixes okfn/ckanext-harvest#36. When the API version is parsed from the configuration it is passed through the int() function. This way the harvesting will still work even if a harvest source was configured with a string API version which makes this commit backward compatible. Signed-off-by: Konrad Reiche --- README.rst | 6 +++--- ckanext/harvest/harvesters/base.py | 6 +++--- ckanext/harvest/harvesters/ckanharvester.py | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.rst b/README.rst index 660846b..5ce5223 100644 --- a/README.rst +++ b/README.rst @@ -151,8 +151,8 @@ The CKAN harvesters support a number of configuration options to control their behaviour. Those need to be defined as a JSON object in the configuration form field. The currently supported configuration options are: -* api_version: You can force the harvester to use either version '1' or '2' of - the CKAN API. Default is '2'. +* api_version: You can force the harvester to use either version 1 or 2 of + the CKAN API. Default is 2. * default_tags: A list of tags that will be added to all harvested datasets. Tags don't need to previously exist. @@ -204,7 +204,7 @@ Here is an example of a configuration object (the one that must be entered in the configuration field):: { - "api_version":"1", + "api_version": 1, "default_tags":["new-tag-1","new-tag-2"], "default_groups":["my-own-group"], "default_extras":{"new_extra":"Test","harvest_url":"{harvest_source_url}/dataset/{dataset_id}"}, diff --git a/ckanext/harvest/harvesters/base.py b/ckanext/harvest/harvesters/base.py index 9a6a9df..f14c250 100644 --- a/ckanext/harvest/harvesters/base.py +++ b/ckanext/harvest/harvesters/base.py @@ -130,11 +130,11 @@ class HarvesterBase(SingletonPlugin): # Check API version if self.config: - api_version = self.config.get('api_version','2') + api_version = int(self.config.get('api_version', 2)) #TODO: use site user when available - user_name = self.config.get('user',u'harvest') + user_name = self.config.get('user', u'harvest') else: - api_version = '2' + api_version = 2 user_name = u'harvest' context = { diff --git a/ckanext/harvest/harvesters/ckanharvester.py b/ckanext/harvest/harvesters/ckanharvester.py index 3920cf7..214fc04 100644 --- a/ckanext/harvest/harvesters/ckanharvester.py +++ b/ckanext/harvest/harvesters/ckanharvester.py @@ -20,13 +20,13 @@ class CKANHarvester(HarvesterBase): ''' config = None - api_version = '2' + api_version = 2 def _get_rest_api_offset(self): - return '/api/%s/rest' % self.api_version + return '/api/%d/rest' % self.api_version def _get_search_api_offset(self): - return '/api/%s/search' % self.api_version + return '/api/%d/search' % self.api_version def _get_content(self, url): http_request = urllib2.Request( @@ -53,7 +53,7 @@ class CKANHarvester(HarvesterBase): self.config = json.loads(config_str) if 'api_version' in self.config: - self.api_version = self.config['api_version'] + self.api_version = int(self.config['api_version']) log.debug('Using config: %r', self.config) else: @@ -260,7 +260,7 @@ class CKANHarvester(HarvesterBase): try: data_dict = {'id': group_name} group = get_action('group_show')(context, data_dict) - if self.api_version == '1': + if self.api_version == 1: validated_groups.append(group['name']) else: validated_groups.append(group['id'])