From 24d5200bdf9bb575c6d9e9dde193c853844f3713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 6 Dec 2020 03:49:30 +0100 Subject: [PATCH] try better --- src/resources.py | 154 ++++++++++++++++++++++------------------------- 1 file changed, 73 insertions(+), 81 deletions(-) diff --git a/src/resources.py b/src/resources.py index 4890fee..37aba73 100644 --- a/src/resources.py +++ b/src/resources.py @@ -48,16 +48,17 @@ class SignupApi(Resource): abort(417, INVALID_JSON_SCHEMA_MSG) user = VideonUser(name=userobj['name'], password=userobj['password']) - try: - user.hash_password() - db.session.add(user) - except Exception as e: - current_app.logger.warning(e) - abort(409, "user already exists") + user.hash_password() + db.session.add(user) expires = datetime.timedelta(days=7) access_token = create_access_token(identity=str(user.name), expires_delta=expires) - db.session.commit() + + try: + db.session.commit() + except Exception as e: + current_app.logger.warning(e) + abort(409, "user already exists") return {'token': access_token}, 200 @@ -86,12 +87,7 @@ class LoginApi(Resource): current_app.logger.info(e) abort(401, "unauthorized") - try: - user.last_logon = datetime.datetime.now() - db.session.add(user) - except Exception as e: - current_app.logger.exception(e) - abort(503, DB_ERROR_MSG) + user.last_logon = datetime.datetime.now() expires = datetime.timedelta(days=7) access_token = create_access_token(identity=str(user.name), expires_delta=expires) @@ -106,6 +102,7 @@ class MeApi(Resource): usermetadataschema = UserMetadataSchema(many=False) + @jwt_required def get(self): username = get_jwt_identity() # username = "jozska" @@ -148,13 +145,13 @@ class UserParameterApi(Resource): """ user = VideonUser.query.filter_by(name=username).first_or_404() - try: - db.session.delete(user) - except Exception as e: - current_app.logger.exception(e) - abort(503, DB_ERROR_MSG) + db.session.delete(user) - db.session.commit() + try: + db.session.commit() + except Exception as e: + current_app.logger.warning(e) + abort(410, DB_ERROR_MSG) return self.usermetadataschema.dump(user), 200 @jwt_required @@ -176,14 +173,14 @@ class UserParameterApi(Resource): current_app.logger.warning(e) abort(417, INVALID_JSON_SCHEMA_MSG) + user.password = userobj['password'] + user.hash_password() + try: - user.password = userobj['password'] - user.hash_password() + db.session.commit() except Exception as e: current_app.logger.exception(e) - abort(503, DB_ERROR_MSG) - - db.session.commit() + abort(412, DB_ERROR_MSG) return self.usermetadataschema.dump(user), 200 @@ -219,6 +216,7 @@ class CreateIngestResource(Resource): ingest.stream_key = str(uuid4()) try: db.session.add(ingest) + db.session.commit() Kubectl(name=ingest.id, resourcetype="ingest", stream_key=ingest.stream_key).create_resource() except Exception as e: current_app.logger.warning(e) @@ -229,13 +227,13 @@ class CreateIngestResource(Resource): neighbourobj = StreamResource.query.filter_by(id=neighbour).first_or_404() if neighbourobj.resource_type == StreamResourceTypeEnum.INGEST: abort(400, "ingest cannot be a downstream component") - try: - neighbourobj.parent_id = ingest.id - except Exception as e: - current_app.logger.warning(e) - abort(503, "could not set neighbourhood") + neighbourobj.parent_id = ingest.id - db.session.commit() + try: + db.session.commit() + except Exception as e: + current_app.logger.exception(e) + abort(412, DB_ERROR_MSG) return self.streamresourceschema.dump(ingest), 200 @@ -273,20 +271,21 @@ class CreateRestreamResource(Resource): restream.parent_id = restreamobj['inputNeighbour'] try: db.session.add(restream) + db.session.commit() Kubectl(name=restream.id, resourcetype="restream", stream_key=restream.stream_key, push_urls=restreamobj['outputURLs']).create_resource() except Exception as e: current_app.logger.warning(e) - abort(503, "object already exists") + abort(409, "object already exists") for url in restreamobj['outputURLs']: - try: - outputurl = OutputUrls(output_url=url, streamresource_id=restream.id) - db.session.add(outputurl) - except Exception as e: - current_app.logger.warning(e) - abort(503, "could not add all outputurls") + outputurl = OutputUrls(output_url=url, streamresource_id=restream.id) + db.session.add(outputurl) - db.session.commit() + try: + db.session.commit() + except Exception as e: + current_app.logger.exception(e) + abort(412, DB_ERROR_MSG) return self.streamresourceschema.dump(restream), 200 @@ -334,23 +333,24 @@ class CreateEncodeResource(Resource): try: db.session.add(encoder) + db.session.commit() Kubectl(name=encoder.id, resourcetype="encoder", stream_key=encoder.stream_key).create_resource() except Exception as e: current_app.logger.warning(e) - abort(503, "object already exists") + abort(409, "object already exists") for neighbour in encoderobj['outputNeighbours']: neighbour = StreamResource.query.filter_by(id=neighbour).first_or_404() if neighbour.resource_type == StreamResourceTypeEnum.INGEST: abort(400, "ingest cannot be a downstream component") - try: - neighbour.parent_id = encoder.id - except Exception as e: - current_app.logger.warning(e) - abort(503, "could not add all neighbours") + neighbour.parent_id = encoder.id - db.session.commit() + try: + db.session.commit() + except Exception as e: + current_app.logger.exception(e) + abort(412, DB_ERROR_MSG) return self.streamresourceschema.dump(encoder), 200 @@ -423,12 +423,12 @@ class ManipulateStreamResource(Resource): abort(422, "invalid arguments") try: db.session.delete(streamreousrce) + db.session.commit() Kubectl(name=streamreousrce.id, resourcetype=streamreousrce.resource_type).delete_resource() except Exception as e: current_app.logger.exception(e) - abort(503, DB_ERROR_MSG) + abort(410, DB_ERROR_MSG) - db.session.commit() return self.streamresourceschema.dump(streamreousrce), 200 @@ -471,33 +471,25 @@ class ModifyIngressResource(Resource): for removeneighbours in listdiffer.elementsinfirstlistbutnotinsecond(currentoutputneighboursids, ingestobj['outputNeighbours']): neighbourobj = StreamResource.query.filter_by(id=removeneighbours).first_or_404() - try: - neighbourobj.parent_id = None - except Exception as e: - current_app.logger.warning(e) - abort(503, "could not remove neighbourhood") + neighbourobj.parent_id = None for addneighbours in listdiffer.elementsinsecondlistbutnotinfirst(currentoutputneighboursids, ingestobj['outputNeighbours']): neighbourobj = StreamResource.query.filter_by(id=addneighbours).first_or_404() if neighbourobj.resource_type == StreamResourceTypeEnum.INGEST: abort(400, "ingest cannot be a downstream component") - try: - neighbourobj.parent_id = ingest.id - except Exception as e: - current_app.logger.warning(e) - abort(503, "could not remove neighbourhood") + neighbourobj.parent_id = ingest.id else: Kubectl(name=ingest.id, resourcetype="ingest", stream_key=ingest.stream_key).update_resource() if currentoutputneighbours: for currentneighbour in currentoutputneighbours: - try: - currentneighbour.parent_id = None - except Exception as e: - current_app.logger.warning(e) - abort(503, "could not remove neighbourhood") + currentneighbour.parent_id = None - db.session.commit() + try: + db.session.commit() + except Exception as e: + current_app.logger.exception(e) + abort(412, DB_ERROR_MSG) return self.streamresourceschema.dump(ingest), 200 @@ -547,25 +539,21 @@ class ModifyRestreamResource(Resource): restreamobj['outputURLs']): elementtodelete = OutputUrls.query.filter_by(output_url=deleteoutputurl, streamresource_id=restream.id).first_or_404() - try: - db.session.delete(elementtodelete) - except Exception as e: - current_app.logger.warning(e) - abort(503, "could not delete url") + db.session.delete(elementtodelete) for addoutputurl in listdiffer.elementsinsecondlistbutnotinfirst(currentoutputurlurls, restreamobj['outputURLs']): elementtoadd = OutputUrls(output_url=addoutputurl, streamresource_id=restream.id) - try: - db.session.add(elementtoadd) - except Exception as e: - current_app.logger.warning(e) - abort(503, "object already exists") + db.session.add(elementtoadd) if currentoutputurlurls != restreamobj['outputURLs']: Kubectl(name=restream.id, resourcetype="restream", stream_key=restream.stream_key, push_urls=restreamobj['outputURLs']).update_resource() - db.session.commit() + try: + db.session.commit() + except Exception as e: + current_app.logger.exception(e) + abort(412, DB_ERROR_MSG) return self.streamresourceschema.dump(restream), 200 @@ -633,11 +621,7 @@ class ModifyEncodeResource(Resource): else: if currentoutputneighbours: for currentneighbour in currentoutputneighbours: - try: - currentneighbour.parent_id = None - except Exception as e: - current_app.logger.warning(e) - abort(503, "could not remove neighbourhood") + currentneighbour.parent_id = None childurls = [] for child in encode.children: @@ -646,7 +630,11 @@ class ModifyEncodeResource(Resource): Kubectl(name=encode.id, resourcetype="encoder", stream_key=encode.stream_key, encode_push_urls=str(childurls)).update_resource() - db.session.commit() + try: + db.session.commit() + except Exception as e: + current_app.logger.exception(e) + abort(412, DB_ERROR_MSG) return self.streamresourceschema.dump(encode), 200 @@ -682,5 +670,9 @@ class CoordModifyResource(Resource): resource.x = coordobj['x'] resource.y = coordobj['y'] - db.session.commit() + try: + db.session.commit() + except Exception as e: + current_app.logger.exception(e) + abort(412, DB_ERROR_MSG) return self.streamresourceschema.dump(resource), 200