Skip to content
Snippets Groups Projects
Commit a039609b authored by Sastry, Nishanth Prof (Comp Sci & Elec Eng)'s avatar Sastry, Nishanth Prof (Comp Sci & Elec Eng)
Browse files

services directory was missing. Added now.

parent 1bc108e6
No related branches found
No related tags found
No related merge requests found
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
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)
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)
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)
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment