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 <konrad.reiche@fokus.fraunhofer.de>
This commit is contained in:
Konrad Reiche 2013-05-22 16:46:14 +02:00
parent ff7287d4b4
commit 05094090af
3 changed files with 11 additions and 11 deletions

View File

@ -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}"},

View File

@ -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 = {

View File

@ -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'])