The idea: A simple RESTful API created using Flask and SQLAlchemy that interacts with a PostgreSQL database of doctors and reviews.
Python Version Used: Python 3.6.0
- Ensure the python3 version is 3.6.0. To check, run
python3 -V
. If you do not have it, you can install it here - Clone the Github repo:
$ git clone git@github.com:mgreenw/flask-restapi-example.git
- Move into the project directory
$ cd flask-restapi-example
- Setup a virtual environment in the project folder using python3:
$ python3 -m venv /path/to/project-parent-folder/flask-restapi-example/venv
- Start the virtual environment. You should see
(venv)
in as part of the command prompt once it is started:$ source /path/to/project-parent-folder/flask-restapi-example/venv/bin/activate
NOTE: To stop the virtual environment at any time, run(venv) $ deactivate
- Install all the requirements, including flask. Be sure not to use
sudo
as this will install flask in the global environment instead of the virtual environment:(venv) $ pip3 install -r requirements.txt
- In a separate terminal window, install PostgreSQL. To do this, either install PostgreSQL.app or use HomeBrew for MacOS:
$ brew install postgresql
- If using Homebrew, start PostgreSQL:
$ postgres -D /usr/local/var/postgres
- In a separate terminal window, run
$ psql
. Then, create a database called doctor_reviews by running# CREATE DATABASE doctor_reviews;
- Finally, run
# \q
to quit psql, and back in the original terminal window run(venv) $ python3 setup.py
to initialize the database tables.
- Set an export path for flask:
(venv) $ export FLASK_APP=app.py
- Run flask!
(venv) $ flask run
- Go to http://127.0.0.1:5000 in a browser
Returns json data about a single doctor.
-
URL
/api/v1/doctors/:id
-
Method:
GET
-
URL Params
Required:
id=[integer]
-
Data Params
None
-
Success Response:
- Code: 200
Content:{"id": 1, "name": "Max Greenwald", "reviews": []}
- Code: 200
-
Error Response:
- Code: 404 NOT FOUND
Content:{"error": "Doctor does not exist"}
- Code: 404 NOT FOUND
Creates Doctor and returns json data about that doctor
-
URL
/api/v1/doctors
-
Method:
POST
-
URL Params
None
-
Data Params
Required:
name=[string]
-
Success Response:
- Code: 201
Content:{"doctor": {"id": 1, "name": "Max Greenwald", "reviews": []}}
- Code: 201
-
Error Response:
- Code: 400 BAD REQUEST
Content:{"error": "Missing required data."}
- Code: 400 BAD REQUEST
Returns json data about a single review.
-
URL
/api/v1/reviews/:id
-
Method:
GET
-
URL Params
Required:
id=[integer]
-
Data Params
None
-
Success Response:
- Code: 200
Content:{ "description": "He's not a doctor exactly...", "doctor": { "id": 1, "name": "Max Greenwald" }, "doctor_id": 1, "id": 1 }
- Code: 200
-
Error Response:
- Code: 404 NOT FOUND
Content:{"error": "Review does not exist."}
- Code: 404 NOT FOUND
Creates a Review and returns json data about that Review
-
URL
/api/v1/reviews
-
Method:
POST
-
URL Params
None
-
Data Params
Required:
description=[text]
doctor_id=[int]
-
Success Response:
- Code: 201
Content:{ "review": { "description": "He's not a doctor exactly...", "doctor_id": 1, "id": 1 } }
- Code: 201
-
Error Response:
-
Code: 400 BAD REQUEST
Content:{"error": "Missing required data."}
OR
-
Code: 400 UNAUTHORIZED
Content:{ error : "Given doctor_id does not exist." }
-