Package scapi :: Class RESTBase
[hide private]
[frames] | no frames]

Class RESTBase

source code

object --+
         |
        RESTBase
Known Subclasses:

The baseclass for all our domain-objects/resources.

Instance Methods [hide private]
 
__init__(self, data, scope, path_stack=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
__getattr__(self, name) source code
 
__setattr__(self, name, value)
This method is used to set a property, a resource or a list of resources as property of the resource the method is invoked on.
source code
 
_as_arguments(self)
Converts a resource to a argument-string the way Rails expects it.
source code
 
_convert_value(self, value) source code
 
_scope(self)
Return the scope this resource lives in, which is the KIND and id
source code
 
__repr__(self)
repr(x)
source code
 
__hash__(self)
hash(x)
source code
 
__eq__(self, other)
Test for equality.
source code
 
__ne__(self, other) source code

Inherited from object: __delattr__, __getattribute__, __new__, __reduce__, __reduce_ex__, __str__

Class Methods [hide private]
 
create(cls, scope, **data)
This is a convenience-method for creating an object that will be passed as parameter - e.g.
source code
 
new(cls, scope, **data)
Create a new resource inside a given Scope.
source code
 
get(cls, scope, id)
Fetch a resource by id.
source code
 
_singleton(cls)
This method will take a resource name like "users" and return the single-case, in the example "user".
source code
Class Variables [hide private]
  REGISTRY = {'comments': <class 'scapi.Comment'>, 'contacts': <...
  ALL_DOMAIN_CLASSES = {'Comment': <class 'scapi.Comment'>, 'Eve...
  ALIASES = []
  KIND = None
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, data, scope, path_stack=None)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)

__setattr__(self, name, value)

source code 

This method is used to set a property, a resource or a list of resources as property of the resource the method is invoked on.

For example, to set a comment on a track, do

>>> sca = scapi.Scope(connector)
>>> track = scapi.Track.new(title='bar', sharing="private")
>>> comment = scapi.Comment.create(body="This is the body of my comment", timestamp=10)    
>>> track.comments = comment

To set a list of users as permissions, do

>>> sca = scapi.Scope(connector)
>>> me = sca.me()
>>> track = scapi.Track.new(title='bar', sharing="private")
>>> users = sca.users()
>>> users_to_set = [user  for user in users[:10] if user != me]
>>> track.permissions = users_to_set

And finally, to simply change the title of a track, do

>>> sca = scapi.Scope(connector)
>>> track = sca.Track.get(track_id)
>>> track.title = "new_title"
Parameters:
  • name (str) - the property name
  • value (RESTBase | list<RESTBase> | basestring | long | int | float) - the property, resource or resources to set
Returns:
None
Overrides: object.__setattr__

create(cls, scope, **data)
Class Method

source code 

This is a convenience-method for creating an object that will be passed as parameter - e.g. a comment. A usage would look like this:

>>> sca = scapi.Scope(connector)
>>> track = sca.Track.new(title='bar', sharing="private")
>>> comment = sca.Comment.create(body="This is the body of my comment", timestamp=10)    
>>> track.comments = comment

new(cls, scope, **data)
Class Method

source code 

Create a new resource inside a given Scope. The actual values are in data.

So for creating new resources, you have two options:

  • create an instance directly using the class:
    >>> scope = scapi.Scope(connector)
    >>> scope.User.new(...)
    <scapi.User object at 0x1234>
  • create a instance in a certain scope:
    >>> scope = scapi.Scope(connector)
    >>> user = scapi.User("1")
    >>> track = user.tracks.new()
    <scapi.Track object at 0x1234>
Parameters:
  • scope (tuple<Scope>[1]) - if not empty, a one-element tuple containing the Scope
  • data (dict) - the data
Returns:
new instance of the resource

get(cls, scope, id)
Class Method

source code 

Fetch a resource by id.

Simply pass a known id as argument. For example

>>> sca = scapi.Scope(connector)
>>> track = sca.Track.get(id)

_scope(self)

source code 

Return the scope this resource lives in, which is the KIND and id

Returns:
"<KIND>/<id>"

_singleton(cls)
Class Method

source code 

This method will take a resource name like "users" and return the single-case, in the example "user".

Currently, it's not very sophisticated, only strips a trailing s.

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

__hash__(self)
(Hashing function)

source code 

hash(x)

Overrides: object.__hash__
(inherited documentation)

__eq__(self, other)
(Equality operator)

source code 

Test for equality.

Resources are considered equal if the have the same kind and id.


Class Variable Details [hide private]

REGISTRY

Value:
{'comments': <class 'scapi.Comment'>,
 'contacts': <class 'scapi.User'>,
 'events': <class 'scapi.Event'>,
 'favorites': <class 'scapi.Track'>,
 'groups': <class 'scapi.Group'>,
 'me': <class 'scapi.User'>,
 'permissions': <class 'scapi.User'>,
 'playlists': <class 'scapi.Playlist'>,
...

ALL_DOMAIN_CLASSES

Value:
{'Comment': <class 'scapi.Comment'>,
 'Event': <class 'scapi.Event'>,
 'Group': <class 'scapi.Group'>,
 'Playlist': <class 'scapi.Playlist'>,
 'Track': <class 'scapi.Track'>,
 'User': <class 'scapi.User'>}