-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottlerest.py
139 lines (114 loc) · 4.21 KB
/
bottlerest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from sqlalchemy.inspection import inspect
from sqlalchemy.orm import sessionmaker
import bottle
import json
class DBApi(object):
def __init__(self, db_class):
self.db_class = db_class
self.primary_key = inspect(db_class).primary_key[0]
self.columns = inspect(db_class).columns
def create(self, session, content_dict):
dbobj = self.db_class()
for key, value in content_dict.items():
setattr(dbobj, key, value)
session.add(dbobj)
session.flush()
pkey = getattr(dbobj, self.primary_key.name)
return {self.primary_key.name: pkey}
def _get_dbobj(self, session, pkey):
dbobj = session.query(self.db_class).filter(
self.primary_key == pkey).first()
return dbobj
def obj_to_dict(self, obj):
result = {}
for col_name in self.columns.keys():
attr = getattr(obj, col_name)
if attr is not None:
result[col_name] = attr
return result
def get(self, session, pkey):
return self.obj_to_dict(self._get_dbobj(session, pkey))
def update(self, session, pkey, content_dict):
count = session.query(self.db_class).filter(
self.primary_key == pkey).update(
content_dict)
return count
def delete(self, session, pkey):
count = session.query(self.db_class).filter(
self.primary_key == pkey).delete()
return count
def search(self, session, **kwargs):
query = session.query(self.db_class)
for key, value in kwargs.items():
mode = None
if '-' in key:
key, mode = key.split('-')
f = self.columns[key] == value
if mode == 'prefix':
f = self.columns[key].startswith(value)
query = query.filter(f)
return map(self.obj_to_dict, query)
class RestApi(object):
def __init__(self, dbapi, sessionmaker):
self.dbapi = dbapi
self.sessionmaker = sessionmaker
def wrapped_call(self, func, *args, **kwargs):
try:
session = self.sessionmaker()
result = func(session, *args, **kwargs)
session.commit()
return result
except Exception as e:
import traceback
traceback.print_exc()
session.rollback()
raise e
def get(self, pkey):
return self.wrapped_call(self.dbapi.get, pkey=pkey)
def put(self, pkey):
content_dict = json.loads(bottle.request.body.read())
count = self.wrapped_call(
self.dbapi.update,
pkey=pkey, content_dict=content_dict)
return {'modified': count}
def post(self):
content_dict = json.loads(bottle.request.body.read())
pkey = self.wrapped_call(
self.dbapi.create,
content_dict=content_dict)
return {'key': pkey}
def delete(self, pkey):
count = self.wrapped_call(self.dbapi.delete, pkey)
return {'deleted': count}
def search(self):
args = bottle.request.query
content = self.wrapped_call(self.dbapi.search, **args)
return {'result': list(content)}
class RestApiApp(object):
def __init__(self, connection, bottle_app=None):
if bottle_app is None:
bottle_app = bottle.default_app()
self.app = bottle_app
if isinstance(connection, str):
from sqlalchemy import create_engine
connection = create_engine(connection)
self.connection = connection
self.sessionmaker = sessionmaker(bind=connection)
self.apis = {}
def bind_api(self, url, clazz):
dbapi = DBApi(clazz)
self.apis[clazz] = dbapi
restapi = RestApi(dbapi, self.sessionmaker)
url_with_id = url +'/<pkey>'
self.app.get(url_with_id)(restapi.get)
self.app.put(url_with_id)(restapi.put)
self.app.delete(url_with_id)(restapi.delete)
self.app.post(url)(restapi.post)
self.app.get(url)(restapi.search)
def rest(self, url):
def decorator(clazz):
self.bind_api(url, clazz)
return clazz
return decorator
def getapi(self, clazz):
return self.apis.get(clazz)