Sunspear Client

sunspear.backends.base

class sunspear.backends.base.BaseBackend
activity_create(activity, **kwargs)

Stores a new activity to the backend.

Parameters:activity (dict) – a dict representing the activity
Returns:a dict representing the newly stored activity
activity_delete(activity, **kwargs)

Performs the task of actually deleting the activity from the backend.

Parameters:activity (dict) – a dict representing the activity
activity_exists(activity, **kwargs)

Determins if an activity already exists in the backend.

Parameters:activity (dict) – the activity we want to determin if it exists
Returns:True if the activity exists, otherwise False
activity_get(activity, **kwargs)
activity_update(activity, **kwargs)

Performs the actual task of updating the activity in the backend.

Parameters:activity (dict) – a dict representing the activity
Returns:a dict representing the newly stored activity
clear_all_activities()

Clears all activities from the backend.

clear_all_objects()

Clears all objects from the backend.

create_activity(activity, **kwargs)

Stores a new activity in the backend. If an object with the same id already exists in the backend, a SunspearDuplicateEntryException is raised. If an ID is not provided, one is generated on the fly.

Activities that provide objects as dictionaries have their objects processed and stored using create_obj, and the objects are replaced with their id’s within the activity.

Parameters:activity (dict) – activity we want to store in the backend
Raises:SunspearDuplicateEntryException if the record already exists in the database.
Returns:dict representing the new activity.
create_obj(obj, **kwargs)

Stores a new obj in the backend. If an object with the same id already exists in the backend, a SunspearDuplicateEntryException is raised. If an ID is not provided, one is generated on the fly.

Parameters:obj (dict) – obj we want to store in the backend
Raises:SunspearDuplicateEntryException if the record already exists in the database.
Returns:dict representing the new obj.
create_sub_activity(activity, actor, content, extra={}, sub_activity_verb='', **kwargs)

Creates a new sub-activity as a child of activity.

Parameters:
  • activity (a string or dict) – the activity we want to create the sub-item for
  • actor (a string or dict) – the object creating the sub-activity
  • content (a string or dict) – a string or an object representing the content of the sub-activity
  • extra (dict) – additional data the is to be included as part of the sub-activity activity
  • sub_activity_verb (string) – the verb of the sub activity
Returns:

a tuple containing the new sub activity and the original activity the sub activity was created for.

dehydrate_activities(activities)

Takes a raw list of activities returned from riak and replace keys with contain ids for riak objects with actual riak object

delete_activity(activity, **kwargs)

Deletes an existing activity from the backend.

Parameters:activity (dict) – a dict representing the activity
Raises:SunspearInvalidActivityException if the activity doesn’t have a valid id.
delete_obj(obj, **kwargs)

Deletes an existing obj from the backend.

raises:

  • SunspearInvalidObjectException – if the obj doesn’t have a valid id.
Parameters:obj (dict) – a dict representing the obj
Raises:SunspearInvalidObjectException
delete_sub_activity(sub_activity, sub_activity_verb, **kwargs)

Deletes a sub_activity made on an activity. This will also update the corresponding activity.

Parameters:
  • sub_activity (string) – the id of the reply activity to delete
  • sub_activity_verb (string) – the verb of the sub activity
get_activity(activity_ids=[], **kwargs)

Gets an activity or a list of activities from the backend.

Parameters:activity_ids (list) – a list of ids of activities that will be retrieved from the backend.
Returns:a list of activities. If an activity is not found, a partial list should be returned.
get_new_id()

Generates a new unique ID. The default implementation uses uuid1 to generate a unique ID.

Returns:a new id
get_obj(obj_ids=[], **kwargs)

Gets an obj or a list of activities from the backend.

Parameters:obj (list) – a list of ids of activities that will be retrieved from the backend.
Returns:a list of activities. If an obj is not found, a partial list should be returned.
get_sub_activity_attribute(sub_activity_verb)
get_sub_activity_model(sub_activity_verb)
is_sub_activity_verb_valid(sub_activity_verb)
obj_create(obj, **kwargs)

Stores a new obj to the backend.

Parameters:obj (dict) – a dict representing the obj
Returns:a dict representing the newly stored obj
obj_delete(obj, **kwargs)
obj_exists(obj, **kwargs)

Determins if an object already exists in the backend.

Parameters:obj (dict) – the activity we want to determin if it exists
Returns:True if the object exists, otherwise False
obj_get(obj, **kwargs)
obj_update(obj, **kwargs)
sub_activity_create(activity, actor, content, extra={}, sub_activity_verb='', sub_activity_attribute='', **kwargs)

Creates a new sub-activity as a child of activity.

Parameters:
  • activity (a string or dict) – the activity we want to create the sub-item for
  • actor (a string or dict) – the object creating the sub-activity
  • content (a string or dict) – a string or an object representing the content of the sub-activity
  • extra (dict) – additional data the is to be included as part of the sub-activity activity
  • sub_activity_verb (string) – the verb of the sub activity
  • sub_activity_attribute (string) – the attribute in the activity the sub-activity will be a part of
Returns:

a tuple containing the new sub activity and the original activity the sub activity was created for.

sub_activity_delete(sub_activity, sub_activity_verb, **kwargs)
update_activity(activity, **kwargs)

Updates an existing activity in the backend. If the object does not exist, it is created in the backend.

Parameters:activity (dict) – a dict representing the activity
Raises:SunspearInvalidActivityException if the activity doesn’t have a valid id.
Returns:a dict representing the newly stored activity
update_obj(obj, **kwargs)

Updates an existing obj in the backend. If the object does not exist, it is created in the backend.

raises:

  • SunspearInvalidObjectException – if the obj doesn’t have a valid id.
Parameters:obj (dict) – a dict representing the obj
Raises:SunspearInvalidObjectException
Returns:a dict representing the newly stored obj

sunspear.backends.riak

Copyright 2016 Numan Sachwani <numan856@gmail.com>

This file is provided to you under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

class sunspear.backends.riak.RiakBackend(protocol='pbc', nodes=[], objects_bucket_name='objects', activities_bucket_name='activities', **kwargs)
activity_create(activity, **kwargs)

Creates an activity. You can provide objects for activities as dictionaries or as ids for already existing objects.

If you provide a dictionary for an object, it is saved as a new object.

If you provide an object id and the object does not exist, it is saved anyway, and returned as an empty dictionary when retriving the activity later.

activity_delete(activity, **kwargs)

Deletes an activity item and all associated sub items

activity_exists(activity, **kwargs)
activity_get(activity_ids=[], raw_filter='', filters={}, include_public=False, audience_targeting={}, aggregation_pipeline=[], **kwargs)

Gets a list of activities. You can also group activities by providing a list of attributes to group by.

Parameters:
  • activity_ids (list) – The list of activities you want to retrieve
  • filters (dict) – filters list of activities by key, value pair. For example, {'verb': 'comment'} would only return activities where the verb was comment. Filters do not work for nested dictionaries.
  • raw_filter (string) – allows you to specify a javascript function as a string. The function should return true if the activity should be included in the result set or false it shouldn’t. If you specify a raw filter, the filters specified in filters will not run. How ever, the results will still be filtered based on the audience_targeting parameter.
  • include_public (boolean) – If True, and the audience_targeting dictionary is defined, activities that are not targeted towards anyone are included in the results
  • audience_targeting (dict) – Filters the list of activities targeted towards a particular audience. The key for the dictionary is one of to, cc, bto, or bcc. The values are an array of object ids
  • aggregation_pipeline (array of sunspear.aggregators.base.BaseAggregator) – modify the final list of activities. Exact results depends on the implementation of the aggregation pipeline
Returns:

list – a list of activities matching activity_ids. If the activities is not found, it is not included in the result set. Activities are returned in the order of ids provided.

activity_update(activity, **kwargs)
clear_all(**kwargs)

Deletes all activity stream data from riak

clear_all_activities(**kwargs)

Deletes all activities data from riak

clear_all_objects(**kwargs)

Deletes all objects data from riak

get_new_id()

Generates a new unique ID. The default implementation uses uuid1 to generate a unique ID.

Returns:a new id
obj_create(obj, **kwargs)
obj_delete(obj, **kwargs)
obj_exists(obj, **kwargs)
obj_get(obj, **kwargs)

Given a list of object ids, returns a list of objects

obj_update(obj, **kwargs)
set_activity_indexes(riak_object)

Store indexes specific to an Activity. Stores the following indexes: 1. verb of the Activity 2. actor of the Activity 3. object of the Activity 4. if target is defined, verb for the target of the Activity

Parameters:riak_object (RiakObject) – a RiakObject representing the model of the class
set_general_indexes(riak_object)

Sets the default riak 2Is.

Parameters:riak_object (RiakObject) – a RiakObject representing the model of the class
set_sub_item_indexes(riak_object, **kwargs)

Store indexes specific to a sub-activity. Stores the following indexes: 1. id of the the parent Activity of this sub-activity

Parameters:riak_object (RiakObject) – a RiakObject representing the model of the class
sub_activity_create(activity, actor, content, extra={}, sub_activity_verb='', published=None, **kwargs)
sub_activity_delete(sub_activity, sub_activity_verb, **kwargs)

Deletes a sub_activity made on an activity. This will also update the corresponding parent activity.

Parameters:
  • sub_activity (string) – the id of the reply activity to delete.
  • sub_activity_verb (string) – the verb of the sub activity
Returns:

a dict representing the updated parent activity