-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.py
executable file
·456 lines (371 loc) · 15.6 KB
/
api.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/env python3
import base64
import os
from enum import Enum
from typing import Union
from urllib.parse import quote_plus
from elasticsearch import Elasticsearch
from elasticsearch.exceptions import TransportError
from fastapi import FastAPI, Request, Response, Body, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, RedirectResponse
from pydantic import BaseModel
from utils import load_config, env_to_list, env_to_dict, list_to_enum
class ApiVersion(str, Enum):
v1 = "1.0.0"
config = load_config()
config["termfields"] = env_to_list("TERMFIELDS") or config.get("termfields", [])
config["termaggrs"] = env_to_list("TERMAGGRS") or config.get("termaggrs", [])
config["indexes"] = env_to_list("INDEXES") or config.get("indexes", [])
config["eshosts"] = env_to_list("ESHOSTS") or config.get("eshosts", ["http://localhost:9200"])
config["esopts"] = env_to_dict("ESOPTS") or config.get("esopts", {})
config["wayback"] = os.getenv("WAYBACK", config.get("wayback", "https://web.archive.org/web")).rstrip("/")
config["maxpage"] = int(os.getenv("MAXPAGE", config.get("maxpage", 1000)))
config["title"] = os.getenv("TITLE", config.get("title", ""))
config["description"] = os.getenv("DESCRIPTION", config.get("description", ""))
config["debug"] = str(os.getenv("DEBUG", config.get("debug", False))).lower() in ("true", "1", "t")
ES = Elasticsearch(config["eshosts"], **config["esopts"])
Collection = list_to_enum("Collection", config["indexes"])
TermField = list_to_enum("TermField", config["termfields"])
TermAggr = list_to_enum("TermAggr", config["termaggrs"])
tags = [{
"name": "info",
"description": "Informational endpoints with human-readable responses to fill the hierarchy."
}, {
"name": "data",
"description": "Data endpoints with machine-readable responses to interact with the collection indexes."
}]
if config["debug"]:
tags.append({
"name": "debug",
"description": "Debugging endpoints with raw data from the backend, not suitable to be enabled in production."
})
app = FastAPI(
version=list(ApiVersion)[-1],
docs_url=None,
redoc_url=None,
openapi_url=None
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # nosemgrep: python.fastapi.security.wildcard-cors.wildcard-cors
allow_methods=["GET", "HEAD", "POST", "OPTIONS"],
allow_headers=["*"],
expose_headers=["link", "x-resume-token", "x-api-version"]
)
@app.middleware("http")
async def add_api_version_header(req: Request, call_next):
res = await call_next(req)
res.headers["x-api-version"] = f"{req.app.version}"
return res
v1 = FastAPI(
title=config.get("title", "Interactive API") + " Docs",
description=config.get("description", "A wrapper API for ES indexes."),
version=ApiVersion.v1,
openapi_tags=tags
)
class Query(BaseModel):
q: str
class PagedQuery(Query):
resume: Union[str, None] = None
def encode(strng: str):
return base64.b64encode(strng.encode(), b"-_").decode().replace("=", "~")
def decode(strng: str):
return base64.b64decode(strng.replace("~", "=").encode(), b"-_").decode()
def cs_basic_query(q: str):
return {
"_source": [
"title",
"publication_date",
"language",
"domain",
"url",
"first_captured"
],
"query": {
"query_string": {
"default_field": "snippet",
"default_operator": "AND",
"query": q
}
}
}
def cs_overview_query(q: str):
query = cs_basic_query(q)
query.update({
"aggregations": {
"daily": {
"date_histogram": {
"field": "publication_date",
"calendar_interval": "day",
"min_doc_count": 1
}
},
"lang": {
"terms": {
"field": "language",
"size": 100
}
},
"domain": {
"terms": {
"field": "domain",
"size": 100
}
},
"tld": {
"terms": {
"field": "tld",
"size": 100
}
}
}
})
return query
def cs_terms_query(q: str, field: str = "title", aggr: str = "top"):
resct = 200
aggr_map = {
"top": {
"terms": {
"field": field,
"size": resct,
"min_doc_count": 10,
"shard_min_doc_count": 5
}
},
"significant": {
"significant_terms": {
"field": field,
"size": resct,
"min_doc_count": 10,
"shard_min_doc_count": 5
}
},
"rare": {
"rare_terms": {
"field": field,
"exclude": "[0-9].*"
}
}
}
query = cs_basic_query(q)
query.update({
"track_total_hits": False,
"_source": False,
"aggregations": {
"sample": {
"sampler": {
"shard_size": 10 if aggr == "rare" else 500
},
"aggregations": {
"topterms": aggr_map[aggr]
}
}
}
})
return query
def cs_paged_query(q: str, resume: Union[str, None] = None):
query = cs_basic_query(q)
query.update({
"size": config["maxpage"],
"track_total_hits": False,
"sort": [{"surt_url": "asc"}]
})
if resume:
query["search_after"] = [decode(resume)]
return query
def format_match(hit: dict, base: str, collection: str, expanded: bool = False):
src = hit["_source"]
ct = src.get("first_captured") or "19700101000000"
res = {
"title": src.get("title") or "[UNKNOWN]",
"publication_date": (src.get("publication_date") or "")[:10],
"capture_time": f"{ct[:4]}-{ct[4:6]}-{ct[6:8]}T{ct[8:10]}:{ct[10:12]}:{ct[12:14]}Z",
"language": src.get("language") or "",
"domain": src["domain"],
"url": src["url"],
"original_capture_url": f"{config['wayback']}/{ct}id_/{src['url']}",
"archive_playback_url": f"{config['wayback']}/{ct}/{src['url']}",
"article_url": f"{base}/{collection}/article/{encode(hit['_id'])}"
}
if expanded:
res["surt_url"] = src["surt_url"]
res["snippet"] = src.get("snippet", "")
res["text_extraction_method"] = src.get("text_extraction_method", "")
res["version"] = src.get("version", "")
return res
def format_day_counts(bucket: list):
return {item["key_as_string"][:10]: item["doc_count"] for item in bucket}
def format_counts(bucket: list):
return {item["key"]: item["doc_count"] for item in bucket}
def proxy_base_url(req: Request):
return f'{str(os.getenv("PROXY_BASE", req.base_url)).rstrip("/")}/{req.scope.get("root_path").lstrip("/")}'
@app.get("/", response_class=HTMLResponse)
@app.head("/", response_class=HTMLResponse)
def api_entrypoint(req: Request):
"""
Link to the interactive API documentation
"""
ver = req.app.version.name if isinstance(req.app.version, Enum) else req.app.version
href = f"{req.scope.get('root_path')}/{ver}/docs"
return "\n".join(['<ul>',
f'<li><a href="{href}">Interactive API Docs ({ver})</a></li>',
'</ul>'])
@app.get("/docs", response_class=RedirectResponse)
@app.head("/docs", response_class=RedirectResponse)
def api_entrypoint_docs(req: Request):
"""
Redirect to recent API documentation
"""
ver = req.app.version.name if isinstance(req.app.version, Enum) else req.app.version
return f'{req.scope.get("root_path")}/{ver}/docs'
@app.get("/redoc", response_class=RedirectResponse)
@app.head("/redoc", response_class=RedirectResponse)
def api_entrypoint_redoc(req: Request):
"""
Redirect to recent API documentation
"""
ver = req.app.version.name if isinstance(req.app.version, Enum) else req.app.version
return f'{req.scope.get("root_path")}/{ver}/redoc'
@v1.get("/", response_class=HTMLResponse, tags=["info"])
@v1.head("/", response_class=HTMLResponse, include_in_schema=False)
def version_root(req: Request):
"""
Links to various collections
"""
lis = [f'<li><a href="{req.scope.get("root_path")}/{col.value}">{col.value}</a></li>' for col in Collection]
return "\n".join(['<ul>'] + lis + ['</ul>'])
@v1.get("/{collection}", response_class=HTMLResponse, tags=["info"])
@v1.head("/{collection}", response_class=HTMLResponse, include_in_schema=False)
def collection_root(collection: Collection, req: Request):
"""
Links to various collection API endpoints
"""
return "\n".join(['<ul>',
f'<li><a href="{req.scope.get("root_path")}/{collection.value}/search">Search API</a></li>',
f'<li><a href="{req.scope.get("root_path")}/{collection.value}/terms">Related Terms API</a></li>',
f'<li><a href="{req.scope.get("root_path")}/{collection.value}/article">Article</a></li>',
'</ul>'])
@v1.get("/{collection}/search", response_class=HTMLResponse, tags=["info"])
@v1.head("/{collection}/search", response_class=HTMLResponse, include_in_schema=False)
def search_root(collection: Collection, req: Request):
"""
Links to various search API endpoints
"""
spath = f"{req.scope.get('root_path')}/{collection.value}/search"
return "\n".join(['<ul>',
f'<li><a href="{spath}/overview">Search Overview</a></li>',
f'<li><a href="{spath}/result">Search Result</a></li>',
'</ul>'])
def _search_overview(collection: Collection, q: str, req: Request):
res = ES.search(index=collection.name, body=cs_overview_query(q))
if not res["hits"]["hits"]:
raise HTTPException(status_code=404, detail="No results found!")
total = res["hits"]["total"]["value"]
tldsum = sum(item["doc_count"] for item in res["aggregations"]["tld"]["buckets"])
base = proxy_base_url(req)
return {
"query": q,
"total": max(total, tldsum),
"topdomains": format_counts(res["aggregations"]["domain"]["buckets"]),
"toptlds": format_counts(res["aggregations"]["tld"]["buckets"]),
"toplangs": format_counts(res["aggregations"]["lang"]["buckets"]),
"dailycounts": format_day_counts(res["aggregations"]["daily"]["buckets"]),
"matches": [format_match(h, base, collection.value) for h in res["hits"]["hits"]]
}
@v1.get("/{collection}/search/overview", tags=["data"])
@v1.head("/{collection}/search/overview", include_in_schema=False)
def search_overview_via_query_params(collection: Collection, q: str, req: Request):
"""
Report overview summary of the search result
"""
return _search_overview(collection, q, req)
@v1.post("/{collection}/search/overview", tags=["data"])
def search_overview_via_payload(collection: Collection, req: Request, payload: Query):
"""
Report summary of the search result
"""
return _search_overview(collection, payload.q, req)
def _search_result(collection: Collection, q: str, req: Request, resp: Response, resume: Union[str, None] = None):
res = ES.search(index=collection.name, body=cs_paged_query(q, resume))
if not res["hits"]["hits"]:
raise HTTPException(status_code=404, detail="No results found!")
base = proxy_base_url(req)
qurl = f"{base}/{collection.value}/search/result?q={quote_plus(q)}"
if len(res["hits"]["hits"]) == config["maxpage"]:
resume_key = encode(res["hits"]["hits"][-1]["sort"][0])
resp.headers["x-resume-token"] = resume_key
resp.headers["link"] = f'<{qurl}&resume={resume_key}>; rel="next"'
return [format_match(h, base, collection.value) for h in res["hits"]["hits"]]
@v1.get("/{collection}/search/result", tags=["data"])
@v1.head("/{collection}/search/result", include_in_schema=False)
def search_result_via_query_params(collection: Collection, q: str, req: Request, resp: Response, resume: Union[str, None] = None): # pylint: disable=line-too-long
"""
Paged response of search result
"""
return _search_result(collection, q, req, resp, resume)
@v1.post("/{collection}/search/result", tags=["data"])
def search_result_via_payload(collection: Collection, req: Request, resp: Response, payload: PagedQuery):
"""
Paged response of search result
"""
return _search_result(collection, payload.q, req, resp, payload.resume)
if config["debug"]:
@v1.post("/{collection}/search/esdsl", tags=["debug"])
def search_esdsl_via_payload(collection: Collection, payload: dict = Body(...)):
"""
Search using ES Query DSL as JSON payload
"""
return ES.search(index=collection.name, body=payload)
def _get_terms(collection: Collection, q: str, field: TermField, aggr: TermAggr):
res = ES.search(index=collection.name, body=cs_terms_query(q, field, aggr))
if not res["hits"]["hits"] or not res["aggregations"]["sample"]["topterms"]["buckets"]:
raise HTTPException(status_code=404, detail="No results found!")
return format_counts(res["aggregations"]["sample"]["topterms"]["buckets"])
@v1.get("/{collection}/terms", response_class=HTMLResponse, tags=["info"])
@v1.head("/{collection}/terms", response_class=HTMLResponse, include_in_schema=False)
def term_field_root(collection: Collection, req: Request):
"""
Links to various term fields
"""
tbase = f"{req.scope.get('root_path')}/{collection.value}/terms"
lis = [f'<li><a href="{tbase}/{field.value}">{field.value}</a></li>' for field in TermField]
return "\n".join(['<ul>'] + lis + ['</ul>'])
@v1.get("/{collection}/terms/{field}", response_class=HTMLResponse, tags=["info"])
@v1.head("/{collection}/terms/{field}", response_class=HTMLResponse, include_in_schema=False)
def term_aggr_root(collection: Collection, req: Request, field: TermField):
"""
Links to various term aggregations
"""
fbase = f"{req.scope.get('root_path')}/{collection.value}/terms/{field.value}"
lis = [f'<li><a href="{fbase}/{aggr.value}">{aggr.value}</a></li>' for aggr in TermAggr]
return "\n".join(['<ul>'] + lis + ['</ul>'])
@v1.get("/{collection}/terms/{field}/{aggr}", tags=["data"])
@v1.head("/{collection}/terms/{field}/{aggr}", include_in_schema=False)
def get_terms_via_query_params(collection: Collection, q: str, field: TermField, aggr: TermAggr):
"""
Top terms with frequencies in matching articles
"""
return _get_terms(collection, q, field.value, aggr.value)
@v1.post("/{collection}/terms/{field}/{aggr}", tags=["data"])
def get_terms_via_payload(collection: Collection, payload: Query, field: TermField, aggr: TermAggr):
"""
Top terms with frequencies in matching articles
"""
return _get_terms(collection, payload.q, field.value, aggr.value)
@v1.get("/{collection}/article/{id}", tags=["data"])
@v1.head("/{collection}/article/{id}", include_in_schema=False)
def get_article(collection: Collection, id: str, req: Request): # pylint: disable=redefined-builtin
"""
Fetch an individual article record by ID
"""
try:
hit = ES.get(index=collection.name, id=decode(id))
except TransportError as e:
raise HTTPException(status_code=404, detail=f"An article with ID {decode(id)} not found!") from e
base = proxy_base_url(req)
return format_match(hit, base, collection.value, True)
app.mount(f"/{ApiVersion.v1.name}", v1)
if __name__ == "__main__":
import uvicorn
uvicorn.run("api:app", host="0.0.0.0", reload=True, root_path=os.getenv("ROOT_PATH", "/"))