-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
112 lines (94 loc) · 3.34 KB
/
Copy pathmain.py
File metadata and controls
112 lines (94 loc) · 3.34 KB
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
import json
import codecs
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, ORJSONResponse
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(CORSMiddleware, allow_origins=['*'])
import io
import zipfile
import ndc_parser
with zipfile.ZipFile("zips/ndc8.zip") as zfile:
with zfile.open("ndc8.ttl") as readfile:
ndc8_items_source = ndc_parser.parse("8", io.TextIOWrapper(readfile, "utf-8"))
ndc8_items = {}
for key, item in ndc8_items_source.items():
i = item.copy()
del i["source"]
ndc8_items[key] = i
with zipfile.ZipFile("zips/ndc9.zip") as zfile:
with zfile.open("ndc9.ttl") as readfile:
ndc9_items_source = ndc_parser.parse("9", io.TextIOWrapper(readfile, "utf-8"))
ndc9_items = {}
for key, item in ndc9_items_source.items():
i = item.copy()
del i["source"]
ndc9_items[key] = i
@app.get("/",
tags=["index"],
summary="トップページ",
description="トップページの表示",
response_description="トップページのHTMLを返す",
response_class=HTMLResponse
)
async def index():
with codecs.open("./templates/index.html", "r", "utf-8") as file:
return file.read()
@app.get("/schema",
tags=["schema"],
summary="JSONスキーマ",
description="JSONスキーマの取得",
response_description="JSONスキーマを返す"
)
async def schema():
with codecs.open("jsonschema.json", "r", "utf-8") as file:
json_schema = json.load(file)
return ORJSONResponse(json_schema, headers={'Access-Control-Allow-Origin': '*'})
@app.get("/ndc8.json",
tags=["ndc8"],
summary="NDC8",
description="NDC8の全データの取得",
response_description="NDC8の全データを返す"
)
async def ndc8_json():
return ORJSONResponse(ndc8_items, headers={'Access-Control-Allow-Origin': '*'})
@app.get("/ndc8/",
tags=["ndc8"],
summary="NDC8トップ",
description="NDC8のトップの取得",
response_description="NDC8のトップを返す"
)
async def ndc8_top():
return ORJSONResponse(ndc8_items_source[""], headers={'Access-Control-Allow-Origin': '*'})
@app.get("/ndc8/{ndc}",
tags=["ndc8"],
summary="NDC8分類項目",
description="NDC8の分類項目の取得",
response_description="NDC8の分類項目を返す"
)
async def ndc8(ndc: str):
return ORJSONResponse(ndc8_items_source[ndc], headers={'Access-Control-Allow-Origin': '*'})
@app.get("/ndc9.json",
tags=["ndc9"],
summary="NDC9",
description="NDC9の全データの取得",
response_description="NDC9の全データを返す"
)
async def ndc9_json():
return ORJSONResponse(ndc9_items, headers={'Access-Control-Allow-Origin': '*'})
@app.get("/ndc9/",
tags=["ndc9"],
summary="NDC9トップ",
description="NDC9のトップの取得",
response_description="NDC9のトップを返す"
)
async def ndc9_top():
return ORJSONResponse(ndc9_items_source[""], headers={'Access-Control-Allow-Origin': '*'})
@app.get("/ndc9/{ndc}",
tags=["ndc9"],
summary="NDC9分類項目",
description="NDC9の分類項目の取得",
response_description="NDC9の分類項目を返す"
)
async def ndc9(ndc: str):
return ORJSONResponse(ndc9_items_source[ndc], headers={'Access-Control-Allow-Origin': '*'})