Skip to content

psychemist/simple_crud_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REST API with Basic CRUD Operations

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.

Table of Contents

Installation

  • 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

Configuration

  • 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.

Usage

  • 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
  • Base URL: http://localhost:5000
  • Detailed documentation of the API can be found here: API Documentation

Create a new person

Request:

POST /api

POST /api
Content-Type: application/json

{
  "name": "chika"
}

Response:

Status: 200 OK
Content-Type: application/json

{
  "id": "65035f6372f25cd7f0e3e7bf",
  "name": "chika"
}

Read details of all persons

GET /api

Request:

GET /api

Response:

Status: 200 OK
Content-Type: application/json

[
  {
    "id": "65035f6372f25cd7f0e3e7bf",
    "name": "chika"
  }
]

Read details of a person

GET /api/{id}

Request:

GET /api/65035f6372f25cd7f0e3e7bf
GET /api/chika

Response:

Status: 200 OK
Content-Type: application/json

{
  "id": "65035f6372f25cd7f0e3e7bf",
  "name": "chika"
}

Update details of a person

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 a person

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.

Error Handling

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"
}

Testing

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:

Run in Postman

Test Screenshots

Tests_1

Tests_2

UML Diagram

UML Diagram

License

The Flask framework is open-sourced software licensed under the BSD3 license.

License

Releases

No releases published

Packages

No packages published

Languages