-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
115 lines (95 loc) · 3.8 KB
/
main.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
# For developing: uvicorn main:app --reload
import logging
import uvicorn
from brotli_asgi import BrotliMiddleware
from edr_pydantic.capabilities import ConformanceModel
from edr_pydantic.capabilities import Contact
from edr_pydantic.capabilities import LandingPageModel
from edr_pydantic.capabilities import Provider
from edr_pydantic.collections import Collection
from edr_pydantic.collections import Collections
from edr_pydantic.link import Link
from fastapi import FastAPI
from fastapi import Request
from api import collection
from api import observations
from api.util import create_url_from_request
def setup_logging():
logger = logging.getLogger()
syslog = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s ; edr-api ; %(process)s ; %(levelname)s ; %(name)s ; %(message)s")
syslog.setFormatter(formatter)
logger.addHandler(syslog)
setup_logging()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
app = FastAPI(
title="RODEO WP5 EDR workshop", swagger_ui_parameters={"defaultModelsExpandDepth": -1, "tryItOutEnabled": True}
)
app.add_middleware(BrotliMiddleware)
@app.get(
"/",
tags=["Capabilities"],
response_model=LandingPageModel,
response_model_exclude_none=True,
)
async def landing_page(request: Request) -> LandingPageModel:
return LandingPageModel(
title="EDR tutorial",
description="A simple example EDR implementation",
keywords=["weather", "temperature", "wind", "humidity", "pressure", "clouds", "radiation"],
provider=Provider(name="RODEO", url="https://rodeo-project.eu/"),
contact=Contact(email="[email protected]"),
links=[
Link(href=f"{request.url}", rel="self", title="Landing Page in JSON"),
Link(href=f"{request.url}docs", rel="service-desc", title="API description in HTML"),
Link(href=f"{request.url}openapi.json", rel="service-desc", title="API description in JSON"),
Link(href=f"{request.url}conformance", rel="data", title="Conformance Declaration in JSON"),
Link(href=f"{request.url}collections", rel="data", title="Collections metadata in JSON"),
],
)
@app.get(
"/conformance",
tags=["Capabilities"],
response_model=ConformanceModel,
response_model_exclude_none=True,
)
async def get_conformance(request: Request) -> ConformanceModel:
return ConformanceModel(
conformsTo=[
"http://www.opengis.net/spec/ogcapi-edr-1/1.1/conf/core",
"http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core",
"http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/collections",
"http://www.opengis.net/spec/ogcapi-edr-1/1.1/conf/oas30",
# "http://www.opengis.net/spec/ogcapi-edr-1/1.1/conf/html",
"http://www.opengis.net/spec/ogcapi-edr-1/1.1/conf/edr-geojson",
"http://www.opengis.net/spec/ogcapi-edr-1/1.1/conf/covjson",
]
)
@app.get(
"/collections",
tags=["Capabilities"],
response_model=Collections,
response_model_exclude_none=True,
)
async def get_collections(request: Request) -> Collections:
base_url = create_url_from_request(request)
return Collections(
links=[
Link(href=f"{base_url}", rel="self"),
],
collections=[collection.get_collection_metadata(base_url, is_self=False)],
)
@app.get(
"/collections/observations",
tags=["Collection metadata"],
response_model=Collection,
response_model_exclude_none=True,
)
async def get_collection_metadata(request: Request) -> Collection:
base_url = create_url_from_request(request)
return collection.get_collection_metadata(base_url, is_self=True)
# Include other routes
app.include_router(observations.router)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)