Example #1
0
 def __init__(self, **kw):
     super(ResourceHandler, self).__init__(**kw)        
     try:
         assert self._meta.name, "name required"
         assert self._meta.path, "path required"
     except AssertionError as e:
         raise exc.dRestResourceError(e.args[0])
         
     self._meta.path = self._meta.path.lstrip('/').rstrip('/')
     
     # instantiate it only if its not
     if not getattr(self._meta.request, '_meta', None):
         if not self._meta.baseurl:
             raise exc.dRestResourceError('baseurl required when passing uninstantiated request handler.')
         self._request = self._meta.request(baseurl=self._meta.baseurl)
     else:
         self._request = self._meta.request
         self._meta.request = self._request.__class__
     request.validate(self._request)
Example #2
0
File: api.py Project: gnomix/drest
 def add_resource(self, name, resource_handler=None, path=None):
     if not path:
         path = '%s' % name
     else:
         path = path.lstrip('/')
         
     if not resource_handler:
         handler = self._meta.resource
     else:
         handler = resource_handler
     
     
     handler = handler(baseurl=self._meta.baseurl, path=path, 
                       name=name, 
                       request=self._request
                       )
     resource.validate(handler)
     if hasattr(self, name):
         raise exc.dRestResourceError(
             "The object '%s' already exist on '%s'" % (name, self))
     setattr(self, name, handler)
     self._resources.append(name)
Example #3
0
File: api.py Project: yrik/drest
    def add_resource(self, name, resource_handler=None, path=None):
        """
        Add a resource handler to the api object.
        
        Required Arguments:
        
            name
                The name of the resource.  This is generally the basic name 
                of the resource on the API.  For example '/api/v0/users/' 
                would likely be called 'users' and will be accessible as
                'api.users' from which additional calls can be made.  For 
                example 'api.users.get()'.  
        
        Optional Arguments:
        
            resource_handler
                The resource handler class to use.  Defaults to 
                self._meta.resource_handler.
            
            path
                The path to the resource on the API (after the base url).
                Defaults to '/<name>/'.
        
        
        Nested Resources:
            
        It is possible to attach resources in a 'nested' fashion.  For example
        passing a name of 'my.nested.users' would be accessible as 
        api.my.nested.users.get().
        
        Usage:
        
        .. code-block:: python
        
            api.add_resource('users')
            response = api.users.get()
            
            # Or for nested resources
            api.add_resource('my.nested.users', path='/users/')
            response = api.my.nested.users.get()
            
        """
        safe_list = ['.', '_']
        for char in name:
            if char in safe_list:
                continue
            if not char.isalnum():
                raise exc.dRestResourceError(
                    "resource name must be alpha-numeric.")

        if not path:
            path = '%s' % name
        else:
            path = path.strip('/')

        if not resource_handler:
            resource_handler = self._meta.resource_handler

        resource.validate(resource_handler)
        handler = resource_handler(self, name, path)
        if hasattr(self, name):
            raise exc.dRestResourceError(
                "The object '%s' already exist on '%s'" % (name, self))

        # break up if nested
        parts = name.split('.')
        if len(parts) == 1:
            setattr(self, name, handler)
        elif len(parts) > 1:
            first = parts.pop(0)
            last = parts.pop()

            # add the first object to self
            setattr(self, first, resource.NestedResource())
            first_obj = getattr(self, first)
            current_obj = first_obj

            # everything in between
            for part in parts:
                setattr(current_obj, part, resource.NestedResource())
                current_obj = getattr(current_obj, part)

            # add the actual resource to the chain of nested objects
            setattr(current_obj, last, handler)

        self._resources.append(name)
Example #4
0
File: api.py Project: eirmag/drest
 def add_resource(self, name, resource_handler=None, path=None):
     """
     Add a resource handler to the api object.
     
     Required Arguments:
     
         name
             The name of the resource.  This is generally the basic name 
             of the resource on the API.  For example '/api/v0/users/' 
             would likely be called 'users' and will be accessible as
             'api.users' from which additional calls can be made.  For 
             example 'api.users.get()'.  
     
     Optional Arguments:
     
         resource_handler
             The resource handler class to use.  Defaults to 
             self._meta.resource_handler.
         
         path
             The path to the resource on the API (after the base url).
             Defaults to '/<name>/'.
     
     
     Nested Resources:
         
     It is possible to attach resources in a 'nested' fashion.  For example
     passing a name of 'my.nested.users' would be accessible as 
     api.my.nested.users.get().
     
     Usage:
     
     .. code-block:: python
     
         api.add_resource('users')
         response = api.users.get()
         
         # Or for nested resources
         api.add_resource('my.nested.users', path='/users/')
         response = api.my.nested.users.get()
         
     """
     safe_list = ['.', '_']
     for char in name:
         if char in safe_list:
             continue
         if not char.isalnum():
             raise exc.dRestResourceError(
                 "resource name must be alpha-numeric."
                 )
     
     if not path:
         path = '%s' % name
     else:
         path = path.strip('/')
         
     if not resource_handler:
         resource_handler = self._meta.resource_handler
         
     resource.validate(resource_handler)
     handler = resource_handler(self, name, path)
     if hasattr(self, name):
         raise exc.dRestResourceError(
             "The object '%s' already exist on '%s'" % (name, self))
                 
     
     # break up if nested
     parts = name.split('.')
     if len(parts) == 1:
         setattr(self, name, handler)
     elif len(parts) > 1:
         first = parts.pop(0)
         last = parts.pop()
         
         # add the first object to self
         setattr(self, first, resource.NestedResource())
         first_obj = getattr(self, first)
         current_obj = first_obj
         
         # everything in between
         for part in parts:
             setattr(current_obj, part, resource.NestedResource())
             current_obj = getattr(current_obj, part)
         
         # add the actual resource to the chain of nested objects
         setattr(current_obj, last, handler)        
         
     self._resources.append(name)