<file_sep>Click==7.0
Flask==1.0.2
Flask-PyMongo==2.2.0
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.0
pymongo==3.7.2
python-dateutil==2.8.0
six==1.12.0
Werkzeug==0.14.1
<file_sep>RESTful Flask API with MongoDB
__________________________________
This repo is an example of CREATE-READ-UPDATE-DELETE (CRUD) API in Python, based on Flask, and uses MongoDB as a database.

Tech/Framework Used
__________________________________
* Python
* Flask
* MongoDB

Getting Started
__________________________________
These instructions will get a copy of the project up and running on your local machine.

* Clone project onto local machine.
* Activate your environment.
  - pip install virtualenv
  - virtualenv env
  - source env/bin/activate
* Install requirements for project.
  - pip install -r requirements.txt
* Start mongoDB.
  mongod
* In a second tab, start flask instance.
  python3 server.py
* In a third tab, you can use and test the API by trying out the following commands:
  * To post (Create a new entry)
    - curl -v -XPOST -H "Content-Type: application/json" http://localhost:5000/info -d '{"name":"<string name>"}'
  * To get (Read an existing entry)
    - curl -v -XGET http://localhost:5000/info/<id # of entry>
  * To put (Update an existing entry)
    - curl -v -XPUT -H "Content-Type: application/json" http://localhost:5000/info/<id of entry> -d '{"name":"<update value>"}'
  * To delete (Delete an existing entry)
    - curl -v -XDELETE http://localhost:5000/info/<id # of entry>
* Make sure to end the environment!
  - deactivate

Extra Notes
__________________________________
* If there are issues with bson (i.e. "cannot import name 'abc' from 'bson.py3compat'"), follow these directions to resolve:
  - Uninstall pymongo and reinstall with command:
    - python3 -m pip install pymongo

Misc
__________________________________
Information on CRUD in Python:
https://realpython.com/flask-connexion-rest-api/
<file_sep>from flask import Flask, request, jsonify
from flask_pymongo import MongoClient
from bson import ObjectId, errors
import sys

app = Flask(__name__)
client = MongoClient('mongodb://localhost:27017/') # Have to specify the host & port
db = client.restdb
collection = db.cats

@app.route('/')
def homepage():
    return "Homepage", 200

@app.route('/info', methods=['POST'])
def post_info():
    if not request.json:
        return "Please enter request in JSON", 400

    info = request.json
    id = collection.insert_one(info).inserted_id

    return jsonify(str(id)), 201

@app.route('/info/<id>', methods=['GET'])
def get_info(id):
    try:
        result = collection.find_one({'_id': ObjectId(id)})
    except errors.InvalidId:
        return "ID entered invalid, please enter valid ID", 400

    if not result:
        return "ID not found in database", 404

    return jsonify({'name': result['name']}), 200

@app.route('/info/<id>', methods=['DELETE'])
def delete_info(id):
    try:
        result = collection.find_one({'_id': ObjectId(id)})
    except errors.InvalidId:
        return "ID entered invalid", 400

    if not result:
        return "ID not found in database", 404

    collection.remove({'_id': ObjectId(id)})

    return jsonify(result['name']), 200

@app.route('/info/<id>', methods=['PUT'])
def put_info(id):
    try:
        result = collection.find_one({'_id': ObjectId(id)})
    except errors.InvalidId:
        return "ID entered invalid", 400

    if not result:
        return "ID not found in database", 404

    if not request.json:
        return "Please enter request in JSON", 400

    info = request.json

    collection.update({'_id': ObjectId(id)}, {'$set': info})

    result = collection.find_one({'_id': ObjectId(id)})

    return jsonify(result['name']), 200

if __name__ == '__main__':
    app.run(debug=False, host="0.0.0.0")
