cattle_grid.extensions.examples.simple_storage
This extension is an example of storing activities and then serving them through a HTTP API.
I will possibly extend it to also store objects
(and provide some Create
activity creation), but
not much more.
A real storage mechanism should have several features this simple API has not, e.g.
- Serving a HTML page through content negotiation
- Allowing one to update the content of the database
- Collecting and adding metadata, e.g. a replies collection for objects
Usage:
[[extensions]]
module = "cattle_grid.extensions.examples.simple_storage"
api_prefix = "/simple_storage"
config = { prefix = "/simple_storage/" }
extension
module-attribute
extension = Extension(
name="simple storage",
module=__name__,
lifespan=lifespan,
config_class=SimpleStorageConfiguration,
)
The simple storage extension
get_activity_or_object
async
get_activity_or_object(
uuid: UUID,
headers: ActivityPubHeaders,
session: SqlSession,
)
Returns the activity or object
Source code in cattle_grid/extensions/examples/simple_storage/__init__.py
lifespan
async
lifespan(engine: SqlAsyncEngine)
The lifespan ensure that the necessary database table is created.
Source code in cattle_grid/extensions/examples/simple_storage/__init__.py
main
async
Basic endpoint that just returns a string, so requesting with an uuid doesn’t return an error.
simple_storage_publish_activity
async
simple_storage_publish_activity(
message: PublishActivity,
config: Config,
session: CommittingSession,
broker: RabbitBroker = Context(),
)
Method to subscribe to the publish_activity
routing_key.
An activity send to this endpoint will be stored in the
database, and then published through the send_message
mechanism.
The stored activity can then be retrieved through the HTTP API.
Source code in cattle_grid/extensions/examples/simple_storage/__init__.py
simple_storage_publish_object
async
simple_storage_publish_object(
message: PublishObject,
config: Config,
session: CommittingSession,
actor: MessageActor,
broker: RabbitBroker = Context(),
)
Publishes an object, subscribed to the routing key
publish_object
.
We note that this routine creates a Create
activity for the object.
Source code in cattle_grid/extensions/examples/simple_storage/__init__.py
config
SimpleStorageConfiguration
Bases: BaseModel
Configuration of the simple storage extension
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
str
|
|
'/simple_storage/'
|
Source code in cattle_grid/extensions/examples/simple_storage/config.py
prefix
class-attribute
instance-attribute
prefix: str = '/simple_storage/'
Path to use before the generated uuid. The protocol and domain will be extracted from the actor id. See cattle_grid.extensions.examples.simple_storage.config.determine_url_start.
determine_url_start
Used to determine the url of a stored object
>>> determine_url_start("http://abel.example/actor/alice",
... "/simple/storage/")
'http://abel.example/simple/storage/'
Source code in cattle_grid/extensions/examples/simple_storage/config.py
message_types
PublishActivity
Bases: BaseModel
Used when publishing an activity
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actor
|
str
|
The actor performing the activity |
required |
data
|
dict
|
Activity to publish |
required |
Source code in cattle_grid/extensions/examples/simple_storage/message_types.py
PublishObject
Bases: BaseModel
Used when publishing an object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actor
|
str
|
The actor performing the activity |
required |
data
|
dict
|
Object to publish |
required |
Source code in cattle_grid/extensions/examples/simple_storage/message_types.py
models
Base
StoredActivity
Bases: Base
Stored activity in the database
Source code in cattle_grid/extensions/examples/simple_storage/models.py
__tablename__
class-attribute
instance-attribute
name of the table
actor
class-attribute
instance-attribute
actor: Mapped[str] = mapped_column()
The actor that created the activity
create_date
class-attribute
instance-attribute
create_date: Mapped[datetime] = mapped_column(
server_default=now()
)
The create timestamp
data
class-attribute
instance-attribute
data: Mapped[dict] = mapped_column(JSON)
The activity as JSON
id
class-attribute
instance-attribute
id: Mapped[bytes] = mapped_column(
UUIDType(binary=True), primary_key=True
)
The id (uuid as bytes)
StoredObject
Bases: Base
Stored object in the database
Source code in cattle_grid/extensions/examples/simple_storage/models.py
__tablename__
class-attribute
instance-attribute
name of the table
actor
class-attribute
instance-attribute
actor: Mapped[str] = mapped_column()
The actor that created the object
create_date
class-attribute
instance-attribute
create_date: Mapped[datetime] = mapped_column(
server_default=now()
)
The create timestamp
id
class-attribute
instance-attribute
id: Mapped[bytes] = mapped_column(
UUIDType(binary=True), primary_key=True
)
The id (uuid as bytes)