def movie_info(movieid): if movieid not in movies: raise NotFound result = movies[movieid] result["uri"] = "/movies/{}".format(movieid) return nice_json(result)
def hello(): return nice_json({ "uri": "/", "subresource_uris": { "bookings": "/bookings", "booking": "/bookings/<username>" } })
def hello(): return nice_json({ "uri": "/", "subresource_uris": { "movies": "/movies", "movie": "/movies/<id>" } })
def hello(): return nice_json({ "uri": "/", "subresource_uris": { "showtimes": "/showtimes", "showtime": "/showtimes/<date>" } })
def hello(): return nice_json({ "uri": "/", "subresource_uris": { "users": "/users", "user": "******", "bookings": "/users/<username>/bookings", "suggested": "/users/<username>/suggested" } })
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)
def user_record(username): if username not in users: raise NotFound return nice_json(users[username])
def get_booking_by_username(username): if username not in bookings: return "No booking found with username {}".format(username) return nice_json(bookings[username])
def get_user_by_username(username): if username not in users: return "No user found with username {}".format(username) return nice_json(users[username])
def showtimes_list(): result = requests.get("http://127.0.0.1:5002/showtimes") result = result.json() return nice_json(result)
def querydatabase(db, table): con = sqlite3.connect(db) cur = con.cursor() c = cur.execute("SELECT *from " + table) return nice_json(cur.fetchall())
def booking_record(username): if username not in bookings: raise NotFound return nice_json(bookings[username])
def showtimes_list(): return nice_json(showtimes)
def root(): return nice_json(ROOT_MOVIES)
def get_all_flights(): return nice_json(flight_dates)
def get_passenger_flights(passenger): return nice_json(passengers[passenger]["flight"])
def get_passenger_specific_flight(passenger): passenger_flight = passengers[passenger]["flight"] response = requests.get( "http://127.0.0.1:5001/flights/{}".format(passenger_flight)).json() return nice_json(response)
def get_passenger_info(passenger): return nice_json(passengers[passenger])
def get_all_passengers(): return nice_json(passengers)
def movies_list(): result = requests.get("http://127.0.0.1:5001/movies") result = result.json() return nice_json(result)
def movie_record(): return nice_json(movies)
def booking_list(): return nice_json(bookings)
def querydatabasebyID(db, table, id): con = sqlite3.connect(db) cur = con.cursor() cur.execute("SELECT * from " + table + " WHERE id = ? ", (id, )) return nice_json(cur.fetchall())
def showtimes_record(date): if date not in showtimes: raise NotFound print showtimes[date] return nice_json(showtimes[date])
def get_all_flights_from_date(date): return nice_json(flight_dates[date])
def user_bookings_add(username, page): with open("{}/tokens/token.json".format(root_dir()), "r") as ff: try: token = json.load(ff) except: raise Unauthorized() headers = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(token['access_token']) } auth_check = requests.get("http://127.0.0.1:5004/check", headers=headers) auth_check = auth_check.json() if auth_check['msg'] != 'OK': raise Unauthorized(auth_check['msg']) headers = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(token['refresh_token']) } if auth_check['msg'] == 'Token has expired': auth_refresh = requests.post("http://127.0.0.1:5004/refresh", headers=headers) if username not in users: raise NotFound("User '{}' not found.".format(username)) if request.method == 'GET': result = {} try: showtimes = requests.get( "http://127.0.0.1:5002/showtimes/{}".format(page)) except requests.exceptions.ConnectionError: raise ServiceUnavailable("The Showtimes service is unavailable.") showtimes = showtimes.json() for movieid in showtimes: result[movieid] = [] 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.") try: movies_resp = movies_resp.json() except ValueError: raise ServiceUnavailable("Sorry! No movies in this day.") result[movieid].append({ "title": movies_resp["title"], "rating": movies_resp["rating"] }) return nice_json(result) if request.method == 'POST': raw = request.get_json() try: user_resp = requests.get( "http://127.0.0.1:5000/users/{}".format(username)) except: raise ServiceUnavailable( "Sorry, service unavailable at the moment!") try: result = requests.post( "http://127.0.0.1:5003/bookings/{}/add".format(username), json={username: raw}) except: raise ServiceUnavailable( "Sorry, service unavailable at the moment!") result = result.json() user_resp = user_resp.json() content = { "id": user_resp["id"], "name": user_resp["name"], "last_active": 0 } content = {username: content} with open("{}/database/users.json".format(root_dir())) as ff: data = json.load(ff) data.update(content) with open("{}/database/users.json".format(root_dir()), "w+") as ff: json.dump(data, ff) users.update(content) return nice_json(result) raise NotImplementedError()
def movie_record(): print(request.remote_addr) return nice_json(movies)
def users_list(): return nice_json(users)
def check(): ret = {'msg': 'OK'} result = nice_json(ret) return result, 200
def users(): if request.method == 'POST': raise NotImplementedError() else: return nice_json([x.to_json() for x in User.query.all()])
def get_all_bookings(): return nice_json(bookings)
def user_record(username): user = User.query.filter_by(username=username).first() if user is None: raise NotFound return nice_json(user.to_json())
def get_all_users(): return nice_json(users)