Commit e556a6e6 authored by deepdey's avatar deepdey

feat: add `web-api/add` endpoint

parent c491f760
ALPHANUMARIC_STRING: str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' ALPHANUMARIC_STRING: str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
ENDPOINT_TAGS: list = ['P', 'V', 'SU', 'PV', 'RA']
ENDPOINT_METHODS: list = ['GET', 'POST', 'PATCH','PUT', 'DELETE']
\ No newline at end of file
import os
import traceback
from flask import request
from flask_restx import Resource
from marshmallow import ValidationError
from app.app_config.config import get_logger
from app.app_config.response_code import *
from app.app_config.response_message import *
from app.app_config.swagger_config import SECURITY
from app.decorators.sanitizer import sanitized
from app.routes import api
from app.routes.web_api.web_api_validator import *
from app.routes.web_api.web_api_payload import *
from app.routes.web_api.web_api_response import *
from app.routes.web_api.web_api_dao_service import *
from app.utils.api_response import prepare_api_response
logger = get_logger(__name__)
ns = api.namespace(
name=os.path.dirname(__file__).split(os.sep)[-1].replace('_', '-'),
description='API connected to web-api module',
ordered=False
)
@ns.route('/add', methods=['POST'])
class Add(Resource):
@ns.doc(security=SECURITY)
@ns.expect(*add_web_api_payload())
@ns.response(*add_web_api_response())
@sanitized()
def post(self):
try:
validator = AddWebApiValidator()
payload: dict = validator.load(request.get_json())
web_api_id: int = add_web_api(payload)
return prepare_api_response(
SUCCESS_CODE,
INSERT_SUCCESS_MESSAGE
)
except ValidationError as e:
return prepare_api_response(
VALIDATION_ERROR_CODE,
VALIDATION_ERROR_MESSAGE,
error=e.normalized_messages()
)
except Exception as e:
logger.error(traceback.format_exc())
return prepare_api_response(EXCEPTION_CODE, str(e))
\ No newline at end of file
from app import orm as ORM
from app import db
def get_web_api_by_endpoint(endpoint: str) -> ORM.Api | None:
return ORM.Api.query.filter_by(endpoint=endpoint).first()
def add_web_api(web_api_info: dict) -> int | None:
try:
existing_web_api: ORM.Api | None =\
get_web_api_by_endpoint(web_api_info.get('endpoint'))
if existing_web_api:
raise Exception('endpoint already exists')
web_api_model = ORM.Api(
endpoint=web_api_info.get('endpoint'),
method=web_api_info.get('method'),
tag=web_api_info.get('tag')
)
db.session.add(web_api_model)
db.session.commit()
return web_api_model.id
except Exception as e:
db.session.rollback()
raise e
\ No newline at end of file
from flask_restx import fields
from app.app_config.constant import ENDPOINT_METHODS, ENDPOINT_TAGS
from app.routes import api
def add_web_api_payload() -> list:
payload = api.model(
'addWebApi Payload',
{
'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]
\ No newline at end of file
from app.app_config.response_code import *
from app.app_config.response_message import *
from app.utils.api_response import prepare_swagger_response
def add_web_api_response() -> any:
return prepare_swagger_response(
code=SUCCESS_CODE,
message=INSERT_SUCCESS_MESSAGE,
model_name='addUser Response'
)
\ No newline at end of file
from marshmallow import Schema, fields, validate
from app.app_config.constant import ENDPOINT_METHODS, ENDPOINT_TAGS
class AddWebApiValidator(Schema):
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