-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
259 lines (236 loc) · 7.97 KB
/
app.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import os
from textwrap import dedent
import orjson
from dotenv import load_dotenv
from motor.motor_asyncio import AsyncIOMotorClient
from sanic import Sanic
from sanic.response import json
from sanic.views import HTTPMethodView
from sanic_ext import openapi
from api.models.note import Note
from api.query import Filter, Sort
load_dotenv()
app = Sanic(__name__)
settings = dict(
DEFAULT_LIMIT=50,
MAX_LIMIT=250,
DB_USER=os.environ.get('DB_USER'),
DB_PASSWORD=os.environ.get('DB_PASSWORD'),
DB_HOST=os.environ.get('DB_HOST'),
CORS_ORIGINS='*',
CORS_ALWAYS_SEND=False,
)
app.config.update(settings)
app.ext.openapi.describe(
"notesreview-api",
version="0.1.0",
description=dedent(
"""
# Information
This API is still subject to change, especially the behavior of the `query` parameter might change in the future,
because right now the possibilities are still a little bit limited.
"""
),
)
@app.before_server_start
async def setup(app, loop):
client = AsyncIOMotorClient(
f'mongodb://{app.config.DB_USER}:{app.config.DB_PASSWORD}@{app.config.DB_HOST}:27017?authSource=notesreview',
io_loop=loop,
)
app.ctx.client = client
app.ctx.db = client.notesreview
@app.before_server_stop
async def shutdown(app, loop):
app.ctx.client.close()
class Search(HTTPMethodView):
@openapi.description('Search and filter all notes in the database')
@openapi.parameter(
'query',
openapi.String(
description=dedent(
"""
A word or sentence which can be found in the comments.
To find an exact occurence of a word or sentence, wrap it in quotation marks `"{query}"`.
Single words can be excluded from the result by prepending a dash `-` to the word.
Spaces and other delimiters like dots are currently treated as a logical OR,
though this will likely change in the future.
"""
),
default=None,
required=False,
),
)
@openapi.parameter(
'bbox',
openapi.String(
description='A pair of coordinates specifying a rectangular box where all results are located in',
example='-87.6955,41.8353,-87.5871,41.9170',
default=None,
),
)
@openapi.parameter(
'polygon',
openapi.String(
description='A GeoJSON polygon specifying a region where all results are located in',
default=None,
),
)
@openapi.parameter(
'status',
openapi.String(
description='The current status of the note',
enum=('all', 'open', 'closed'),
default='all',
),
)
@openapi.parameter(
'anonymous',
openapi.String(
description='Whether anonymous notes should be included inclusively, excluded or included exclusively in the results',
enum=('include', 'hide', 'only'),
default='include',
),
)
@openapi.parameter(
'author',
openapi.String(
description='Name of the user who opened the note, searching for multiple users is possible by separating them with a comma',
default=None,
),
)
@openapi.parameter(
'user',
openapi.String(
description='Name of any user who commented on the note, searching for multiple users is possible by separating them with a comma',
default=None,
),
)
@openapi.parameter(
'after',
openapi.DateTime(
description='Only return notes updated or created after this date',
default=None,
example='2020-03-13T10:20:24',
),
)
@openapi.parameter(
'before',
openapi.DateTime(
description='Only return notes updated or created before this date',
default=None,
example='2020-05-11T07:10:45',
),
)
@openapi.parameter(
'comments',
openapi.Integer(
description='Filters the amount of comments on a note',
minimum=0,
default=None,
),
)
@openapi.parameter(
'commented',
openapi.String(
description='Whether commented notes should be included inclusively, excluded or included exclusively in the results',
enum=('include', 'hide', 'only'),
default='include',
),
)
@openapi.parameter(
'sort_by',
openapi.String(
description='Sort notes either by no criteria, the date of the last update or their creation date',
enum=('none', 'updated_at', 'created_at'),
default='updated_at',
),
)
@openapi.parameter(
'order',
openapi.String(
description='Sort notes either in ascending or descending order',
enum=('descending', 'desc', 'ascending', 'asc'),
default='descending',
),
)
@openapi.parameter(
'limit',
openapi.Integer(
description='Limit the amount of notes to return',
minimum=1,
maximum=app.config.MAX_LIMIT,
default=app.config.DEFAULT_LIMIT,
),
)
@openapi.response(
200,
openapi.Array(items=Note, uniqueItems=True),
'The response is an array containing the notes with the requested information',
)
@openapi.response(
400,
openapi.Object(properties={'error': openapi.String()}),
'In case one of the parameters is invalid, the response contains the error message',
)
async def get(self, request):
try:
sort, filter, limit = self.parse(request.args)
except ValueError as error:
return json({'error': str(error)}, status=400)
return await self.search(sort, filter, limit)
async def post(self, request):
try:
sort, filter, limit = self.parse(request.json)
except ValueError as error:
return json({'error': str(error)}, status=400)
return await self.search(sort, filter, limit)
def parse(self, data):
sort = (
Sort()
.by(data.get('sort_by', 'updated_at'))
.order(data.get('order', 'descending'))
.build()
)
filter = (
Filter(sort)
.query(data.get('query'))
.bbox(data.get('bbox'))
.polygon(data.get('polygon'))
.status(data.get('status'))
.anonymous(data.get('anonymous'))
.author(data.get('author'))
.user(data.get('user'))
.after(data.get('after', None))
.before(data.get('before', None))
.comments(data.get('comments', None))
.commented(data.get('commented'))
.build()
)
limit = data.get('limit', app.config.DEFAULT_LIMIT)
return sort, filter, limit
async def search(self, sort, filter, limit):
# Apply the default limit in case the argument could not be parsed (e.g. for limit=NaN)
try:
limit = int(limit)
except ValueError:
limit = app.config.DEFAULT_LIMIT
if limit > app.config.MAX_LIMIT:
return json(
{
'error': f'Limit must not be higher than {app.config.MAX_LIMIT}'
},
status=400,
)
# Prevent that a limit of 0 is treated as no limit at all
if limit == 0:
limit = app.config.DEFAULT_LIMIT
cursor = app.ctx.db.notes.find(filter).limit(limit)
# Queries are faster if the sorting is not explicitly specified (if desired)
if sort[0] is not None:
cursor = cursor.sort(*sort)
result = []
async for document in cursor:
result.append(document)
return json(result, dumps=orjson.dumps, option=orjson.OPT_NAIVE_UTC)
app.add_route(Search.as_view(), '/api/search')