ModelViewSet APIs
CRUD APIs in the Spear backend are often handled using Django's built in ModelViewSet. APIs built this way share methods and behavior, and are documented once here.
For example, /client/ is an API built using ModelViewSet. With this API you may:
GET/client/to retrieve a (possibly paginated) list of itemsPOST/client/to create a new clientGET/client/1to get the client with id=1PUT/client/1to completely overwrite the data for the client with id=1PATCH/client/1to partially update provided fields for the client with id=1DELETE/client/1to delete the client with id=1
Examples are provided below for the /client endpoint, but these APIs will apply for all APIs built using ModelViewSet.
Such APIs will be documented by providing examples of JSON objects for the model and all its fields, which can then be manipulated using the APIs below.
List Items
GET /client/: Lists all clients.
Depending on the model, you may have the option to specify a query/filter along with other options.
Example Response
[
{
"id": 1,
"name": "Test Client",
"is_active": true
},
{
"id": 2,
"name": "Test 2",
"is_active": true
}
]
Create Item
POST /client/: Creates a new client.
Example Request
Request Headers
Content-Type: application/json
Request Body
{
"name": "Sarina",
"is_active": true
}
Response
{
"id": 3,
"name": "Sarina",
"is_active": true
}
Get Item
GET /client/1: Retrieves the client with id=1
Example Response
{
"id": 1,
"name": "Test Client",
"is_active": true
}
Partially Update Item
PATCH /client/1: Updates the provided fields for client id=1.
Example Request
Request Headers
Content-Type: application/json
Request Body
{
"id": 3,
"is_active": false
}
Response
{
"id": 3,
"name": "Sarina",
"is_active": false
}
Update Item
PUT /client/1: Replaces data for client id=1 with the data provided. Unspecified fields will use their default value if available. You may want to use a partial update instead if you only need to change one or a few fields.
Example Request
Request Headers
Content-Type: application/json
Request Body
{
"id": 3,
"name": "Sarina (Black Sunny)",
"is_active": false
}
Response
{
"id": 3,
"name": "Sarina (Black Sunny)",
"is_active": false
}
Delete Item
DELETE /client/1: Deletes the client with id=1
No request body is needed. Upon success, you will receive a 204 No Content response.