https://github.com/eriknguyen/friends-management
A simple friends management API built with Flask and MySQL
https://github.com/eriknguyen/friends-management
api flask mysql python
Last synced: 4 months ago
JSON representation
A simple friends management API built with Flask and MySQL
- Host: GitHub
- URL: https://github.com/eriknguyen/friends-management
- Owner: eriknguyen
- Created: 2018-02-24T16:29:49.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-26T05:46:41.000Z (over 8 years ago)
- Last Synced: 2025-10-14T20:35:20.753Z (10 months ago)
- Topics: api, flask, mysql, python
- Language: Python
- Homepage: http://sp.erikng.me/
- Size: 84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Friends Management API
*A simple friends management API built with Flask and MySQL*
## Up and Running
#### 1. Setup
```bash
pyvenv venv -p python3
# Or: virtualenv venv -p python3
source venv/bin/activate
pip install -r requirements.txt
```
#### 1. Run app with Python
```bash
source venv/bin/activate
python app.py
```
#### 2. Or run with `gunicorn` server
```bash
# use start script
source start.sh
# or manually
source venv/bin/activate
gunicorn --bind 0.0.0.0:8000 wsgi:app
```
#### 3. Test
Run test in virtual environment
```bash
source venv/bin/activate
python test.py
```
## Overview of the designs
### 1. Technical decisions
* [Python Flask](http://flask.pocoo.org/) is used for quick installation and fast deployment with high flexibility and scalability
* [SQLAlchemy](https://www.sqlalchemy.org/) is used and wired up with flask using `flask-sqlalchemy` for easier ORM
* On the server, the app is running on a WSGI `gunicorn` server for better performance, especially with the use of multiple workers for handling concurrent requests
* Request handling process:
* App getting request with view functions that are mapped to url patterns in `routes.py`
* View functions are imported from `middleware.py`, where the main request handling processes take place.
* View functions in `middleware.py` use data service methods in `store.py` for data-related operations.
### 2. Project Structure
* Main application files:
* `app.py` contains the main `app` instance
* `routes.py` contains all url mapping with route functions
* `middleware.py` contains all route functions for handling requests and response
* `store.py` contains data-related methods for read/write/update database directly
* `model/*` is the folder contains data model created with `SQLAlchemy`
* Other files:
* `test.py` contains all test cases built with Flask test client
* `wsgi.py` is used for running the server with `gunicorn`
### 3. Database design

---
## Friends API Specifications
### 1. `POST /api/friends/connect` - create friend connection
* Request data:
```json
{
"friends": [
"andy@example.com",
"john@example.com"
]
}
```
* Response:
- Success: `{ "success": true }`
- Error:
```json
{
"error": "Error message"
}
```
### 2. `POST /api/friends` - get friend list of email
* Request data:
```json
{
"email": "andy@example.com"
}
```
* Response:
- Success:
```json
{
"success": true,
"friends" : [
"john@example.com"
],
"count" : 1
}
```
- Error:
```json
{
"error": "Error message"
}
```
### 3. `POST /api/friends/common` - get common friends of users
* Request data:
```json
{
"friends": [
"andy@example.com",
"john@example.com"
]
}
```
* Response:
- Success:
```json
{
"success": true,
"friends" : [
"common@example.com"
],
"count" : 1
}
```
- Error:
```json
{
"error": "Error message"
}
```
### 4. `POST /api/friends/subscribe` - subscribe to update from an email
* Request data:
```json
{
"requestor": "lisa@example.com",
"target": "john@example.com"
}
```
* Response:
- Success:
```json
{
"success": true
}
```
- Error:
```json
{
"error": "Error message"
}
```
### 5. `POST /api/friends/block` - block updates from an email
* Request data:
```json
{
"requestor": "andy@example.com",
"target": "john@example.com"
}
```
* Response:
- Success:
```json
{
"success": true
}
```
- Error:
```json
{
"error": "Error message"
}
```
### 6. `POST /api/friends/subscribers` - get all users that can receive updates from a sender
* Request data:
```json
{
"sender": "john@example.com",
"text": "Hello World! kate@example.com"
}
```
* Response:
- Success:
```json
{
"success": true,
"recipients": [
"lisa@example.com",
"kate@example.com"
]
}
```
- Error:
```json
{
"error": "Error message"
}
```
### HTTP Status Code Used
* `200` - success
* `404` - resource not found (eg. user email not valid, user id not valid, etc.)
* `500` - Internal server error (eg. error while updating database, error while creating new record, etc.)