Esempio n. 1
0
def api_dict(config):
    """Accepts a config file, pre-narrowed to user_role.
    Returns a dictionary that can be expanded
    into a readable api.
    """
    result = {res:
              {'meta': meta_for(res), 'urls': []}
              for res in config if res != 'index'
              }
    viewmaps = convert_to_viewmaps(config)
    for resource, res_config in viewmaps.items():
        prefix = (resource is not 'index') and ("/%s/:id" % resource) or ''
        traversals = res_config.get('traversals')
        # First of all, the forward links from here. We're going to make an entry for each in
        # the forward resource's 'urls' key.
        if traversals:
            valid_traversals = [
                t for t
                in traversals
                if t['resource'] in
                helpers.valid_traversals(resource, config)['many']]
            # Each t will be a dictionary such as
            # {'url';'foo','methods':['GET','PUT']}
            for t in valid_traversals:
                # finesse point. We're actually appending these urls to the result for the resource
                # they link to. That is, using the above example, we're appending these urls to
                # result {'foo'['urls']}
                target = t['url']
                for method in t['methods']:
                    url_dict = {'url': '%s/%s' %
                                (prefix, target), 'method': method}
                    target_fields = viewmaps[target]['fields']
                    if method == 'GET':
                        url_dict['fields'] = target_fields['read']
                    elif method == 'POST':
                        url_dict['fields'] = target_fields['write']
                    result[target]['urls'].append(url_dict)
        methods = res_config.get('methods')
        if methods and resource is not "index":
            available_methods = permit.allowed_methods_of(methods)
            for m in available_methods['allowed']:
                url_dict = {'url': '/%s/:id' % resource, 'method': m}
                if m == 'GET':
                    url_dict['fields'] = res_config['fields']['read']
                if m == 'PUT':
                    url_dict['fields'] = res_config['fields']['write']
                result[resource]['urls'].append(url_dict)
    return result
Esempio n. 2
0
 def test_sorting_of_traversals(self):
     self.traversals = helpers.valid_traversals()