Working with Activity API in Open Event API Server
Recently, I added the Activities API with documentation and dredd tests for the same in Open Event API Server. The Activity Model in the Open Event Server is basically a log of all the things that happen while the server is running, like - event updates, speaker additions, invoice generations and other similar things. This blogpost explains how to implement Activity API in the Open Event API Server’s nextgen branch. In the Open Event Server, we first add the endpoints, then document these so that the consumers ( Open Event Orga App, Open Event Frontend) find it easy to work with all the endpoints. We also test the documentation against backend implementation to ensure that a end-developer who is working with the APIs is not misled to believe what each endpoint actually does in the server. We also test the documentation against backend implementation to ensure that a end-developer who is working with the APIs is not misled to believe what each endpoint actually does in the server. The Activities API endpoints are based on the Activity database model. The Activity table has four columns - id, actor, time, action, the names are self-explanatory. Now for the API schema, we need to make fields corresponding these columns. Since id is auto generated, we do not need to add it as a field for API. Also the activity model’s __init__ method stamps time with the current system time. So this field is also not required in the API fields. We are left with two fields- actor and action. Defining API Schema Next, we define the API Schema class for Activities model. This will involve a Meta class and fields for the class.The Meta class contains the metadata of the class. This includes details about type_, self_view, self_view_kwargs and an inflect parameter to dasherize the input fields from request body. We define the four fields - id, actor, time and action according to marshmallow fields based on the data type and parameters from the activities model. Since id, actor and action are string columns and time is a DateTime column, the fields are written as following: The id field is marked as dump only because it is a read-only type field. The other fields are marked with allow_none as they are all non-required field. ActivityList Class: The activity list class will provide us with the endpoint: “/v1/activities” This endpoint will list all the activities. Since we wanted only GET requests to be working for this, so defined method = [‘GET’, ] for ResourceList. The activities are to be internally created based on different actions like creating an event, updating an event, adding speakers to sessions and likewise. Since the activities are to be shown only to the server admin, is_admin permission is used from the permission manager. ActivityDetail Class: The activity detail gives methods to work with an activity based on the activity id. The endpoint provided is : ‘/v1/activity/<int:activity_id>’ Since this is also an admin-only accessible GET only endpoint the following was written: Writing Documentation: The documentation is written…
