Open Event Server – Pages API
This article illustrates how the Pages API has been designed and implemented on the server side, i.e., FOSSASIA‘s Open Event Server. Pages endpoint is used to create static pages such as “About Page” or any other page that doesn’t need to be updated frequently and only a specific content is to be shown.
Parameters
- name – This stores the name of the page.
-
- Type – String
- Required – Yes
-
- title – This stores the title of the page.
-
- Type – String
- Required – No
-
- url – This stores the url of the page.
-
- Type – String
- Required – Yes
-
- description – This stores the description of the page.
-
- Type – String
- Required – Yes
-
- language – This stores the language of the page.
-
- Type – String
- Required – No
-
- index – This stores the position of the page.
-
- Type – Integer
- Required – No
- Default – 0
-
- place – Location where the page will be placed.
-
- Type – String
- Required – No
- Accepted Values – ‘footer’ and ‘event’
-
These are the allowed parameters for the endpoint.
Model
Lets see how we model this API. The ORM looks like this :
__tablename__ = 'pages' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) title = db.Column(db.String) url = db.Column(db.String, nullable=False) description = db.Column(db.String) place = db.Column(db.String) language = db.Column(db.String) index = db.Column(db.Integer, default=0)
As you can see, we created a table called “pages”. This table has 8 columns, 7 of which are the parameters that I have mentioned above. The column “id” is an Integer column and is the primary key column. This will help to differentiate between the various entries in the table.
The visualisation for this table looks as follows :
API
We support the following operations:
- GET all the pages in the database
- POST create a new page
- GET details of a single page as per id
- PATCH a single page by id
- DELETE a single page by id
To implement this we first add the routes in our python file as follows :
api.route(PageList, 'page_list', '/pages') api.route(PageDetail, 'page_detail', '/pages/<int:id>')
Then we define these classes to handle the requests. The first route looks as follows:
class PageList(ResourceList): """ List and create page """ decorators = (api.has_permission('is_admin', methods="POST"),) schema = PageSchema data_layer = {'session': db.session, 'model': Page}
As can be seen above, this request requires the user to be an admin. It uses the Page model described above and handles a POST request.
The second route is:
class PageDetail(ResourceDetail): """ Page detail by id """ schema = PageSchema decorators = (api.has_permission('is_admin', methods="PATCH,DELETE"),) data_layer = {'session': db.session, 'model': Page}
This route also requires the user to be an admin. It uses the Page model and handles PATCH, DELETE requests.
To summarise our APIs are:
GET
/v1/pages{?sort,filter}
POST
/v1/pages{?sort,filter}
GET
/v1/pages/{page_id}
PATCH
/v1/pages/{page_id}
DELETE
/v1/pages/{page_id}
You must be logged in to post a comment.