Activity #49: Python Flask API with SQLALCHEMY integration

Step 1: Fork the repository

  1. Go to the GitHub repository thirdygayares/restaurant_menu.

  2. Click on the "Fork" button on the top right of the repository page.

  3. This creates a copy of the repository in your GitHub account.


Step 2: Clone your repository

  1. Navigate to your GitHub profile and open the newly forked repository.

  2. Click on the green "Code" button and copy the HTTPS link.

  3. Open your terminal and run the following command to clone the repository:

     git clone https://github.com/your-username/flask-api-with-mysql.git
    

    (Replace your-username with your actual GitHub username.)

  4. Change into the directory:

     cd flask-api-with-mysql
    

Step 3: Set up the Virtual Environment

A. Create a Virtual Environment

  1. Create a virtual environment in your project directory by running:

     python -m venv venv
    
  2. Activate the virtual environment:

     venv\Scripts\activate
    

B. Install Dependencies

  1. Install the required dependencies by running:

     pip install -r requirements.txt
    

    This will install all the packages listed in the requirements.txt file.

C. Set up the .env File

  1. Create a .env file in the root directory of the project.

  2. Add the following content to the .env file (replace {password}, {host}, {port}, and {databasename1} with your actual database credentials):

     FLASK_ENV=development
     DATABASE_URL=mysql://avnadmin:{password}@{host}:{port}/{databasename1}
     SECRET_KEY=your_secret_key
    
  3. The SECRET_KEY can be any random string that Flask will use to sign cookies and sessions.

D. Add Certificates (Optional)

  1. If required by the application (e.g., for secure connections), copy the SSL certificates to the app/certs folder. Ensure the necessary certificates (cert.pem, key.pem, etc.) are in place.

E. Run Database Migrations

  1. Run the following command to upgrade the database:

     flask db upgrade
    

    This will apply any pending migrations to the database.

F. Run the Application

  1. Finally, run the application by executing:

     python run.py
    

    This should start the Flask application on http://127.0.0.1:5000.


Step 4: Create a Database on Aiven Dashboard

  1. Go to the Aiven dashboard.

  2. Create a new PostgreSQL or MySQL database.

  3. Make sure to replace the database connection details (like password, host, port, and databasename1) in your .env file with the values provided by Aiven.

Step 5: Explain the Code and Show Postman Requests

Code Explanation

  • Flask Application: The project is a Flask-based application that manages restaurant menu items and users. It uses a MySQL database for storing user and menu information.

  • Endpoints:

    • POST /users: Creates a new user in the database.

    • GET /users/<id>: Retrieves a user by their ID.

Here is an overview of the most important files:

  • run.py: This is the entry point of the Flask application. It initializes the app and starts the server.

  • app/models.py: Contains the models for the database, such as the User model for user-related data.

  • app/routes.py: Contains the Flask routes that define the application’s API endpoints (e.g., creating and fetching users).

Example API Requests

  • Create User (POST)

    To create a new user, use the POST method at the following endpoint:

      http://127.0.0.1:5000/users
    
    • Body: JSON format with user details:

        {
          "username": "john_doe",
          "email": "john@example.com",
          "password": "securepassword"
        }
      

  • Get User by ID (GET)

    To fetch a user by their ID, use the GET method:

      http://127.0.0.1:5000/users/3
    

    Replace “3"with the ID of the user you want to retrieve.

Connect to your database cloud to check if the user data is now added

Postman Screenshot

Once you have Postman set up, you can test the API by performing the following steps:

  1. Create User:

    • Set the request type to POST.

    • Use the URL http://127.0.0.1:5000/users.

    • Add the JSON body as shown above.

    • Click "Send".

  2. Get User by ID:

    • Set the request type to GET.

    • Use the URL http://127.0.0.1:5000/users/3 (replace 3 with the ID of the user).

    • Click "Send".

After sending the requests, you should see the appropriate responses in Postman (either a confirmation for creating a user or the user details when retrieving by ID).

RomelUsigan/flask-api-with-mysql