Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Health feature #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
from flask import Flask
from flask import Flask, jsonify
import os

from blueprints.movies import movies
from model.movies import Movies
from model.mongo import Movies
from model.mongo import Health

app = Flask(__name__)

app.movies = Movies()
app.health = Health()


@app.route('/')
def hello_world():
return 'Hello, World!'


@app.route('/health')
def health():
h = {'health': True}
h.update({'database_connection': app.health.check_connection()})
for k, v in h.items():
if not v:
h['health'] = False
break
return jsonify(h)

app.register_blueprint(movies, url_prefix='/movies')

if __name__ == '__main__':
Expand Down
46 changes: 28 additions & 18 deletions model/mongo.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
from pymongo import MongoClient
from os import getenv


class Movies():

def __init__(self):
client = MongoClient('ds013456.mlab.com', 13456)
client['piank-test'].authenticate('test', 'test')
db = client['piank-test']
client = MongoClient(getenv('MONGO_SERVER'), int(getenv('MONGO_PORT')))
db = client[getenv('MONGO_DB')]
db.authenticate(getenv('MONGO_USER'), getenv('MONGO_PASS'))
self.movies = db.movies
d = self.movies.find_one(sort=[('_id', -1)])
if d is None:
self.id = 0
else:
self.id = int(d['id'])

def create_movie(self, data):
return self.movies.insert_one(data).inserted_id
self.id += 1
data['id'] = str(self.id)
self.movies.insert_one(data)
return self.movies.find_one(data, projection={'_id': False})

def get_movie(self, id):
return self.movies.find_one({'_id': id})
return self.movies.find_one({'id': str(id)}, projection={'_id': False})

def update_movie(self, id, data):
return self.movies.find_one_and_replace({'_id': id}, data)
return self.movies.find_one_and_replace({'id': str(id)}, data)

def delete_movie(self, id):
return self.movies.delete_one({'_id': id})

# Only for testing
if __name__ == "__main__":
movies = Movies()
new_id = movies.create_movie({"title": "Trainspotting", "year": 1995})
print ("Created movie:", new_id)
retrieved_movie = movies.get_movie(new_id)
print ("Retrieved movie: ", retrieved_movie)
movies.update_movie(new_id, {"title": "Trainspotting", "year": 1996})
retrieved_movie = movies.get_movie(new_id)
print ("Updated movie: ", retrieved_movie)
return self.movies.find_one_and_delete({'id': str(id)}, projection={'_id': False})


class Health:
def check_connection(self):
try:
client = MongoClient(getenv('MONGO_SERVER'), int(getenv('MONGO_PORT')))
db = client[getenv('MONGO_DB')]
db.authenticate(getenv('MONGO_USER'), getenv('MONGO_PASS'))
except:
return False
return True
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pytest
behave
mock
requests
pymongo