SteaneMurphy / API-Webservice

Flask API Webserver using PostgreSQL Backend
0 stars 0 forks source link

JAMES MURPHY API PROJECT

${\textsf{\color{goldenrod}PROJECT REPOSITORY - JAMESMURPHY T2A2}}$

Link To GitHub Repository

Clone (HTTPS): https://github.com/SteaneMurphy/JamesMurphy_T1A2

image

${\textsf{\color{goldenrod}PROJECT DESCRIPTION}}$

This project is a working API Flask web-service using a PostgreSQL backend. It has been designed using MVC and RESTful API architecture with a 3NF (3rd Normal Form) database schema.

${\textsf{\color{goldenrod}WHAT IS THIS API?}}$

This API provides data to any front-end application or service in the form of JSON objects. The service is a mock-up SaaS system whereby users can register and login to the system, purchase products through subscriptions and the system invoices the customer for said purchase. Each product comes with attatched licensing.

This API helps an online business manage customers and the products and services they purchase as well as allowing the business to update or change a product/service at will. The product/service can be offered through a subscription by having the system automate billing and payments.

This solution reduces the amount of contact hours an employee of the business needs to spend organising customer data, preparing invoices and checking subscription and payment statuses by providing the data to automate the procedure.

Alongside productivity boosts, a SaaS product has a high chance of increasing business income due to its customer ease-of-use, ability to adapt to the market and offer the ability to subscribe to a service rather than paying an expensive upfront fee. This structure is seen in popular software like the Adobe suite, which in times past, used to cost thoudands of dollars per seat.

Other benefits include software that runs on the web or from the cloud, removing the need for high IT costs and managing licenses, freeing up staff to hot-desk or work remotely.

In 2024 (Vena Solutions, 2024, 74 SaaS Statistics, Trends and Benchmarks for 2024, accessed, 20 June 2024, https://www.venasolutions.com/blog/saas-statistics):

${\textsf{\color{goldenrod}PROJECT TRACKING}}$

All completed and outstanding tasks for this project can be found on its associated Github Projects board.

The project is split into 7 parent tasks, comprised of smaller individual tasks:

It is possible that further tasks will be added as issues are encountered throughtout the project.

image

These tasks are tracked across four stages of completion:

image

image

image

image

${\textsf{\color{goldenrod}PACKAGES AND DEPENDENCIES}}$

This application uses the following dependencies:

These are only major dependencies, it is recommended that a user installs the full list of dependencies from the "requirements.txt" file included in this repository.

The following are installation instructions to get his application working in your environment.

${\textsf{\color{goldenrod}SETTING UP THE SERVICE}}$

CLONE THE REPOSITORY

To clone the repository, please make a directory for the project. This can be done by making a new folder in Windows or by typing the following into a terminal:

mkdir <directory_path>

INSTALL VIRTUAL ENVIRONMENT

The Python Virtual Environment package will allow a user to modify their project environment without affecting the overall system. To install this package, please enter the following command into the terminal:

pip install venv

RUN VIRTUAL ENVIRONMENT

Once the virtual environment package has been installed, we need to create a new virtual environment. To create a new virtual environment, please use the following command:

python3 -m venv .venv

This new environment must be activated:

source .venv/bin/activate

INSTALL DEPENDENCIES

Ensuring that you are within the newly created virtual environment, you can safely install the dependencies listed in the "requirements.txt". To iterate over the list and install each package, please use the following terminal command:

pip install -r requirements.txt

INSOMNIA HTTPS REQUESTS

Included in the documents folder is a list of endpoints that can be imported into the Insomnia API software. This can help if you don't want to manually create HTTPS body requests and URIs.

RUNNING THE DATABASE

You will need to create a new postgreSQL database and fill out the database details in the .env.sample including databse username and password. The .env.sample will need its file extenstion changed to .env by removing the .sample extenstion

FLASK COMMAND LINE COMMANDS

To seed the database or run an instance of the the Flask service. Please use the following commands in terminal, when in the project folder.

Flask cli create - recreates and seeds the database

Flask run - runs the Flask local service

The local service runs on HTTPS://127.0.0.1/5000

ADMIN ACCOUNT

You cannot create an admin account from accoutn creation and will need to use the existing admin account when the database is seeded. This admin account can make other accounts, admin by using the appropriate endpoint.

HTTPS://127.0.0.1/5000/users/login/

{
    "email": "admin_email_1",
    "password": "testpassword123"
}

${\textsf{\color{goldenrod}RELATIONAL DATABASE SYSTEMS}}$

PostgreSQL was the database used for this API. Whilst it is a powerful and open-source system, it also has its drawbacks including in its use as a relational database system.

BENEFITS

DRAWBACKS

${\textsf{\color{goldenrod}ORM (OBJECT RELATIONAL MAPPER)}}$

SQLAlchemy is the ORM (Object-Relational Mapping) system used in this API. The ORM allowed access to read and manipulate the database using python objects rather than SQL language.

SQLAlchemy allows the creation of database schemas or tables directly within the API using python classes. This was extended with the uses of the 'Declarative' and 'Mapped' extenstions to SQLAlchemy. Columns and data types can be created directly inside a python class in a similar fashion to how the tables would look within the database. As follows is a code example utilising specific data types, primary and foreign keys, attribute creation and mapping.

image

SQlAlchemy also helps support relationships between database tables through the use of the 'relationship' and 'ForeignKey' function and types. These relationships can be back-referenced to parent tables and help facilitate cascade deletes. The ORM supports one-to-one, many-to-one and many-to-many relationship types.

image

SQLAlchemy also allows for database querying and retrieval of data as well as database session management. The database queries are abstracted from the base SQL query and easier to use. For all intents and purposes, the ORM keeps the SQL syntax similar with terms like 'select', 'join' and 'where'.

The database session can be managed with terms like 'session.commit' and 'session.flush'.

image

${\textsf{\color{goldenrod}ERD (ENTITY RELATIONSHIP DIAGRAM)}}$

The following is an ERD diagram for the proposed database system used by this API. The ERD has been normalised to 3rd normal form (3NF). All tables have had duplication removed and have a unique id to their respective entity. Each table has its proposed data types, constraints and relationships notated. A unique id can appear on another table as a foreign key (FK) associating the two tables together.

I arrived at the the 3NF by normalising the data through the 1st normal form (1NF) and 2nd normal form (2NF) respectively.

1NF

Each attribute in the table (row and column) should be unique without duplication. Each attribute was reduced to a singular format, for example, each row in the 'product' table contains a singular 'product' not multiple 'products'. Each table was assigned a primary key, in the case of this ERD, they are all ID values.

2NF

For 2nd normal form, each attribute that was not a key was made 'independent', that is it is only dependent on the primary key in the table it appears, it does not rely on another table's primary key when being referenced. An example of this is the date values in the 'user' table. They can only be accessed by using the primary key in the 'users' table, they are independent of the other tables.

3NF

All duplication has been removed. Tables that rely on each other in a many-to-many relationship are abstracted by having their primary keys appear as combination-keys in an association table, for example, the subscription_id, product_id and license attributes in the 'subscription_details' table.

image

image

${\textsf{\color{goldenrod}ENTITY MODELS AND RELATIONSHIPS}}$

Before and during development of the API, multiple relationships were identified and developed between the entity models:

image

image

image

image

image

image

image

image

image

${\textsf{\color{goldenrod}API ENDPOINTS}}$

${\textsf{\color{magenta}USERS}}$

${\textsf{\color{lightblue}REGISTER NEW USER ACCOUNT}}$

${\textsf{\color{lightblue}LOGIN TO EXISTING ACCOUNT}}$

${\textsf{\color{lightblue}GET ACCOUNT BY USER ID}}$

${\textsf{\color{lightblue}RETURN LIST OF ALL ACCOUNTS}}$

${\textsf{\color{lightblue}SET ADMIN ROLE FOR AN ACCOUNT}}$

${\textsf{\color{lightblue}UPDATE ACCOUNT DETAILS}}$

${\textsf{\color{lightblue}UPDATE ACCOUNT PASSWORD}}$

${\textsf{\color{lightblue}DELETE A USER ACCOUNT}}$

${\textsf{\color{magenta}SUBSCRIPTIONS}}$

${\textsf{\color{lightblue}CREATE NEW SUBSCRIPTION}}$

${\textsf{\color{lightblue}RETURN EXISTING SUBSCRIPTION BY ID}}$

${\textsf{\color{lightblue}RETURN ALL USER SUBSCRIPTIONS}}$

${\textsf{\color{lightblue}RETURN ALL SUBSCRIPTIONS (DATABASE)}}$

${\textsf{\color{lightblue}UPDATE AN EXISTING SUBSCRIPTION}}$

${\textsf{\color{lightblue}DELETE A SUBSCRIPTION}}$

${\textsf{\color{magenta}PAYMENTS}}$

${\textsf{\color{lightblue}CREATE A NEW PAYMENT}}$

${\textsf{\color{lightblue}RETURN A PAYMENT BY ID}}$

${\textsf{\color{lightblue}RETURN PAYMENTS BY USER}}$

${\textsf{\color{lightblue}RETURN LIST OF ALL PAYMENTS (DATABASE)}}$

${\textsf{\color{lightblue}UPDATE A PAYMENT}}$

${\textsf{\color{lightblue}DELETE A PAYMENT}}$

${\textsf{\color{magenta}TICKETS}}$

${\textsf{\color{lightblue}CREATE NEW SUPPORT TICKET}}$

${\textsf{\color{lightblue}RETURN TICKET BY ID}}$

${\textsf{\color{lightblue}RETURN TICKETS BY USER}}$

${\textsf{\color{lightblue}RETURN LIST OF ALL TICKETS (DATABASE)}}$

${\textsf{\color{lightblue}UPDATE EXISTING SUPPORT TICKET}}$

${\textsf{\color{lightblue}DELETE A TICKET}}$

${\textsf{\color{magenta}PLANS}}$

${\textsf{\color{lightblue}CREATE A NEW PLAN}}$

${\textsf{\color{lightblue}RETURN PLAN BY ID}}$

${\textsf{\color{lightblue}RETURN PLAN BY USER}}$

${\textsf{\color{lightblue}RETURN LIST OF ALL PLANS (DATABASE)}}$

${\textsf{\color{lightblue}UPDATE AN EXISTING PLAN}}$

${\textsf{\color{magenta}PRODUCTS}}$

${\textsf{\color{lightblue}CREATE A NEW PRODUCT}}$

${\textsf{\color{lightblue}RETURN PRODUCT BY ID}}$

${\textsf{\color{lightblue}RETURN ALL PRODUCTS (DATABASE)}}$

${\textsf{\color{lightblue}UPDATE AN EXISTING PRODUCT}}$

${\textsf{\color{magenta}SUBSCRIPTION DETAILS}}$

${\textsf{\color{lightblue}RETURN SUBSCRIPTION DETAILS BY ID}}$

${\textsf{\color{lightblue}RETURN SUBSCRIPTION DETAILS BY SUBSCRIPTION}}$

${\textsf{\color{lightblue}RETURN ALL SUBSCRIPTION DETAILS (DATABASE)}}$