def put(self, server_id): """ Update the information of a server. The server is identified by its ``server_id``. The data which may be updated is the ``server_name``, the ``server_address``, and the ``server_port`` """ DB.session.begin() try: server = DB.session.query(ServerModel).filter_by( server_id=server_id, user_id=AUTHENTICATOR.get_user_id()).one() except exc.NoResultFound: return "", 404 payload = NAMESPACE.apis[0].payload if "server_name" in payload: server.server_name = payload["server_name"] if "server_address" in payload: server.server_address = payload["server_address"] if "server_port" in payload: server.server_port = payload["server_port"] user_server = ServerSchema().dump(server).data DB.session.commit() DB.session.close() # serializing as JSON for return return user_server
def post(self, server_id): """ Apply a preset (stored in a preset) whose contents are to be applied to a server. """ try: server_object = DB.session.query( ServerModel).filter_by( server_id=server_id, user_id=AUTHENTICATOR.get_user_id() ).one() except exc.NoResultFound: return "", 404 # transforming into JSON-serializable objects server = ServerSchema().dump(server_object).data server_url = server["server_address"] + ":" + server["server_port"] preset_id = NAMESPACE.apis[0].payload["preset_id"] preset_object = Preset().get(server_id, preset_id) preset = ast.literal_eval(preset_object["preset_state"]) for device in preset: for activator in device["activators"]: query = (server_url + f"/devices/{device['deviceId']}" + f"/activators/{activator['activatorId']}") payload = {"state": activator["state"]} route_request("POST", query, payload) # transforming into JSON-serializable objects return "Batch request successful"
def post(self): """ List of users. """ user_id = {'user_id': AUTHENTICATOR.get_user_id()} schema = UserSchema().load(user_id) new_user = UserModel(**schema.data) DB.session.begin() DB.session.add(new_user) DB.session.commit() return UserSchema().dump(new_user).data
def get(self): """ Get the list of servers. A server is associated with a user denoted by an authentication JWT. """ servers = DB.session.query( ServerModel).filter_by(user_id=AUTHENTICATOR.get_user_id()) # transforming into JSON-serializable objects schema = ServerSchema(many=True) all_servers = schema.dump(servers).data return all_servers
def get(self, server_id): """ List of presets. Returns a list of all presets associated with a server. If the server is not associated with the user it returns an exception. """ try: DB.session.query(ServerModel).filter_by( server_id=server_id, user_id=AUTHENTICATOR.get_user_id()).one() except exc.NoResultFound: return "", 404 presets = DB.session.query(PresetModel).filter_by(server_id=server_id,) schema = PresetSchema(many=True) all_presets = schema.dump(presets).data return all_presets
def delete(self): """ Delete a user. A user is identified by its ``user_id`` from the authentication JWT. """ try: user = DB.session.query(UserModel).filter_by( user_id=AUTHENTICATOR.get_user_id()).one() except exc.NoResultFound: return "", 404 DB.session.begin() DB.session.delete(user) DB.session.commit() return "", 204
def delete(self, server_id): """ Delete a server. The server is identified by its ``server_id`` """ try: server = DB.session.query(ServerModel).filter_by( server_id=server_id, user_id=AUTHENTICATOR.get_user_id()).one() except exc.NoResultFound: return "", 404 DB.session.begin() DB.session.delete(server) DB.session.commit() return "", 204
def get(self, server_id, preset_id): """ Return a specific preset with ``preset_id`` associated with a server with ``server_id`` """ try: DB.session.query(ServerModel).filter_by( server_id=server_id, user_id=AUTHENTICATOR.get_user_id()).one() except exc.NoResultFound: return "", 404 try: preset = DB.session.query(PresetModel).filter_by( server_id=server_id, preset_id=preset_id ).one() except exc.NoResultFound: return "", 404 return PresetSchema().dump(preset).data
def post(self, server_id): """ Post a ping request to a server. """ try: server_object = DB.session.query( ServerModel).filter_by( server_id=server_id, user_id=AUTHENTICATOR.get_user_id() ).one() except exc.NoResultFound: return "", 404 # transforming into JSON-serializable objects server = ServerSchema().dump(server_object).data server_query = server['server_address'] + ':' + server['server_port'] return ping(server_query)
def get(self, server_id): """ Get a server. The server is identified by its ``server_id`` """ # Attempt to retrieve the SINGLE entry in the database with the server_id given. # Error 404 if >1 or D.N.E try: server_object = DB.session.query( ServerModel).filter_by( server_id=server_id, user_id=AUTHENTICATOR.get_user_id() ).one() except exc.NoResultFound: return "", 404 # transforming into JSON-serializable objects server = ServerSchema().dump(server_object).data return server
def delete(self, server_id, preset_id): """ Delete a specific preset with ``preset_id`` associated with a server with ``server_id`` """ try: DB.session.query(ServerModel).filter_by( server_id=server_id, user_id=AUTHENTICATOR.get_user_id()).one() except exc.NoResultFound: return "", 404 try: preset = DB.session.query(PresetModel).filter_by( server_id=server_id, preset_id=preset_id ).one() except exc.NoResultFound: return "", 404 DB.session.begin() DB.session.delete(preset) DB.session.commit() return "", 204
def post(self, server_id): """ Forward a request to a server. """ try: server_object = DB.session.query( ServerModel).filter_by( server_id=server_id, user_id=AUTHENTICATOR.get_user_id() ).one() except exc.NoResultFound: return "", 404 # transforming into JSON-serializable objects server = ServerSchema().dump(server_object).data request_type = NAMESPACE.apis[0].payload["requestType"] endpoint = NAMESPACE.apis[0].payload["endpoint"] optional_payload = NAMESPACE.apis[0].payload["optionalPayload"] server_query = server['server_address'] + \ ':' + server['server_port'] + endpoint return route_request(request_type, server_query, optional_payload)
def post(self, server_id): """ Post a preset to a server. """ payload = NAMESPACE.apis[0].payload try: server_object = DB.session.query(ServerModel).filter_by( server_id=server_id, user_id=AUTHENTICATOR.get_user_id()).one() except exc.NoResultFound: return "", 404 # transforming into JSON-serializable objects server = ServerSchema().dump(server_object).data payload["server_id"] = server_id server_address = server['server_address'] + \ ':' + server['server_port'] + '/devices/' # Get the current state of the server. payload["preset_state"] = str( route_request("GET", server_address, "").json) posted_preset = PresetSchema(only=( 'preset_name', 'preset_state', 'server_id', )).load(payload) preset = PresetModel(**posted_preset.data) DB.session.begin() DB.session.add(preset) DB.session.commit() return PresetSchema().dump(preset).data
def post(self): """ Add a server to the list of servers. A server is associated with a user denoted by an authentication JWT. """ payload = NAMESPACE.apis[0].payload print(payload) payload['user_id'] = AUTHENTICATOR.get_user_id() posted_server = ServerSchema( only=('user_id', 'server_name', 'server_address', 'server_port')).load(payload) server = ServerModel(**posted_server.data) # persist server DB.session.begin() DB.session.add(server) DB.session.commit() # return created server return ServerSchema().dump(server).data
def flask_app_authenticated_client_in_DB_with_server(flask_app_server_in_db): """ Create an authenticated flask client with that user and server in the DB. """ AUTHENTICATOR.set_user_id("1") return flask_app_server_in_db.test_client()
def flask_app_authenticated_client(flask_app): """ Create an authentaicated flask client to test HTTP responses. """ AUTHENTICATOR.set_user_id("1") return flask_app.test_client()