Giter Club home page Giter Club logo

sonarqubetest's Introduction

Flask Product API

This is a simple Flask API for managing product data. The API allows you to perform basic CRUD (Create, Read, Update, Delete) operations on products.

Setup

To use this API, you need to have the following software installed on your system:

  • Python 3.10 or later
  • Flask
  • Flask-RESTful
  • Flask-Cors
  • Peewee

You can install the required packages by running the following command:

pip install -r requirements.txt

Usage

db.py

How to use

  1. Open a terminal and navigate to the directory where db.py is located.
  2. Run the script for create/update products.db using the following command:
python db.py
  • Use the following arguments to customize the script behavior:
-d, --delete_all_data: Deletes all existing product data.
-a, --add_default_data: Adds default product data to the database.
-p, --print_all_data: Prints all product data to the console.

app.py

To run the API, execute the following command:

python app.py --host [HOST] --port [PORT]

Replace [HOST] with the IP address or hostname of the server and [PORT] with the port number you want to run the application on.

By default, the application will run on localhost and port 5000.

The API will then be accessible at http://localhost:5000.

Endpoints

GET /api/products

Retrieves a list of all products in the database.

Example response:

[
  {
    "id": 1,
    "name": "Sugar",
    "price": 32.0
  },
  {
    "id": 2,
    "name": "Sult",
    "price": 19.0
  },
  {
    "id": 3,
    "name": "Bread",
    "price": 20.0
  },
  {
    "id": 4,
    "name": "Butter",
    "price": 62.0
  },
  {
    "id": 5,
    "name": "Milk",
    "price": 32.0
  }
]

POST /api/products

Adds a new product to the database.

Example request body:

{
  "name": "Eggs",
  "price": 12.0
}

Example response:

{
  "message": "Product added successfully.",
  "productId": 6
}

GET /api/products/{product_id}

Retrieves a single product by ID.

Example response:

{
  "id": 1,
  "name": "Sugar",
  "price": 32.0
}

PATCH /api/products/{product_id}

Updates a single product by ID.

Example request body:

{
  "name": "Sugar (1kg)",
  "price": 35.0
}

Example response:

{
  "message": "Product updated successfully."
}

DELETE /api/products/{product_id}

Deletes a single product by ID.

Example response:

{
  "message": "Product deleted."
}

sonarqubetest's People

Contributors

switcherblades avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.