def reverse_apicontroller_action(url, status, response): """ Make an API call look like a direct action call by reversing the exception -> HTTP response translation that ApiController.action does """ try: parsed = json.loads(response) if parsed.get('success'): return parsed['result'] if hasattr(parsed, 'get'): err = parsed.get('error', {}) else: err = {} except (AttributeError, ValueError): err = {} etype = err.get('__type') emessage = err.get('message', '').split(': ', 1)[-1] if etype == 'Search Query Error': # I refuse to eval(emessage), even if it would be more correct raise SearchQueryError(emessage) elif etype == 'Search Error': # I refuse to eval(emessage), even if it would be more correct raise SearchError(emessage) elif etype == 'Search Index Error': raise SearchIndexError(emessage) elif etype == 'Validation Error': raise ValidationError(err) elif etype == 'Not Found Error': raise NotFound(emessage) elif etype == 'Authorization Error': raise NotAuthorized(err) # don't recognize the error raise CKANAPIError(repr([url, status, response]))
def call_action(self, name, data_dict): if name == 'package_show' and data_dict['id'] == 'seekrit': raise NotAuthorized('naughty user') if name == 'package_create' and data_dict['name'] == '34': raise ValidationError({'name': 'That URL is already in use.'}) if name == 'organization_update': if data_dict['id'] == 'used' and data_dict.get('users') != [ 'people']: raise ValidationError({'users': 'should be unchanged'}) if data_dict['id'] == 'unused' and data_dict.get('users') != []: raise ValidationError({'users': 'should be cleared'}) try: return { 'package_show': { '12': {'title': "Twelve"}, '30ish': {'id': '34', 'title': "Thirty-four"}, '34': {'id': '34', 'title': "Thirty-four"}, }, 'group_show': { 'ab': {'title': "ABBA"}, }, 'organization_show': { 'cd': {'id': 'cd', 'title': "Super Trouper"}, 'used': {'users': ['people']}, 'unused': {'users': ['people']}, }, 'package_create': { None: {'name': 'something-new'}, }, 'package_update': { '34': {'name': 'something-updated'}, }, 'group_update': { 'ab': {'name': 'group-updated'}, }, 'organization_update': { 'cd': {'name': 'org-updated'}, 'used': {'name': 'users-unchanged'}, 'unused': {'name': 'users-cleared'}, }, 'organization_create': { None: {'name': 'org-created'}, }, }[name][data_dict.get('id')] except KeyError: raise NotFound()