From a039609b9da9d765fb1229ff2ed84cb00214efb5 Mon Sep 17 00:00:00 2001 From: "Prof. Nishanth Sastry" <n.sastry@surrey.ac.uk> Date: Fri, 18 Mar 2022 13:46:40 +0000 Subject: [PATCH] services directory was missing. Added now. --- services/__init__.py | 14 +++++++ services/bookings.py | 38 ++++++++++++++++++ services/movies.py | 41 +++++++++++++++++++ services/showtimes.py | 37 +++++++++++++++++ services/user.py | 92 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 222 insertions(+) create mode 100644 services/__init__.py create mode 100644 services/bookings.py create mode 100644 services/movies.py create mode 100644 services/showtimes.py create mode 100644 services/user.py diff --git a/services/__init__.py b/services/__init__.py new file mode 100644 index 0000000..ed17e01 --- /dev/null +++ b/services/__init__.py @@ -0,0 +1,14 @@ +import os +import json +from flask import make_response + + +def root_dir(): + """ Returns root director for this project """ + return os.path.dirname(os.path.realpath(__file__ + '/..')) + + +def nice_json(arg): + response = make_response(json.dumps(arg, sort_keys = True, indent=4)) + response.headers['Content-type'] = "application/json" + return response \ No newline at end of file diff --git a/services/bookings.py b/services/bookings.py new file mode 100644 index 0000000..6cf957a --- /dev/null +++ b/services/bookings.py @@ -0,0 +1,38 @@ +from services import root_dir, nice_json +from flask import Flask +import json +from werkzeug.exceptions import NotFound + + +app = Flask(__name__) + +with open("{}/database/bookings.json".format(root_dir()), "r") as f: + bookings = json.load(f) + + +@app.route("/", methods=['GET']) +def hello(): + return nice_json({ + "uri": "/", + "subresource_uris": { + "bookings": "/bookings", + "booking": "/bookings/<username>" + } + }) + + +@app.route("/bookings", methods=['GET']) +def booking_list(): + return nice_json(bookings) + + +@app.route("/bookings/<username>", methods=['GET']) +def booking_record(username): + if username not in bookings: + raise NotFound + + return nice_json(bookings[username]) + +if __name__ == "__main__": + app.run(port=5003, debug=True) + diff --git a/services/movies.py b/services/movies.py new file mode 100644 index 0000000..b9907dd --- /dev/null +++ b/services/movies.py @@ -0,0 +1,41 @@ +from services import root_dir, nice_json +from flask import Flask +from werkzeug.exceptions import NotFound +import json + + +app = Flask(__name__) + +with open("{}/database/movies.json".format(root_dir()), "r") as f: + movies = json.load(f) + + +@app.route("/", methods=['GET']) +def hello(): + return nice_json({ + "uri": "/", + "subresource_uris": { + "movies": "/movies", + "movie": "/movies/<id>" + } + }) + +@app.route("/movies/<movieid>", methods=['GET']) +def movie_info(movieid): + if movieid not in movies: + raise NotFound + + result = movies[movieid] + result["uri"] = "/movies/{}".format(movieid) + + return nice_json(result) + + +@app.route("/movies", methods=['GET']) +def movie_record(): + return nice_json(movies) + + +if __name__ == "__main__": + app.run(port=5001, debug=True) + diff --git a/services/showtimes.py b/services/showtimes.py new file mode 100644 index 0000000..b6c622d --- /dev/null +++ b/services/showtimes.py @@ -0,0 +1,37 @@ +from services import root_dir, nice_json +from flask import Flask +from werkzeug.exceptions import NotFound +import json + + +app = Flask(__name__) + +with open("{}/database/showtimes.json".format(root_dir()), "r") as f: + showtimes = json.load(f) + + +@app.route("/", methods=['GET']) +def hello(): + return nice_json({ + "uri": "/", + "subresource_uris": { + "showtimes": "/showtimes", + "showtime": "/showtimes/<date>" + } + }) + + +@app.route("/showtimes", methods=['GET']) +def showtimes_list(): + return nice_json(showtimes) + + +@app.route("/showtimes/<date>", methods=['GET']) +def showtimes_record(date): + if date not in showtimes: + raise NotFound + print showtimes[date] + return nice_json(showtimes[date]) + +if __name__ == "__main__": + app.run(port=5002, debug=True) diff --git a/services/user.py b/services/user.py new file mode 100644 index 0000000..17edad2 --- /dev/null +++ b/services/user.py @@ -0,0 +1,92 @@ +from services import root_dir, nice_json +from flask import Flask +from werkzeug.exceptions import NotFound, ServiceUnavailable +import json +import requests + + +app = Flask(__name__) + +with open("{}/database/users.json".format(root_dir()), "r") as f: + users = json.load(f) + + +@app.route("/", methods=['GET']) +def hello(): + return nice_json({ + "uri": "/", + "subresource_uris": { + "users": "/users", + "user": "/users/<username>", + "bookings": "/users/<username>/bookings", + "suggested": "/users/<username>/suggested" + } + }) + + +@app.route("/users", methods=['GET']) +def users_list(): + return nice_json(users) + + +@app.route("/users/<username>", methods=['GET']) +def user_record(username): + if username not in users: + raise NotFound + + return nice_json(users[username]) + + +@app.route("/users/<username>/bookings", methods=['GET']) +def user_bookings(username): + """ + Gets booking information from the 'Bookings Service' for the user, and + movie ratings etc. from the 'Movie Service' and returns a list. + :param username: + :return: List of Users bookings + """ + if username not in users: + raise NotFound("User '{}' not found.".format(username)) + + try: + users_bookings = requests.get("http://127.0.0.1:5003/bookings/{}".format(username)) + except requests.exceptions.ConnectionError: + raise ServiceUnavailable("The Bookings service is unavailable.") + + if users_bookings.status_code == 404: + raise NotFound("No bookings were found for {}".format(username)) + + users_bookings = users_bookings.json() + + # For each booking, get the rating and the movie title + result = {} + for date, movies in users_bookings.iteritems(): + result[date] = [] + for movieid in movies: + try: + movies_resp = requests.get("http://127.0.0.1:5001/movies/{}".format(movieid)) + except requests.exceptions.ConnectionError: + raise ServiceUnavailable("The Movie service is unavailable.") + movies_resp = movies_resp.json() + result[date].append({ + "title": movies_resp["title"], + "rating": movies_resp["rating"], + "uri": movies_resp["uri"] + }) + + return nice_json(result) + + +@app.route("/users/<username>/suggested", methods=['GET']) +def user_suggested(username): + """ + Returns movie suggestions. The algorithm returns a list of 3 top ranked + movies that the user has not yet booked. + :param username: + :return: Suggested movies + """ + raise NotImplementedError() + + +if __name__ == "__main__": + app.run(port=5000, debug=True) -- GitLab