This is a REST API capable of basic CRUD operations, built with Flask 2.3 and MongoDB 6.0.
It dynamically handles request parameters, in order to perform create, read, update, and delete (CRUD) operations on a "person" resource.
-
You must have Python and pip package manager installed on your operating system.
-
Follow the steps in the official Python venv docs to create a virtual environment (OPTIONAL).
-
Create a new folder for the project.
-
Clone this repository into the folder using the
git clone
command. -
Install the dependencies by running the following command in the terminal:
pip -r requirements.txt
- Configure the Flask app to serve the API on a different host and port by including the HOST_NAME and PORT variables in the run command. By default, the app is served on http://localhost:5000.
- You can configure the Flask app to use a different database by specifying the MONGODB_URI connection string while starting the server. The database defaults to http://localhost:27017/hngx without any authentication if no connection string is specified.
- Run the following command to start the server:
- Linux/MacOS:
[HOST_NAME] [PORT] [MONGODB_URI] python3 -m api.v2.app
- Windows:
[HOST_NAME] [PORT] [MONGODB_URI] python -m api.v2.app
- Linux/MacOS:
- Base URL: http://localhost:5000
- Detailed documentation of the API can be found here: API Documentation
Request:
POST /api
POST /api
Content-Type: application/json
{
"name": "chika"
}
Response:
Status: 200 OK
Content-Type: application/json
{
"id": "65035f6372f25cd7f0e3e7bf",
"name": "chika"
}
GET /api
Request:
GET /api
Response:
Status: 200 OK
Content-Type: application/json
[
{
"id": "65035f6372f25cd7f0e3e7bf",
"name": "chika"
}
]
GET /api/{id}
Request:
GET /api/65035f6372f25cd7f0e3e7bf
GET /api/chika
Response:
Status: 200 OK
Content-Type: application/json
{
"id": "65035f6372f25cd7f0e3e7bf",
"name": "chika"
}
PUT /api/{id}
Request:
PUT /api/65035f6372f25cd7f0e3e7bf
PUT /api/chika
Content-Type: application/json
{
"name": "bolu",
}
Response:
Status: 200 OK
Content-Type: application/json
{
"id": "65035f6372f25cd7f0e3e7bf",
"name": "bolu"
}
DELETE /api/{id}
Request:
DELETE /api/65035f6372f25cd7f0e3e7bf
DELETE /api/bolu
Response:
Status: 204 No Content
Note: Replace URL path paramter {id}
with the valid ID or name of the user you want to fetch, update, or delete.
The API returns the following in case of an error:
Status: Error Code
Content-Type: application/json
{
"error": "Error Message"
}
Example:
Status: 404
Content-Type: application/json
{
"error": "Not found"
}
The API was extensively tested using Postman. The full scripts are available here.
The collection of the test requests and with included scripts can be found here:
The Flask framework is open-sourced software licensed under the BSD3 license.