def get_messages(flight_id, lastMessageTime): if lastMessageTime is None: # Get all messages for the given flight sQuery = ("SELECT * FROM messages WHERE flight_id=? ORDER BY " "time DESC") else: # Only get more recent messages since last push to client sQuery = ("SELECT * FROM messages WHERE flight_id=? AND time>" + str(lastMessageTime) + " ORDER BY time DESC") with app.app_context(): alive_counter = 0 while True: # TODO use blocking/events, not polling time2.sleep(push_poll) alive_counter = alive_counter + push_poll message_results = ApiFunctions.query_db(sQuery, [flight_id]) if len(message_results) > 0: lastMessageTime = message_results[0]['time'] # Format messages fo transport messages = [] for row in message_results: message = Message.Message(row['id'], row['body'], row['time'], flight_id) messages.append(message.to_json()) # Break loop by returning the latest messages # NB no need to use Flask's jsonify object here, as it adds # lots of additional processing bloat for a Response object # (which we do not use or need here for the stream). return [ json.dumps({'messages': messages}, separators=(',', ':')), # Minified lastMessageTime ] elif alive_counter >= keep_alive_poll: raise IdleConnectionException()
def get_gates(): gates = [] results = ApiFunctions.query_db("SELECT * FROM gates") for row in results: gate = Gate(row['id'], row['name'], row['airport_id']) gates.append(gate.to_json()) return jsonify({'gates': gates})
def get_users(): users = [] results = ApiFunctions.query_db("SELECT * FROM users") for row in results: user = User(row['id']) users.append(user.to_json()) return jsonify({'users': users})
def get_airports(): airports = [] results = ApiFunctions.query_db("SELECT * FROM airports") for row in results: airport = Airport(row['id'], row['airport_short'], row['airport_full']) airports.append(airport.to_json()) return jsonify({'airports': airports})
def get_airline(airline_id): row = ApiFunctions.query_db("SELECT * FROM airlines WHERE id = ?", [airline_id], one=True) if row is None: abort(404) airline = Airline(row['id'], row['airline_short'], row['airline_full']) return jsonify({'airline': airline.to_json()})
def get_airlines(): airlines = [] results = ApiFunctions.query_db("SELECT * FROM airlines") for row in results: airline = Airline(row['id'], row['airline_short'], row['airline_full']) airlines.append(airline.to_json()) return jsonify({'airlines': airlines})
def get_messages(): messages = [] results = ApiFunctions.query_db("SELECT * FROM messages") for row in results: message = Message(row['id'], row['body'], row['time'], row['flight_id']) messages.append(message.to_json()) return jsonify({'messages': messages})
def get_flight_gates(): flight_gates = [] results = ApiFunctions.query_db("SELECT * FROM flight_gates") for row in results: flight_gate = FlightGate(row['id'], row['gate_id'], row['flight_id']) flight_gates.append(flight_gate.to_json()) return jsonify({'flight_gates': flight_gates})
def get_user_messages(user_id): messages = [] # Get the flight ids from ListeningUsers flights = ApiFunctions.query_db("SELECT * FROM listening_users WHERE user_id=?", [user_id]); for row in flights: flight_id = row['flight_id']; # Get all the messages that correspond to this flight_id message_results = ApiFunctions.query_db("SELECT * FROM messages where flight_id=?", [flight_id]); # Add all the messages from that flight to our list for row in message_results: message = Message.Message(row['id'], row['text'], row['time'], row['flight_id']) messages.append(message.to_json()) return jsonify({'messages': messages})
def get_message(message_id): row = ApiFunctions.query_db("SELECT * FROM messages WHERE id = ?", [message_id], one=True) if row is None: abort(404) message = Message(row['id'], row['body'], row['time'], row['flight_id']) return jsonify({'message': message.to_json()})
def find_flight(gate): row = ApiFunctions.query_db("SELECT * FROM flights WHERE gate = ?", [gate], one=True) if row is None: abort(404) airline = ApiFunctions.query_db( "SELECT airline_short, airline_full FROM airlines WHERE id = ?", [row['airline_id']], one=True) flight = DisplayFlight.DisplayFlight(row['id'], airline['airline_full'], airline['airline_short'], row['dest_airport'], row['flight_number'], row['gate'], row['departure']) return jsonify({'flights': [flight.to_json()]})
def get_flights(): flights = [] results = ApiFunctions.query_db( "SELECT * FROM flights WHERE departure > strftime('%s', 'now') ORDER BY departure" ) for row in results: airline = ApiFunctions.query_db( "SELECT airline_short, airline_full FROM airlines WHERE id = ?", [row['airline_id']], one=True) flight = DisplayFlight.DisplayFlight(row['id'], airline['airline_full'], airline['airline_short'], row['dest_airport'], row['flight_number'], row['gate'], row['departure']) flights.append(flight.to_json()) return jsonify({'flights': flights})
def create_flight_gate(): if not request.json or not 'gate_id' in request.json or not 'flight_id' in request.json: abort(400) gate_id = request.json['gate_id'] flight_id = request.json['flight_id'] result = ApiFunctions.post_db( "INSERT INTO flight_gates VALUES (NULL, ?, ?)", [gate_id, flight_id]) inserted_id = c.lastrowid print u'Inserted new flight_gate at row ' + str(inserted_id) return jsonify({'id': str(inserted_id)}), 201
def find_gates(): if not request.json or not 'id' in request.json: abort(400) airport_id = request.json['id'] gates = [] results = ApiFunctions.query_db( "SELECT * FROM gates where airport_id=?", [airport_id]) for row in results: gate = Gate(row['id'], row['name'], row['airport_id']) gates.append(gate.to_json()) return jsonify({'gates': gates})
def get_flight_messages(flight_id): try: message_results = ApiFunctions.query_db( "SELECT * FROM messages WHERE flight_id=? ORDER BY time DESC", [flight_id]) messages = [] for row in message_results: message = Message.Message(row['id'], row['body'], row['time'], flight_id) messages.append(message.to_json()) return jsonify({'messages': messages}) except Exception as e: return jsonify({'err': str(e)})
def create_airport(): if not request.json or not 'airport_short' in request.json or not 'airport_full' in request.json: abort(400) airport_short = request.json['airport_short'] airport_full = request.json['airport_full'] result = ApiFunctions.post_db( "INSERT INTO airports VALUES (NULL, ?, ?)", [airport_short, airport_full]) inserted_id = c.lastrowid print u'Inserted new airport at row ' + str(inserted_id) return jsonify({'id': str(inserted_id)}), 201
def create_gate(): if not request.json or not 'name' in request.json or not 'airport_id' in request.json: abort(400) name = request.json['name'] airport_id = request.json['airport_id'] if not name: abort(400) result = ApiFunctions.post_db("INSERT INTO gates VALUES (NULL, ?, ?)", [name, airport_id]) inserted_id = c.lastrowid print u'Inserted new gate at row ' + str(inserted_id) return jsonify({'id': str(inserted_id)}), 201
def create_flight_message(flight_id): if not request.json or not 'body' in request.json or not 'time' in request.json: abort(400) body = request.json['body'] time = request.json['time'] if not body: abort(400) result = ApiFunctions.post_db( "INSERT INTO messages VALUES (NULL, ?, ?, ?)", [body, time, flight_id]) inserted_id = c.lastrowid return jsonify({'id': str(inserted_id)}), 201
def create_flight(): if not request.json or not 'airline_id' in request.json or not 'dest_airport' in request.json or not 'flight_number' in request.json or not 'gate' in request.json or not 'departure' in request.json: abort(400) airline_id = request.json['airline_id'] dest_airport = request.json['dest_airport'] flight_number = request.json['flight_number'] gate = request.json['gate'] departure = request.json['departure'] if not airline_id or not dest_airport or not gate: abort(400) result = ApiFunctions.post_db( "INSERT INTO flights VALUES (NULL, ?, ?, ?, ?, ?)", [airline_id, dest_airport, flight_number, gate, departure]) inserted_id = c.lastrowid print u'Inserted new flight at row ' + str(inserted_id) return jsonify({'id': str(inserted_id)}), 201
def create_user(): result = ApiFunctions.post_db("INSERT INTO users VALUES (NULL)"); inserted_id = c.lastrowid user = User(id) print u'Inserted new user at row ' + str(inserted_id) return jsonify({'id': str(inserted_id)}), 201
def delete_message(message_id): ApiFunctions.post_db("DELETE FROM messages WHERE id=?", [message_id]) print u'Deleted message with ID ' + str(inserted_id) return jsonify({'result': True})
def delete_airport(airport_id): ApiFunctions.post_db("DELETE FROM airports WHERE id=?", [airport_id]) print u'Deleted airport with ID ' + str(inserted_id) return jsonify({'result': True})
def delete_flight_gate(flight_gate_id): ApiFunctions.post_db("DELETE FROM flight_gates WHERE id=?", [flight_gate_id]) print u'Deleted listening_user with ID ' + str(inserted_id) return jsonify({'result': True})
def delete_user(user_id): ApiFunctions.post_db("DELETE FROM users WHERE id=?", [user_id]) print u'Deleted user with ID ' + str(inserted_id) return jsonify({'result': True})
import requests import json import ApiFunctions as api apiUrl = " https://us-central1-marcy-playground.cloudfunctions.net/ordoroCodingTest" r = requests.get(apiUrl) if not r.ok: raise Exception("url down {}".format(apiUrl)) data = r.json()['data'] users = {} for access in data: api.registerLogin(access, users) emails = api.emailList(users) domains = api.usersPerDomain(users) aprilBois = api.aprilUsers(users) my_response = { "your_email_address": "*****@*****.**", "unique_emails": emails, "user_domain_counts": domains, "april_emails": aprilBois } r = requests.post(apiUrl, json=my_response) print("Submitted {}".format(json.dumps(my_response)), "\n") print("Post Response {}".format(r), "\n")
def delete_flight(flight_id): ApiFunctions.post_db("DELETE FROM flights WHERE id=?", [flight_id]) print u'Deleted flight with ID ' + str(inserted_id) return jsonify({'result': True})