2019-07-29 10:01:11 +08:00
|
|
|
import os
|
|
|
|
import re
|
2019-11-27 13:50:55 +08:00
|
|
|
import traceback
|
2019-07-29 10:01:11 +08:00
|
|
|
|
2022-06-22 17:10:20 +08:00
|
|
|
from aiohttp.web_response import Response
|
2019-07-29 10:01:11 +08:00
|
|
|
from aiohttp.web_exceptions import (
|
|
|
|
HTTPException,
|
|
|
|
HTTPExpectationFailed,
|
|
|
|
HTTPForbidden,
|
|
|
|
HTTPMethodNotAllowed,
|
|
|
|
HTTPNotFound,
|
|
|
|
)
|
|
|
|
from aiohttp import web
|
|
|
|
from aiohttp.web_request import Request
|
|
|
|
from aiohttp.web import json_response
|
|
|
|
|
|
|
|
|
2021-12-13 12:52:28 +08:00
|
|
|
from sqlor.dbpools import DBPools
|
2019-07-29 10:01:11 +08:00
|
|
|
|
|
|
|
from appPublic.dictObject import multiDict2Dict
|
|
|
|
from appPublic.jsonConfig import getConfig
|
|
|
|
|
|
|
|
from .error import Error,Success
|
|
|
|
|
|
|
|
DEFAULT_METHODS = ('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE')
|
|
|
|
|
|
|
|
class RestEndpoint:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.methods = {}
|
|
|
|
|
|
|
|
for method_name in DEFAULT_METHODS:
|
|
|
|
method = getattr(self, method_name.lower(), None)
|
|
|
|
if method:
|
|
|
|
self.register_method(method_name, method)
|
|
|
|
|
|
|
|
def register_method(self, method_name, method):
|
|
|
|
self.methods[method_name.upper()] = method
|
|
|
|
|
|
|
|
async def dispatch(self):
|
2021-12-13 12:52:28 +08:00
|
|
|
method = self.methods.get(self.request.method.lower())
|
2019-07-29 10:01:11 +08:00
|
|
|
if not method:
|
|
|
|
raise HTTPMethodNotAllowed('', DEFAULT_METHODS)
|
|
|
|
|
|
|
|
return await method()
|
|
|
|
|
|
|
|
|
|
|
|
class DBCrud(RestEndpoint):
|
|
|
|
def __init__(self, request,dbname,tablename, id=None):
|
|
|
|
super().__init__()
|
|
|
|
self.dbname = dbname
|
|
|
|
self.tablename = tablename
|
|
|
|
self.request = request
|
2021-12-13 12:52:28 +08:00
|
|
|
self.db = DBPools()
|
2019-07-29 10:01:11 +08:00
|
|
|
self.id = id
|
|
|
|
|
|
|
|
async def options(self) -> Response:
|
|
|
|
try:
|
2021-12-13 12:52:28 +08:00
|
|
|
with self.db.sqlorContext(self.dbname) as sor:
|
|
|
|
d = await sor.I(self.tablename)
|
|
|
|
return json_response(Success(d))
|
2019-07-29 10:01:11 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2019-11-27 13:50:55 +08:00
|
|
|
traceback.print_exc()
|
2019-07-29 10:01:11 +08:00
|
|
|
return json_response(Error(errno='metaerror',msg='get metadata error'))
|
|
|
|
|
|
|
|
async def get(self) -> Response:
|
|
|
|
"""
|
|
|
|
query data
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
ns = multiDict2Dict(self.request.query)
|
2021-12-13 12:52:28 +08:00
|
|
|
with self.db.sqlorContext(self.dbname) as sor:
|
|
|
|
d = await sor.R(self.tablename, ns)
|
|
|
|
return json_response(Success(d))
|
2019-07-29 10:01:11 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2019-11-27 13:50:55 +08:00
|
|
|
traceback.print_exc()
|
2019-07-29 10:01:11 +08:00
|
|
|
return json_response(Error(errno='search error',msg='search error'))
|
|
|
|
|
|
|
|
async def post(self):
|
|
|
|
"""
|
|
|
|
insert data
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
ns = multiDict2Dict(await self.request.post())
|
2021-12-13 12:52:28 +08:00
|
|
|
with self.db.sqlorContext(self.dbname) as sor:
|
|
|
|
d = await sor.C(self.tablename, ns)
|
|
|
|
return json_response(Success(d))
|
2019-07-29 10:01:11 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2019-11-27 13:50:55 +08:00
|
|
|
traceback.print_exc()
|
2019-07-29 10:01:11 +08:00
|
|
|
return json_response(Error(errno='add error',msg='add error'))
|
|
|
|
|
|
|
|
async def put(self):
|
|
|
|
"""
|
|
|
|
update data
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
ns = multiDict2Dict(await self.request.post())
|
2021-12-13 12:52:28 +08:00
|
|
|
with self.db.sqlorContext(self.dbname) as sor:
|
|
|
|
d = await sor.U(self.tablename, ns)
|
|
|
|
return json_response(Success(' '))
|
2019-07-29 10:01:11 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2019-11-27 13:50:55 +08:00
|
|
|
traceback.print_exc()
|
2019-07-29 10:01:11 +08:00
|
|
|
return json_response(Error(errno='update error',msg='update error'))
|
|
|
|
|
|
|
|
async def delete(self, request: Request, instance_id):
|
|
|
|
"""
|
|
|
|
delete data
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
ns = multiDict2Dict(self.request.query)
|
2021-12-13 12:52:28 +08:00
|
|
|
with self.db.sqlorContext(self.dbname) as sor:
|
|
|
|
d = await sor.D(self.tablename, ns)
|
|
|
|
return json_response(Success(d))
|
2019-07-29 10:01:11 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2019-11-27 13:50:55 +08:00
|
|
|
traceback.print_exc()
|
2019-07-29 10:01:11 +08:00
|
|
|
return json_response(Error(erron='delete error',msg='error'))
|