def send_data(): """ Sends user data Returns: Response: JSON String containing all the user data """ token = request.args.get("token") user = request.args.get("user") if not authenticate(user, token): return make_response(jsonify({"error": "Unauthorized API request"}), 403) print(user) document = my_app.find_one({"user": user}) # print(document) if document == None: print("does not have record for the user: "******"user": user, "run_id": "", "event_id": "", "waveform": None, "tags_data": [], "waveform_history": [], } my_app.insert_one(post) document = my_app.find_one({"user": user}) document["available_runs"] = available_runs json_str = dumps(document) return make_response(json_str, 200)
def get_waveform(): """ Retrieves a waveform from the cache Returns: str: A JSON formatted string representing the waveform """ if request.is_json: token = request.args.get("token") req = request.get_json() run_id = None user = None event_id = None try: run_id = req["run_id"] user = req["user"] event_id = req["event_id"] except: return make_response(jsonify({"error": "Bad Request"}), 400) if not authenticate(user, token): return make_response(jsonify({"error": "Unauthorized API request"}), 403) waveform = wait_for_waveform(run_id, event_id) if isinstance(waveform, str): return make_response(jsonify({"err_msg": waveform}), 202) print("Retrieved waveform from cache") # Update database in another thread threading.Thread( target=update_db_new_waveform, args=(user, run_id, event_id, waveform) ).start() print("returning waveform...") return json.dumps(waveform) else: return make_response(jsonify({"error": "Bad Request"}), 400)
def delete_waveform(): """ Deletes a waveform associated with a tag Returns: Response: A symbolic 200 response signifying success """ if request.is_json: req = request.get_json() print(req) user = None tag = None try: user = req["user"] tag = req["tag"] except: return make_response(jsonify({"error": "Bad Request"}), 400) token = request.args.get("token") if not authenticate(user, token): return make_response(jsonify({"error": "Unauthorized API request"}), 403) # Update database mongo_document = my_app.find_one( {"user": user, "tags_data." + tag: {"$exists": True}} ) if mongo_document: print("#DELETING mongo document found: ") my_app.update_one( {"user": user, "tags_data." + tag: {"$exists": True}}, {"$pull": {"tags_data": {tag: {"$exists": True}},}}, ) return make_response(jsonify({"success": True}), 200) else: return make_response(jsonify({"error": "Bad Request"}), 400)
def get_event_plot(): """ Connects the frontend to the Bokeh Server by pulling and returning a sesssion which generates Bokeh plots displaying all the events for a given run Returns: str: A script tag that embeds content from a specific existing session on a Bokeh server. """ if request.is_json: token = request.args.get("token") req = request.get_json() run_id = None user = None try: run_id = req["run_id"] user = req["user"] except: return make_response(jsonify({"error": "Bad Request"}), 400) if not authenticate(user, token): return make_response(jsonify({"error": "Unauthorized API request"}), 403) print("RUN ID IS: ", run_id) threading.Thread(target=cache_events_request, args=[run_id]).start() session_id = generate_session_id() my_sessions.insert_one({session_id: run_id}) script = server_session(url=BOKEH_SERVER_URL, session_id=session_id) # use the script in the rendered page return script else: return make_response(jsonify({"error": "Bad Request"}), 400)
def ToBox(client_id, client_secret, client_token): print('''\ ########################################################## ##### Welcome to the ToBox toolbox for HoltLab@CMU ##### ########################################################## ''') commands = set(['quit', 'help', 'cd', 'ls', 'upload', 'download']) print('Authenticating', end='...') client = app.authenticate(client_id, client_secret, client_token) try: print('Done! {}'.format(client.user().get())) except: print('Fail! Incorrect token(s)') return working_dir = client.folder(folder_id='0') while True: cmd = input('\n>>> ') try: (command, params) = app.parse(commands, cmd) if command == 'quit': print('Bye!') break elif command == 'help': print( 'Currently supported commands: {}.\nFor specific instruction, try: <command> --help' .format(commands)) else: working_dir = scheduler(client, working_dir, command, params) except Exception as e: print(e) except SystemExit: pass
def save_waveform(): """ Saves the tag and comments associated with a particular waveform Returns: Response: A symbolic 200 response signifying success """ if request.is_json: token = request.args.get("token") req = None user = None tag = None comments = None event_id = None run_id = None try: req = request.get_json() user = req["user"] tag = req["tag"] event_id = req["event_id"] run_id = req["run_id"] except: return make_response(jsonify({"error": "Bad Request"}), 400) if not authenticate(user, token): return make_response(jsonify({"error": "Unauthorized API request"}), 403) try: comments = req["comments"] # Optional except KeyError: comments = "" # Update database in another thread threading.Thread( target=update_db_new_tag, args=(user, run_id, event_id, tag, comments) ).start() return make_response(jsonify({"success": True}), 200) else: return make_response(jsonify({"error": "Bad Request"}), 400)
def test_authenticate(self): self.assertTrue(app.authenticate(os.getenv('TOKEN')))
def test_authenticate_pass(self, consumer_key, consumer_token): authentication = app.authenticate(consumer_key, consumer_token) assert 'access_token' in authentication
def test_authenticate_fail(self, consumer_key, consumer_secret): authentication = app.authenticate(consumer_key, consumer_secret) assert "access_token" not in authentication