def get(self, username): try: current_app.logger.info("Received UserFriendsResource GET Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) user = User.get_user_by_username(username) friends_usernames = user["user"]["friends_usernames"] response = {"friends": []} if friends_usernames: for username in friends_usernames: return_friend = User.get_user_by_username(username)["user"] friend = { "username": username, "profile_pic": return_friend["profile_pic"], "name": return_friend["name"] } response["friends"].append(friend) current_app.logger.debug("Python Server Response: 200 - %s", user) return make_response(jsonify(response), 200) except UserNotFoundException as e: status_code = 403 message = e.args[0] current_app.logger.error("Python Server Response: %s - %s", status_code, message) return ErrorHandler.create_error_response(status_code, message)
def post(self): try: current_app.logger.info("Received StoriesResource POST Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) story_data = json.loads(request.data) story_created = Story.create( story_data["user_id"], story_data["location"], story_data["visibility"], story_data["title"], story_data["description"], story_data["is_quick_story"], story_data["timestamp"]) current_app.logger.debug("Python Server Response: 200 - %s", story_created) if config.firebase_config.FIREBASE_NOTIFICATIONS_ENABLED is True: try: user = User.get_user_by_id(story_data["user_id"]) if user is not None: user = user["user"] for friend_username in user["friends_usernames"]: try: FirebaseManager.send_firebase_message( user["name"], friend_username, NOTIFICATION_TYPE_STORY_MESSAGE, NOTIFICATION_TYPE_STORY) except UserNotFoundException: pass except UserNotFoundException: pass return make_response(jsonify(story_created), 200) except ValueError: error = "Unable to handle StoriesResource POST Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error)
def get(self, from_username): try: current_app.logger.info( "Received FriendshipRequestResource - sent requests - GET Request" ) StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) friendship_requests = FriendshipRequest.get_sent_friendship_requests( from_username) if friendship_requests["friendship_requests"]: for friendship_request in friendship_requests[ "friendship_requests"]: user = User.get_user_by_username( friendship_request["to_username"])["user"] friendship_request["profile_pic"] = user["profile_pic"] friendship_request["name"] = user["name"] current_app.logger.debug("Python Server Response: 200 - %s", friendship_requests) return make_response(jsonify(friendship_requests), 200) except ValueError: error = "Unable to handle FriendshipRequestResource - sent requests - GET Request" current_app.logger.error("Python Server Response: %s - %s", 500, error) return ErrorHandler.create_error_response(500, error) except UserNotFoundException as e: status_code = 403 message = e.args[0] current_app.logger.error("Python Server Response: %s - %s", status_code, message) return ErrorHandler.create_error_response(status_code, message)
def post(self): try: current_app.logger.info( "Received FriendshipRequestResource POST Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) request_data = json.loads(request.data) friendship_request_created = FriendshipRequest.create( request_data["from_username"], request_data["to_username"], int(round(time.time() * 1000))) from_username = User.get_user_by_username( request_data["from_username"]) if request_data["to_username"] in from_username["user"][ "friends_usernames"]: current_app.logger.debug("Python Server Response: 409 - %s", "Users are already friends.") return make_response("Users are already friends.", 409) if friendship_request_created is None: current_app.logger.debug("Python Server Response: 409 - %s", "Friendship request already exists") return make_response("Friendship request already exists", 409) else: current_app.logger.debug("Python Server Response: 201 - %s", friendship_request_created) if config.firebase_config.FIREBASE_NOTIFICATIONS_ENABLED is True: try: user = User.get_user_by_username( request_data["from_username"])["user"] if user is not None: FirebaseManager.send_firebase_message( user["name"], request_data["to_username"], NOTIFICATION_TYPE_FRIENDSHIP_REQUEST_MESSAGE, NOTIFICATION_TYPE_FRIENDSHIP_REQUEST) except UserNotFoundException: pass return make_response(jsonify(friendship_request_created), 201) except ValueError: error = "Unable to handle FriendshipRequestResource POST Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error) except messaging.ApiCallError: error = "Unable to send Firebase Notification - ApiCallError" current_app.logger.error("Python Server Response: 409 - %s", error) return ErrorHandler.create_error_response(500, error) except UserNotFoundException as e: status_code = 403 message = e.args[0] current_app.logger.error("Python Server Response: %s - %s", status_code, message) return ErrorHandler.create_error_response(status_code, message)
def get(self): try: current_app.logger.info("Received UsersResource GET Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) users_response = User.get_all() current_app.logger.debug("Python Server Response: 200 - %s", users_response) return make_response(jsonify(users_response), 200) except ValueError: error = "Unable to handle UsersResource GET Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error)
def get(self): current_app.logger.info("Received PingSharedServerResource GET Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) current_app.logger.info("Sending Request to Shared Server at:" + SHARED_SERVER_PING_PATH) response = requests.get(SHARED_SERVER_PING_PATH) current_app.logger.debug("Shared Server Response: %s", response.status_code) if response.ok: current_app.logger.debug("Python Server Response: 200 - %s", SUCCESS_MESSAGE) return Response(SUCCESS_MESSAGE, 200) else: failure_message = "Connection to Shared Server failed" current_app.logger.warning("Python Server Response: 404 - %s", failure_message) return Response(failure_message, 404)
def get(self, user_id): try: current_app.logger.info("Received SingleUserResource GET Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) user = User.get_user_by_id(user_id) current_app.logger.debug("Python Server Response: 200 - %s", user) return make_response(jsonify(user), 200) except UserNotFoundException as e: status_code = 403 message = e.args[0] current_app.logger.error("Python Server Response: %s - %s", status_code, message) return ErrorHandler.create_error_response(status_code, message)
def post(self): try: current_app.logger.info("Received UserLoginResource POST Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) credentials = json.loads(request.data) payload = { "username": credentials["username"], "password": credentials["password"] } headers = { 'content-type': 'application/json', 'Authorization': 'Bearer {}'.format(APP_SERVER_TOKEN) } response = requests.post(SHARED_SERVER_TOKEN_PATH, data=json.dumps(payload), headers=headers) current_app.logger.debug("Shared Server Response: %s - %s", response.status_code, response.text) json_response = json.loads(response.text) if response.ok: user_response = User.get_user_by_username( credentials["username"]) built_response = { "token": { "expiresAt": json_response["token"]["expiresAt"], "token": json_response["token"]["token"] }, "user": user_response["user"] } current_app.logger.debug("Python Server Response: %s - %s", response.status_code, built_response) else: built_response = { "error": { "code": json_response["code"], "message": json_response["message"] } } current_app.logger.error("Python Server Response: %s - %s", response.status_code, built_response) return make_response(jsonify(built_response), response.status_code) except ValueError: error = "Unable to handle UsersResource POST Request" current_app.logger.error("Python Server Response: %s - %s", 500, error) return ErrorHandler.create_error_response(500, error)
def post(self): try: current_app.logger.info( "Received SetFriendshipResource POST Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) friendship_data = json.loads(request.data) User.add_friend(friendship_data["from_username"], friendship_data["to_username"]) User.add_friend(friendship_data["to_username"], friendship_data["from_username"]) deleted_friendship_request = FriendshipRequest.delete( friendship_data["from_username"], friendship_data["to_username"]) if deleted_friendship_request is None: current_app.logger.debug( "Python Server Response: 409 - %s", "No friendship request found for those parameters.") return make_response( "No friendship request found for those parameters.", 409) else: current_app.logger.debug("Python Server Response: 201 - %s", deleted_friendship_request) if config.firebase_config.FIREBASE_NOTIFICATIONS_ENABLED is True: try: user = User.get_user_by_username( friendship_data["to_username"])["user"] if user is not None: FirebaseManager.send_firebase_message( user["name"], friendship_data["from_username"], NOTIFICATION_TYPE_FRIENDSHIP_REQUEST_ACCEPTED_MESSAGE, NOTIFICATION_TYPE_FRIENDSHIP_REQUEST_ACCEPTED) except UserNotFoundException: pass return make_response(jsonify(deleted_friendship_request), 201) except UserNotFoundException: error = "Unable to find a User with the parameters given. SetFriendshipResource POST Request" current_app.logger.error("Python Server Response: 403 - %s", error) return ErrorHandler.create_error_response(403, error) except ValueError: error = "Unable to handle SetFriendshipResource POST Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error)
def get(self, to_username): try: current_app.logger.info( "Received DirectMessagesReceivedResource - received requests - GET Request" ) StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) direct_messages = DirectMessage.get_received_direct_messages( to_username) current_app.logger.debug("Python Server Response: 200 - %s", direct_messages) return make_response(jsonify(direct_messages), 200) except ValueError: error = "Unable to handle DirectMessagesReceivedResource - received requests - GET Request" current_app.logger.error("Python Server Response: %s - %s", 500, error) return ErrorHandler.create_error_response(500, error)
def get(self): try: current_app.logger.info("Received StatsResource GET Request") stats = StatManager.get_stats() current_app.logger.debug("Python Server Response: 200 - %s", stats) return make_response(jsonify(stats), 200) except ValueError: error = "Unable to handle StatsResource GET Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error)
def put(self, user_id): try: current_app.logger.info( "Received UserFirebaseTokenResource PUT Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) request_data = json.loads(request.data) updated_user = User.update_user_firebase_token( user_id, request_data["firebase_token"]) current_app.logger.debug("Python Server Response: 200 - %s", updated_user) return make_response(jsonify(updated_user), 200) except UserNotFoundException as e: status_code = 403 message = e.args[0] current_app.logger.error("Python Server Response: %s - %s", status_code, message) return ErrorHandler.create_error_response(status_code, message)
def get(self): try: user_id = request.args.get('user_id') current_app.logger.info( "Received StoriesResource GET Request for User ID: " + user_id) StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) response = Story.get_by_user(user_id) current_app.logger.debug("Python Server Response: 200 - %s", response) return make_response(jsonify(response), 200) except ValueError: error = "Unable to handle StoriesResource GET Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error) except UserNotFoundException as e: status_code = 403 message = e.args[0] current_app.logger.error("Python Server Response: %s - %s", status_code, message) return ErrorHandler.create_error_response(status_code, message)
def get(self, username): try: current_app.logger.info( "Received UserDirectMessagesResource - received requests - GET Request" ) StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) direct_messages = DirectMessage.get_user_direct_messages_sorted_by_timestamp( username) current_app.logger.debug("Python Server Response: 200 - %s", direct_messages) return make_response(jsonify(direct_messages), 200) except UserNotFoundException: error = "Unable to find a User with the parameters given. UserDirectMessagesResource POST Request" current_app.logger.error("Python Server Response: 403 - %s", error) return ErrorHandler.create_error_response(403, error) except ValueError: error = "Unable to handle UserDirectMessagesResource - received requests - GET Request" current_app.logger.error("Python Server Response: %s - %s", 500, error) return ErrorHandler.create_error_response(500, error)
def delete(self, story_id): try: current_app.logger.info( "Received SingleStoryResource DELETE Request for story: " + story_id) StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) deleted_story = Story.delete(story_id) if deleted_story is None: current_app.logger.debug("Python Server Response: 409 - %s", "No story found with that ID!.") return make_response("No story found with that ID!", 409) else: current_app.logger.debug("Python Server Response: 201 - %s", deleted_story) return make_response(jsonify(deleted_story), 201) except ValueError: error = "Unable to handle SingleFriendshipRequestResource - DELETE Request" current_app.logger.error("Python Server Response: %s - %s", 500, error) return ErrorHandler.create_error_response(500, error)
def post(self): try: current_app.logger.info( "Received DirectMessageResource POST Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) request_data = json.loads(request.data) User.get_user_by_username(request_data["to_username"]) direct_message_created = DirectMessage.create( request_data["from_username"], request_data["to_username"], request_data["message"], int(round(time.time() * 1000))) if direct_message_created is None: raise ValueError('DB error') else: if config.firebase_config.FIREBASE_NOTIFICATIONS_ENABLED is True: try: user = User.get_user_by_username( request_data["from_username"])["user"] if user is not None: FirebaseManager.send_firebase_message( user["name"], request_data["to_username"], request_data["message"], NOTIFICATION_TYPE_MESSAGE) except UserNotFoundException: pass current_app.logger.debug("Python Server Response: 201 - %s", direct_message_created) return make_response(jsonify(direct_message_created), 201) except ValueError: error = "Unable to handle DirectMessageResource POST Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error) except UserNotFoundException: error = "Unable to send message - User not found" current_app.logger.error("Python Server Response: 403 - %s", error) return ErrorHandler.create_error_response(403, error) except messaging.ApiCallError: error = "Unable to send Firebase Notification - ApiCallError" current_app.logger.error("Python Server Response: 409 - %s", error) return ErrorHandler.create_error_response(409, error)
def patch(self, story_id): try: current_app.logger.info( "Received SingleStoryResource PATCH Request for story: " + str(request.data)) StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) patch_document = json.loads(request.data) # See http://jsonpatch.com/ for more info story_updated = None if patch_document["path"] == "/reactions" and patch_document[ "op"] == "add": current_app.logger.debug("Add reaction: " + str(patch_document["value"])) story_updated = Story.add_reaction(story_id, patch_document["value"]) if patch_document["path"] == "/reactions" and patch_document[ "op"] == "remove": current_app.logger.debug("Remove reaction: " + str(patch_document["value"])) story_updated = Story.remove_reaction(story_id, patch_document["value"]) if patch_document["path"] == "/comments" and patch_document[ "op"] == "add": current_app.logger.debug("Adding comment to story: " + str(patch_document["value"])) story_updated = Story.add_comment(story_id, patch_document["value"]) current_app.logger.debug("Python Server Response: 200 - %s", story_updated) return make_response(jsonify(story_updated), 200) except ValueError: error = "Unable to handle StoriesResource POST Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error)
def get(self, user_id, query): try: current_app.logger.info("Received UserSearchResource GET Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) users_response = User.get_all() users = users_response["users"] built_response = {"found_users": []} User.get_user_by_id(user_id)["user"]["username"] if users: for user in users: if user_id != user["user_id"]: if query.lower() in user["username"].lower( ) or query.lower() in user["name"].lower(): final_user = { "username": user["username"], "profile_pic": user["profile_pic"], "name": user["name"] } built_response["found_users"].append(final_user) current_app.logger.debug("Python Server Response: 200 - %s", built_response) return make_response(jsonify(built_response), 200) except ValueError: error = "Unable to handle UserSearchResource GET Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error) except UserNotFoundException as e: status_code = 403 message = e.args[0] current_app.logger.error("Python Server Response: %s - %s", status_code, message) return ErrorHandler.create_error_response(status_code, message)
def delete(self, from_username, to_username): try: current_app.logger.info( "Received SingleFriendshipRequestResource - DELETE Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) deleted_friendship_request = FriendshipRequest.delete( from_username, to_username) if deleted_friendship_request is None: current_app.logger.debug( "Python Server Response: 409 - %s", "No friendship request found for those parameters.") return make_response( "No friendship request found for those parameters.", 409) else: current_app.logger.debug("Python Server Response: 201 - %s", deleted_friendship_request) return make_response(jsonify(deleted_friendship_request), 201) except ValueError: error = "Unable to handle SingleFriendshipRequestResource - DELETE Request" current_app.logger.error("Python Server Response: %s - %s", 500, error) return ErrorHandler.create_error_response(500, error)
def post(self, story_id): try: current_app.logger.info("Received FileResource POST Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) upload_headers = {'Authorization': 'Bearer {}'.format(APP_SERVER_TOKEN)} uploaded_file = request.files['file'].read() filename = request.form.get('filename') shared_server_upload = requests.post(SHARED_SERVER_FILE_UPLOAD_PATH, files={'file': (filename, uploaded_file)}, headers=upload_headers) current_app.logger.debug("Shared Server Response: %s - %s", shared_server_upload.status_code, shared_server_upload.text) file_data = json.loads(shared_server_upload.text) if shared_server_upload.ok: story_updated = Story.update_file(story_id, file_data['file']['resource']) current_app.logger.debug("Python Server Response: %s - %s", shared_server_upload.status_code, story_updated) return make_response(jsonify(story_updated), 200) return make_response(shared_server_upload.text, shared_server_upload.status_code) except ValueError as ex: error = "Unable to handle FileResource POST Request" + ex current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error)
def post(self): try: current_app.logger.info( "Received FacebookLoginResource POST Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) user_data = json.loads(request.data) payload = { "username": user_data["username"], "password": user_data["username"], "applicationOwner": SHARED_SERVER_APPLICATION_OWNER } headers = { 'content-type': 'application/json', 'Authorization': 'Bearer {}'.format(APP_SERVER_TOKEN) } user = User.get_facebook_user(user_data["username"]) if user["user"] is None: signup_response = requests.post(SHARED_SERVER_USER_PATH, data=json.dumps(payload), headers=headers) current_app.logger.debug( "Shared Server Signup Response: %s - %s", signup_response.status_code, signup_response.text) if not signup_response.ok and signup_response.status_code != 409: current_app.logger.debug("Python Server Response: %s - %s", signup_response.status_code, signup_response.text) return make_response(signup_response.text, signup_response.status_code) else: profile_pic_url = user_data["profile_pic"] profile_pic_bytes = base64.b64encode( requests.get(profile_pic_url).content) profile_pic_string = profile_pic_bytes.decode('utf-8') user = User.create(user_data["username"], user_data["email"], user_data["name"], profile_pic_string, user_data["firebase_token"]) current_app.logger.debug( "User created with firebase_token = %s", user_data["firebase_token"]) payload.pop("applicationOwner") login_response = requests.post(SHARED_SERVER_TOKEN_PATH, data=json.dumps(payload), headers=headers) current_app.logger.debug("Shared Server Response: %s - %s", login_response.status_code, login_response.text) json_response = json.loads(login_response.text) if login_response.ok: built_response = { "user": user["user"], "token": { "expiresAt": json_response["token"]["expiresAt"], "token": json_response["token"]["token"] } } current_app.logger.debug("Python Server Response: %s - %s", login_response.status_code, built_response) else: built_response = { "error": { "code": json_response["code"], "message": json_response["message"] } } current_app.logger.error("Python Server Response: %s - %s", login_response.status_code, built_response) return make_response(jsonify(built_response), login_response.status_code) except ValueError: error = "Unable to handle FacebookLoginResource POST Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error)
def post(self): try: current_app.logger.info("Received UsersResource POST Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) user_data = json.loads(request.data) payload = { "username": user_data["username"], "password": user_data["password"], "applicationOwner": SHARED_SERVER_APPLICATION_OWNER } headers = { 'content-type': 'application/json', 'Authorization': 'Bearer {}'.format(APP_SERVER_TOKEN) } signup_response = requests.post(SHARED_SERVER_USER_PATH, data=json.dumps(payload), headers=headers) current_app.logger.debug("Shared Server Signup Response: %s - %s", signup_response.status_code, signup_response.text) if signup_response.ok: user_created = User.create(user_data["username"], user_data["email"], user_data["name"], '', user_data["firebase_token"]) current_app.logger.debug( "User created with firebase_token = %s", user_data["firebase_token"]) payload.pop("applicationOwner") login_response = requests.post(SHARED_SERVER_TOKEN_PATH, data=json.dumps(payload), headers=headers) current_app.logger.debug("Shared Server Response: %s - %s", login_response.status_code, login_response.text) json_response = json.loads(login_response.text) if login_response.ok: built_response = { "user": user_created["user"], "token": { "expiresAt": json_response["token"]["expiresAt"], "token": json_response["token"]["token"] } } current_app.logger.debug("Python Server Response: %s - %s", login_response.status_code, built_response) else: built_response = { "error": { "code": json_response["code"], "message": json_response["message"] } } current_app.logger.error("Python Server Response: %s - %s", login_response.status_code, built_response) return make_response(jsonify(built_response), login_response.status_code) current_app.logger.debug("Python Server Response: %s - %s", signup_response.status_code, signup_response.text) return make_response(signup_response.text, signup_response.status_code) except ValueError: error = "Unable to handle UsersResource POST Request" current_app.logger.error("Python Server Response: 500 - %s", error) return ErrorHandler.create_error_response(500, error)
def get(self): current_app.logger.info("Received PingResource GET Request") StatManager.create(request.environ["PATH_INFO"] + " " + request.environ["REQUEST_METHOD"]) current_app.logger.debug("Python Server Response: 200 - %s", SUCCESS_MESSAGE) return Response(SUCCESS_MESSAGE, 200)