101 lines
2.7 KiB
Python
101 lines
2.7 KiB
Python
from paramecio2.libraries.mtemplates import env_theme, PTemplate
|
|
from paramecio2.libraries.urls import make_url
|
|
from paramecio2.libraries.db import corefields
|
|
from paramecio2.libraries.db.extrafields.datetimefield import DateTimeField
|
|
from flask import request
|
|
from settings import config
|
|
#from paramecio2.wsgiapp import app
|
|
from modules.apidoc2 import apidoc_app
|
|
from flask import abort
|
|
|
|
import os
|
|
try:
|
|
import ujson as json
|
|
except:
|
|
import json
|
|
|
|
import inspect
|
|
import re
|
|
from typing import Annotated
|
|
from modules.apidoc2.libraries.apidoc import AppDoc
|
|
from importlib import import_module
|
|
|
|
env=env_theme(__file__)
|
|
t=PTemplate(env)
|
|
|
|
import json
|
|
|
|
class Object:
|
|
def toJSON(self):
|
|
return json.dumps(
|
|
self,
|
|
default=lambda o: o.__dict__,
|
|
sort_keys=True,
|
|
indent=4)
|
|
|
|
class General(Object):
|
|
|
|
name: int = None
|
|
|
|
gen=General()
|
|
|
|
class Item:
|
|
|
|
name=corefields.CharField('name', '')
|
|
item_id=corefields.IntegerField('item_id', '')
|
|
date_added=DateTimeField('date_added', '')
|
|
|
|
@apidoc_app.route('/apidoc/<module>')
|
|
def apidocs(module):
|
|
|
|
if os.path.isfile('modules/{}/__apidoc__.py'.format(module)):
|
|
|
|
return t.load_template('api.phtml', module=module)
|
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
@apidoc_app.route('/apidoc/<module>/api.yaml')
|
|
def json_spec(module):
|
|
|
|
module_api='modules/{}/__apidoc__.py'.format(module)
|
|
|
|
if os.path.isfile(module_api):
|
|
|
|
module_api=module_api.replace('/', '.').replace('.py', '')
|
|
|
|
appdoc=import_module(module_api)
|
|
|
|
api_json=t.load_template('openapi.yaml', appdoc=appdoc.appdoc, version="1.0", title="Apidoc specification for paramecio2", terms_of_service='', email='antonio.delarosa@salirdelhoyo.com',license={'name': 'AGPLV3', 'url': 'https://www.gnu.org/licenses/agpl-3.0.html'})
|
|
|
|
#return json.loads(api_json)
|
|
return api_json
|
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
# Api test
|
|
# FullArgSpec(args=['item_id'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'item_id': <class 'int'>}
|
|
|
|
@apidoc_app.route('/apidoc/v1/item/<int:item_id>')
|
|
def item(item_id: Annotated[int, 'Id of the item in database'], tag='apidoc') -> Item:
|
|
"""
|
|
A method for get item using id from database
|
|
|
|
A method for get item using id from database. You can access to one object.
|
|
"""
|
|
|
|
return { "date_added": "2024-06-01 12:24:11", "item_id": item_id, "name": "Tree" }
|
|
|
|
@apidoc_app.post('/apidoc/v1/item')
|
|
def item_post(tag='apidoc', post: Annotated[Item, 'Insert a item in db'] = Item):
|
|
|
|
error=0
|
|
message=''
|
|
code_error=0
|
|
|
|
return {'error': error, 'message': message, 'code_error': code_error}
|
|
|
|
|