Commit 1dd3f4ed authored by deepdey's avatar deepdey

feat: add web-api/update endpoint

parent 6a0c6b3c
...@@ -25,7 +25,7 @@ ns = api.namespace( ...@@ -25,7 +25,7 @@ ns = api.namespace(
@ns.route('/add', methods=['POST']) @ns.route('/add', methods=['POST'])
class Add(Resource): class AddWebApi(Resource):
@ns.doc(security=SECURITY) @ns.doc(security=SECURITY)
@ns.expect(*add_web_api_payload()) @ns.expect(*add_web_api_payload())
...@@ -50,6 +50,37 @@ class Add(Resource): ...@@ -50,6 +50,37 @@ class Add(Resource):
error=e.normalized_messages() error=e.normalized_messages()
) )
except Exception as e:
logger.error(traceback.format_exc())
return prepare_api_response(EXCEPTION_CODE, str(e))
@ns.route('/update', methods=['PATCH'])
class UpdateWebApi(Resource):
@ns.doc(security=SECURITY)
@ns.expect(*update_web_api_payload())
@ns.response(*update_web_api_response())
@sanitized()
def patch(self):
try:
validator = UpdateWebApiValidator()
payload: dict = validator.load(request.get_json())
updated: bool = update_web_api(payload)
if not updated:
return prepare_api_response(EXCEPTION_CODE, UPDATE_ERROR_MESSAGE)
return prepare_api_response(SUCCESS_CODE, UPDATE_SUCCESS_MESSAGE)
except ValidationError as e:
return prepare_api_response(
VALIDATION_ERROR_CODE,
VALIDATION_ERROR_MESSAGE,
error=e.normalized_messages(),
)
except Exception as e: except Exception as e:
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())
return prepare_api_response(EXCEPTION_CODE, str(e)) return prepare_api_response(EXCEPTION_CODE, str(e))
\ No newline at end of file
...@@ -22,8 +22,39 @@ def add_web_api(web_api_info: dict) -> int | None: ...@@ -22,8 +22,39 @@ def add_web_api(web_api_info: dict) -> int | None:
db.session.add(web_api_model) db.session.add(web_api_model)
db.session.commit() db.session.commit()
# TODO: add new api entry to redis
return web_api_model.id return web_api_model.id
except Exception as e:
db.session.rollback()
raise e
def update_web_api(web_api_info: dict) -> bool:
try:
existing_web_api_info: ORM.Api | None =\
ORM.Api.query.filter_by(id=web_api_info.get('id')).first()
if existing_web_api_info is None:
return False
old_endpoint = existing_web_api_info.get('endpoint')
new_endpoint = web_api_info.get('endpoint')
# TODO: delete old one from redis and add new one
if existing_web_api_info.tag != web_api_info.get('tag'):
# TODO: refresh redis api role mapping as a daemon job
pass
existing_web_api_info.endpoint = new_endpoint
existing_web_api_info.method = web_api_info.get('method')
existing_web_api_info.tag = web_api_info.get('tag')
db.session.commit()
return True
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
raise e raise e
\ No newline at end of file
...@@ -17,4 +17,23 @@ def add_web_api_payload() -> list: ...@@ -17,4 +17,23 @@ def add_web_api_payload() -> list:
}, },
) )
return [payload]
def update_web_api_payload() -> list:
payload = api.model(
'updateWebApi Payload',
{
'id': fields.Integer(required=True),
'endpoint': fields.String(required=True),
'method': fields.String(required=True, enum=ENDPOINT_METHODS),
'tag': fields.String(
required=True,
enum=ENDPOINT_TAGS,
description='`P` - No need to login | `V` - Need to login but no role check | `SU` Only accessable by *SUPER USERS* | `PA` - Only accesable by *PROJECT ADMIN and above* | `RA` - Only accesable by *PREVILAGED USERS and above*'
),
},
)
return [payload] return [payload]
\ No newline at end of file
...@@ -8,5 +8,14 @@ def add_web_api_response() -> any: ...@@ -8,5 +8,14 @@ def add_web_api_response() -> any:
return prepare_swagger_response( return prepare_swagger_response(
code=SUCCESS_CODE, code=SUCCESS_CODE,
message=INSERT_SUCCESS_MESSAGE, message=INSERT_SUCCESS_MESSAGE,
model_name='addUser Response' model_name='addWebApi Response'
)
def update_web_api_response() -> any:
return prepare_swagger_response(
code=SUCCESS_CODE,
message=UPDATE_SUCCESS_MESSAGE,
model_name='updateWebApi Response'
) )
\ No newline at end of file
from marshmallow import Schema, fields, validate from marshmallow import Schema, fields, validate
from app.app_config.constant import ENDPOINT_METHODS, ENDPOINT_TAGS from app.app_config.constant import ENDPOINT_METHODS, ENDPOINT_TAGS
...@@ -13,3 +12,16 @@ class AddWebApiValidator(Schema): ...@@ -13,3 +12,16 @@ class AddWebApiValidator(Schema):
required=True, required=True,
validate=validate.OneOf(ENDPOINT_TAGS), validate=validate.OneOf(ENDPOINT_TAGS),
) )
class UpdateWebApiValidator(Schema):
id = fields.Integer(required=True)
endpoint = fields.String(required=True)
method = fields.String(
required=True,
validate=validate.OneOf(ENDPOINT_METHODS)
)
tag = fields.String(
required=True,
validate=validate.OneOf(ENDPOINT_TAGS),
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment