Quick Start Guide¶
Usage¶
Sunspear provides a very simple api managing activity stream items
Initialize¶
You initialize sunspear by providing sunspear.clients.SunspearClient with an instance of a backend. All backends
are located in sunspear.backends and extend the sunspear.backend.base.BaseBackend:
from sunspear.backends.RiakBackend import RiakBackend
from sunspear.clients import SunspearClient
import datetime
client = SunspearClient(RiakBackend(**{
"host_list": [{'port': 8087}],
"defaults": {'host': '127.0.0.1'},
}))
Create Objects¶
Once you have a reference to the SunspearClient, you can, create objects:
obj = client.create_object({
"objectType": "user"
"displayName": "John Doe",
"email": "jdoe@gmail.com",
"id": "user:1234",
})
Note
If you do not specify the id of an object, one will be automatically generated for you. This also applies for the published date.
Create Activity¶
You can also create an activity. As per the JSON Activity Stream 1.0 specifications, an activity must have a verb, actor and an object.
Creating an activity for:
John Doe created the team "Marketing"
may look something like this:
activity = client.create_activity({
"verb": "create",
"actor": "user:1234",
"object": {
"objectType": "team",
"displayName": "Marketing",
},
})
Couple of intresting things to note here:
- We used the
idfor theactorinstead of the full blownobjectbecause we created theobjectearlier. - We didn’t specify an
idfor ourteamobject. One will be automatically generated. - The verb is arbratry. It can be anything except for response and activity summary verbs. For list a list of common
verbsandobjectsyou may want to use, see the Activity Base Schema. create_activityreturns the fully parsed activity.
Note
The reason you can’t use response and activity summary verbs is because Sunspear uses some of them internally.
Create Responses¶
You can create responses to activities such as liking an activity or replying to an activity. Sunspear supports a few of the response types described here.
All methods that create responses, return the newly created response activity and the original activity the response was created for with the response activity embedded.
Note
Sunspear does responses a little bit differently than what is describe in the specifications.
Responses themselves are fully fledged activities and not objects. This was untimatly done to provide maximum flexibility.
Create a Reply¶
You can create replies to activities.
reply_activity, original_activity = client.create_reply(activity['id'], "user:1234",
"This is my Reply!")
Create a Like¶
You can like activities.
like_activity, original_activity = client.create_like(activity['id'], "user:1234")
Delete Reply¶
original_activity = client.delete_reply(reply_activity["id"])
Delete Like¶
original_activity = client.delete_like(reply_activity["id"])
Get Activities¶
You can get activities by providing a list of ids.
client.get_activities([activity["id"], "1234", "3456"])
Note
If the activity with the id does not exist, it is simply ignored.
Get Objects¶
You can get objects by providing a list of ids.
client.get_objects([obj["id"], "1234", "3456"])
Note
If the object with the id does not exist, it is simply ignored.