weather_provider_api 2.29.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- var_maps/arome_var_map.json +27 -0
- var_maps/era5land_var_map.json +10 -0
- var_maps/era5sl_var_map.json +25 -0
- weather_provider_api/__init__.py +5 -0
- weather_provider_api/app_config.py +103 -0
- weather_provider_api/core/__init__.py +5 -0
- weather_provider_api/core/base_model.py +12 -0
- weather_provider_api/core/errors/__init__.py +5 -0
- weather_provider_api/core/errors/additional_responses.py +9 -0
- weather_provider_api/core/errors/api_models.py +11 -0
- weather_provider_api/core/errors/exceptions.py +19 -0
- weather_provider_api/core/initializers/__init__.py +5 -0
- weather_provider_api/core/initializers/error_handling.py +31 -0
- weather_provider_api/core/initializers/headers.py +34 -0
- weather_provider_api/core/initializers/logging.py +73 -0
- weather_provider_api/core/initializers/monitoring.py +25 -0
- weather_provider_api/core/initializers/mounting.py +27 -0
- weather_provider_api/core/initializers/validation.py +29 -0
- weather_provider_api/core/views.py +14 -0
- weather_provider_api/main.py +69 -0
- weather_provider_api/routers/__init__.py +5 -0
- weather_provider_api/routers/weather/__init__.py +5 -0
- weather_provider_api/routers/weather/api_models.py +179 -0
- weather_provider_api/routers/weather/api_view_v1.py +155 -0
- weather_provider_api/routers/weather/api_view_v2.py +186 -0
- weather_provider_api/routers/weather/base_models/__init__.py +3 -0
- weather_provider_api/routers/weather/base_models/model.py +180 -0
- weather_provider_api/routers/weather/base_models/source.py +37 -0
- weather_provider_api/routers/weather/controller.py +153 -0
- weather_provider_api/routers/weather/exceptions.py +29 -0
- weather_provider_api/routers/weather/repository/__init__.py +5 -0
- weather_provider_api/routers/weather/repository/repository.py +340 -0
- weather_provider_api/routers/weather/sources/__init__.py +5 -0
- weather_provider_api/routers/weather/sources/cds/__init__.py +5 -0
- weather_provider_api/routers/weather/sources/cds/cds.py +21 -0
- weather_provider_api/routers/weather/sources/cds/client/__init__.py +5 -0
- weather_provider_api/routers/weather/sources/cds/client/downloader.py +452 -0
- weather_provider_api/routers/weather/sources/cds/client/era5land_repository.py +169 -0
- weather_provider_api/routers/weather/sources/cds/client/era5sl_repository.py +161 -0
- weather_provider_api/routers/weather/sources/cds/client/utils_era5.py +357 -0
- weather_provider_api/routers/weather/sources/cds/factors.py +20 -0
- weather_provider_api/routers/weather/sources/cds/models/__init__.py +5 -0
- weather_provider_api/routers/weather/sources/cds/models/era5land.py +208 -0
- weather_provider_api/routers/weather/sources/cds/models/era5sl.py +189 -0
- weather_provider_api/routers/weather/sources/knmi/__init__.py +5 -0
- weather_provider_api/routers/weather/sources/knmi/client/__init__.py +5 -0
- weather_provider_api/routers/weather/sources/knmi/client/arome_repository.py +478 -0
- weather_provider_api/routers/weather/sources/knmi/client/knmi_downloader.py +156 -0
- weather_provider_api/routers/weather/sources/knmi/knmi.py +33 -0
- weather_provider_api/routers/weather/sources/knmi/knmi_factors.py +16 -0
- weather_provider_api/routers/weather/sources/knmi/models/__init__.py +5 -0
- weather_provider_api/routers/weather/sources/knmi/models/actuele_waarnemingen.py +259 -0
- weather_provider_api/routers/weather/sources/knmi/models/daggegevens.py +361 -0
- weather_provider_api/routers/weather/sources/knmi/models/harmonie_arome.py +120 -0
- weather_provider_api/routers/weather/sources/knmi/models/pluim.py +288 -0
- weather_provider_api/routers/weather/sources/knmi/models/uurgegevens.py +324 -0
- weather_provider_api/routers/weather/sources/knmi/stations.py +752 -0
- weather_provider_api/routers/weather/sources/knmi/utils.py +54 -0
- weather_provider_api/routers/weather/sources/weather_alert/__init__.py +5 -0
- weather_provider_api/routers/weather/sources/weather_alert/weather_alert.py +136 -0
- weather_provider_api/routers/weather/utils/__init__.py +5 -0
- weather_provider_api/routers/weather/utils/date_helpers.py +137 -0
- weather_provider_api/routers/weather/utils/file_helpers.py +46 -0
- weather_provider_api/routers/weather/utils/geo_position.py +183 -0
- weather_provider_api/routers/weather/utils/grid_helpers.py +50 -0
- weather_provider_api/routers/weather/utils/pandas_helpers.py +17 -0
- weather_provider_api/routers/weather/utils/serializers.py +152 -0
- weather_provider_api/scripts/__init__.py +5 -0
- weather_provider_api/scripts/erase_arome_repository.py +16 -0
- weather_provider_api/scripts/erase_era5land_repository.py +17 -0
- weather_provider_api/scripts/erase_era5sl_repository.py +16 -0
- weather_provider_api/scripts/update_arome_repository.py +16 -0
- weather_provider_api/scripts/update_era5land_repository.py +16 -0
- weather_provider_api/scripts/update_era5sl_repository.py +16 -0
- weather_provider_api/versions/__init__.py +5 -0
- weather_provider_api/versions/v1.py +14 -0
- weather_provider_api/versions/v2.py +14 -0
- weather_provider_api-2.29.8.dist-info/LICENSE +373 -0
- weather_provider_api-2.29.8.dist-info/LICENSES/Apache-2.0.txt +183 -0
- weather_provider_api-2.29.8.dist-info/LICENSES/CC-BY-2.5.txt +220 -0
- weather_provider_api-2.29.8.dist-info/LICENSES/MPL-2.0.txt +292 -0
- weather_provider_api-2.29.8.dist-info/METADATA +234 -0
- weather_provider_api-2.29.8.dist-info/RECORD +85 -0
- weather_provider_api-2.29.8.dist-info/WHEEL +4 -0
- weather_provider_api-2.29.8.dist-info/entry_points.txt +9 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"1": "mean_sea_level_pressure",
|
|
3
|
+
"6": "geopotential",
|
|
4
|
+
"11": "temperature",
|
|
5
|
+
"33": "u_component_of_wind",
|
|
6
|
+
"34": "v_component_of_wind",
|
|
7
|
+
"52": "relative_humidity",
|
|
8
|
+
"66": "snow_cover",
|
|
9
|
+
"67": "boundary_layer_height",
|
|
10
|
+
"71": "cloud_cover",
|
|
11
|
+
"73": "low_cloud_cover",
|
|
12
|
+
"74": "medium_cloud_cover",
|
|
13
|
+
"75": "high_cloud_cover",
|
|
14
|
+
"111": "net_short_wave_radiation",
|
|
15
|
+
"112": "net_long_wave_radiation",
|
|
16
|
+
"117": "global_radiation",
|
|
17
|
+
"122": "sensible_heat_flux",
|
|
18
|
+
"132": "latent_heat_flux",
|
|
19
|
+
"162": "u_component_max_squall",
|
|
20
|
+
"163": "v_component_max_squall",
|
|
21
|
+
"181": "_rain_water",
|
|
22
|
+
"184": "_snow_water",
|
|
23
|
+
"186": "cloud_base",
|
|
24
|
+
"201": "_graupel",
|
|
25
|
+
"SD Snow depth m": "snow_depth",
|
|
26
|
+
"T Temperature K": "temperature - Extra copy"
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"stl1": "soil_temperature_level_1",
|
|
3
|
+
"stl2": "soil_temperature_level_2",
|
|
4
|
+
"stl3": "soil_temperature_level_3",
|
|
5
|
+
"stl4": "soil_temperature_level_4",
|
|
6
|
+
"swvl1": "volumetric_soil_water_layer_1",
|
|
7
|
+
"swvl2": "volumetric_soil_water_layer_2",
|
|
8
|
+
"swvl3": "volumetric_soil_water_layer_3",
|
|
9
|
+
"swvl4": "volumetric_soil_water_layer_4"
|
|
10
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"u100": "100m_u_component_of_wind",
|
|
3
|
+
"v100": "100m_v_component_of_wind",
|
|
4
|
+
"d2m": "2m_dewpoint_temperature",
|
|
5
|
+
"msl": "mean_sea_level_pressure",
|
|
6
|
+
"mwd": "mean_wave_direction",
|
|
7
|
+
"mwp": "mean_wave_period",
|
|
8
|
+
"sp": "surface_pressure",
|
|
9
|
+
"sst": "sea_surface_temperature",
|
|
10
|
+
"stl1": "soil_temperature_level_1",
|
|
11
|
+
"stl2": "soil_temperature_level_2",
|
|
12
|
+
"stl3": "soil_temperature_level_3",
|
|
13
|
+
"stl4": "soil_temperature_level_4",
|
|
14
|
+
"ssrd": "surface_solar_radiation_downwards",
|
|
15
|
+
"ssrdc": "surface_solar_radiation_downward_clear_sky",
|
|
16
|
+
"swh": "significant_height_of_combined_wind_waves_and_swell",
|
|
17
|
+
"swvl1": "volumetric_soil_water_layer_1",
|
|
18
|
+
"swvl2": "volumetric_soil_water_layer_2",
|
|
19
|
+
"swvl3": "volumetric_soil_water_layer_3",
|
|
20
|
+
"swvl4": "volumetric_soil_water_layer_4",
|
|
21
|
+
"t2m": "2m_temperature",
|
|
22
|
+
"tp": "total_precipitation",
|
|
23
|
+
"u10": "10m_u_component_of_wind",
|
|
24
|
+
"v10": "10m_v_component_of_wind"
|
|
25
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import tempfile
|
|
9
|
+
from importlib import metadata as importlib_metadata
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class BaseConfig(object):
|
|
13
|
+
""" Base configuration class
|
|
14
|
+
|
|
15
|
+
This class holds and/or gathers all the APIs base settings...
|
|
16
|
+
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
APP_NAME = os.environ.get("APP_NAME", __name__)
|
|
20
|
+
APP_DESCRIPTION = os.environ.get("APP_DESCRIPTION", """Alliander Weather Provider API""")
|
|
21
|
+
APP_MAINTAINER = os.environ.get("APP_MAINTAINER", "DNB/ST Innovatieteam")
|
|
22
|
+
APP_MAINTAINER_EMAIL = os.environ.get(
|
|
23
|
+
"APP_MAINTAINER_EMAIL", "weather.provider@alliander.com"
|
|
24
|
+
)
|
|
25
|
+
SHOW_MAINTAINER = os.environ.get("SHOW_MAINTAINER", False)
|
|
26
|
+
APP_VERSION = os.environ.get("APP_VERSION", importlib_metadata.version(__package__))
|
|
27
|
+
APP_V1_VERSION = os.environ.get("APP_V1_VERSION", "2.0.3")
|
|
28
|
+
APP_V2_VERSION = os.environ.get("APP_V1_VERSION", "2.0.3")
|
|
29
|
+
APP_VALID_DATE = os.environ.get("APP_VALID_DATE", "2024-12-31")
|
|
30
|
+
NETWORK_INTERFACE = os.environ.get("NETWORK_INTERFACE", "127.0.0.1")
|
|
31
|
+
NETWORK_PORT = os.environ.get("NETWORK_PORT", 8080)
|
|
32
|
+
LOG_LEVEL = os.environ.get("LOG_LEVEL", "DEBUG")
|
|
33
|
+
|
|
34
|
+
# The following two parameters determine the active configuration
|
|
35
|
+
DEPLOYED = os.environ.get("DEPLOYED", False)
|
|
36
|
+
DEBUG = os.environ.get("DEBUG", True)
|
|
37
|
+
|
|
38
|
+
REPO_FOLDER = os.environ.get("REPO_FOLDER", f"{tempfile.gettempdir()}/Weather_Repository")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class LocalConfig(BaseConfig):
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class LocalDebugConfig(LocalConfig):
|
|
46
|
+
DEBUG = True
|
|
47
|
+
LOG_LEVEL = os.environ.get("LOG_LEVEL", "DEBUG")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class DeployedConfig(BaseConfig):
|
|
51
|
+
NETWORK_INTERFACE = os.environ.get("NETWORK_INTERFACE", "0.0.0.0")
|
|
52
|
+
DEPLOYED = True
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class DeployedDebugConfig(DeployedConfig):
|
|
56
|
+
DEBUG = True
|
|
57
|
+
LOG_LEVEL = os.environ.get("LOG_LEVEL", "DEBUG")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
_active_config = None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def get_active_config(): # pragma: no cover
|
|
64
|
+
"""Retrieve the active configuration.
|
|
65
|
+
|
|
66
|
+
The active configuration is based on the DEPLOYED and DEBUG
|
|
67
|
+
environment variables. The active config is set once and returned
|
|
68
|
+
for each subsequent call.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
Active configuration
|
|
72
|
+
"""
|
|
73
|
+
global _active_config
|
|
74
|
+
|
|
75
|
+
is_deployed = os.environ.get("DEPLOYED", False)
|
|
76
|
+
is_debug = os.environ.get("DEBUG", False)
|
|
77
|
+
|
|
78
|
+
if _active_config is None:
|
|
79
|
+
if is_deployed:
|
|
80
|
+
if is_debug:
|
|
81
|
+
_active_config = DeployedDebugConfig
|
|
82
|
+
else:
|
|
83
|
+
_active_config = DeployedConfig
|
|
84
|
+
else:
|
|
85
|
+
if is_debug:
|
|
86
|
+
_active_config = LocalDebugConfig
|
|
87
|
+
else:
|
|
88
|
+
_active_config = LocalConfig
|
|
89
|
+
|
|
90
|
+
return _active_config
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def get_setting(key: str):
|
|
94
|
+
"""Return value set for key.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
key (str): key of configuration setting
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
value if key exists, None otherwise
|
|
101
|
+
"""
|
|
102
|
+
active_config = get_active_config()
|
|
103
|
+
return getattr(active_config, key, None)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
from pydantic import BaseModel as PydanticBaseModel
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class BaseModel(PydanticBaseModel):
|
|
11
|
+
class Config:
|
|
12
|
+
orm_mode = True
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
from weather_provider_api.core.errors.api_models import ErrorResponseModel
|
|
8
|
+
|
|
9
|
+
ERROR_RESPONSE = {"model": ErrorResponseModel}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
from weather_provider_api.core.base_model import BaseModel
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ErrorResponseModel(BaseModel):
|
|
11
|
+
detail: str # error message
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from fastapi import HTTPException
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class APIExpiredException(HTTPException):
|
|
13
|
+
def __init__(self, detail: Any = None):
|
|
14
|
+
self.detail = (
|
|
15
|
+
detail
|
|
16
|
+
or "This API has passed it's expiry date and should be revalidated. "
|
|
17
|
+
"Please contact the API maintainer."
|
|
18
|
+
)
|
|
19
|
+
self.status_code = 404
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
import structlog
|
|
8
|
+
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
9
|
+
from starlette.requests import Request
|
|
10
|
+
from starlette.responses import JSONResponse
|
|
11
|
+
|
|
12
|
+
from weather_provider_api.app_config import get_setting
|
|
13
|
+
|
|
14
|
+
logger = structlog.get_logger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
async def handle_http_exception(
|
|
18
|
+
request: Request, exc: StarletteHTTPException
|
|
19
|
+
) -> JSONResponse:
|
|
20
|
+
headers = getattr(exc, "headers", None)
|
|
21
|
+
|
|
22
|
+
body = {"detail": exc.detail}
|
|
23
|
+
if get_setting("SHOW_MAINTAINER"):
|
|
24
|
+
body["maintainer"] = get_setting("APP_MAINTAINER")
|
|
25
|
+
body["maintainer_email"] = get_setting("APP_MAINTAINER_EMAIL")
|
|
26
|
+
|
|
27
|
+
return JSONResponse(body, status_code=exc.status_code, headers=headers)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def initialize_error_handling(app):
|
|
31
|
+
app.add_exception_handler(StarletteHTTPException, handle_http_exception)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
8
|
+
from starlette.requests import Request
|
|
9
|
+
|
|
10
|
+
from weather_provider_api.app_config import get_setting
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def initialize_metadata_header_middleware(app):
|
|
14
|
+
async def add_metadata_headers(request: Request, call_next):
|
|
15
|
+
response = await call_next(request)
|
|
16
|
+
response.headers["X-App-Version"] = get_setting("APP_VERSION")
|
|
17
|
+
response.headers["X-App-Valid-Till"] = get_setting("APP_VALID_DATE")
|
|
18
|
+
return response
|
|
19
|
+
|
|
20
|
+
async def add_metadata_headers_with_maintainer(request: Request, call_next):
|
|
21
|
+
response = await call_next(request)
|
|
22
|
+
response.headers["X-App-Version"] = get_setting("APP_VERSION")
|
|
23
|
+
response.headers["X-App-Valid-Till"] = get_setting("APP_VALID_DATE")
|
|
24
|
+
response.headers["X-Maintainer"] = get_setting("APP_MAINTAINER")
|
|
25
|
+
response.headers["X-Maintainer-Email"] = get_setting("APP_MAINTAINER_EMAIL")
|
|
26
|
+
return response
|
|
27
|
+
|
|
28
|
+
show_maintainer = get_setting("SHOW_MAINTAINER")
|
|
29
|
+
if show_maintainer:
|
|
30
|
+
insertion_func = add_metadata_headers_with_maintainer
|
|
31
|
+
else:
|
|
32
|
+
insertion_func = add_metadata_headers
|
|
33
|
+
|
|
34
|
+
app.add_middleware(BaseHTTPMiddleware, dispatch=insertion_func)
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
import logging.config
|
|
9
|
+
|
|
10
|
+
import structlog
|
|
11
|
+
|
|
12
|
+
from weather_provider_api.app_config import get_setting
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def add_application_metadata(logger, method_name, event_dict):
|
|
16
|
+
if "app_version" not in event_dict:
|
|
17
|
+
event_dict["app_version"] = get_setting("APP_VERSION")
|
|
18
|
+
if "app_valid_date" not in event_dict:
|
|
19
|
+
event_dict["app_valid_date"] = get_setting("APP_VALID_DATE")
|
|
20
|
+
|
|
21
|
+
return event_dict
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def initialize_logging():
|
|
25
|
+
timestamper = structlog.processors.TimeStamper(fmt="iso")
|
|
26
|
+
|
|
27
|
+
shared_processors = [
|
|
28
|
+
structlog.stdlib.add_logger_name,
|
|
29
|
+
structlog.stdlib.add_log_level,
|
|
30
|
+
add_application_metadata,
|
|
31
|
+
timestamper,
|
|
32
|
+
structlog.processors.UnicodeDecoder(),
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
pre_chain = shared_processors
|
|
36
|
+
|
|
37
|
+
# Setup JSON-logging to stdout
|
|
38
|
+
logging.captureWarnings(True)
|
|
39
|
+
logging.config.dictConfig(
|
|
40
|
+
{
|
|
41
|
+
"version": 1,
|
|
42
|
+
"disable_existing_loggers": False,
|
|
43
|
+
"formatters": {
|
|
44
|
+
"json": {
|
|
45
|
+
"()": structlog.stdlib.ProcessorFormatter,
|
|
46
|
+
"processor": structlog.processors.JSONRenderer(),
|
|
47
|
+
"foreign_pre_chain": pre_chain,
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"handlers": {
|
|
51
|
+
"default": {
|
|
52
|
+
"level": get_setting("LOG_LEVEL"),
|
|
53
|
+
"class": "logging.StreamHandler",
|
|
54
|
+
"formatter": "json",
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"loggers": {
|
|
58
|
+
"": {
|
|
59
|
+
"handlers": ["default"],
|
|
60
|
+
"level": get_setting("LOG_LEVEL"),
|
|
61
|
+
"propagate": True,
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
structlog.configure(
|
|
68
|
+
processors=[structlog.stdlib.filter_by_level]
|
|
69
|
+
+ shared_processors
|
|
70
|
+
+ [structlog.stdlib.ProcessorFormatter.wrap_for_formatter],
|
|
71
|
+
logger_factory=structlog.stdlib.LoggerFactory(),
|
|
72
|
+
cache_logger_on_first_use=True,
|
|
73
|
+
)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
import structlog
|
|
8
|
+
from starlette_prometheus import metrics, PrometheusMiddleware
|
|
9
|
+
|
|
10
|
+
from weather_provider_api.app_config import get_setting
|
|
11
|
+
|
|
12
|
+
logger = structlog.get_logger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def initialize_prometheus_middleware(app, endpoint="/metrics"):
|
|
16
|
+
"""Start HTTP endpoint for Prometheus monitoring.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
app: FastAPI weather_provider_api instance
|
|
20
|
+
endpoint (str): URL at which the metrics should be available
|
|
21
|
+
"""
|
|
22
|
+
if get_setting("DEPLOYED"):
|
|
23
|
+
logger.info(f'Enabling Prometheus endpoint on: "{endpoint}"')
|
|
24
|
+
app.add_middleware(PrometheusMiddleware)
|
|
25
|
+
app.add_route(endpoint, metrics)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
from fastapi import FastAPI
|
|
8
|
+
|
|
9
|
+
from weather_provider_api.core.initializers.error_handling import initialize_error_handling
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def mount_api_version(base_app: FastAPI, versioned_app):
|
|
13
|
+
"""Mounts a versioned API application.
|
|
14
|
+
|
|
15
|
+
This method helps to expose an API version to the main application.
|
|
16
|
+
Additionally, it ensures Alliander core API endpoints are set. The latter
|
|
17
|
+
are obligatory and may not be removed or bypassed.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
base_app: main FastAPI application
|
|
21
|
+
versioned_app: versioned FastAPI application
|
|
22
|
+
"""
|
|
23
|
+
from weather_provider_api.core.views import router
|
|
24
|
+
|
|
25
|
+
initialize_error_handling(versioned_app)
|
|
26
|
+
versioned_app.include_router(router, prefix="/core")
|
|
27
|
+
base_app.mount(versioned_app.root_path, versioned_app)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
import datetime
|
|
8
|
+
|
|
9
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
10
|
+
from starlette.requests import Request
|
|
11
|
+
|
|
12
|
+
from weather_provider_api.app_config import get_setting
|
|
13
|
+
from weather_provider_api.core.errors.exceptions import APIExpiredException
|
|
14
|
+
from weather_provider_api.core.initializers.error_handling import handle_http_exception
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def initialize_validation_middleware(app):
|
|
18
|
+
|
|
19
|
+
valid_date = datetime.datetime.strptime(get_setting("APP_VALID_DATE"), "%Y-%m-%d")
|
|
20
|
+
|
|
21
|
+
async def check_api_validity(request: Request, call_next):
|
|
22
|
+
if datetime.datetime.now() > valid_date:
|
|
23
|
+
response = await handle_http_exception(request, APIExpiredException())
|
|
24
|
+
else:
|
|
25
|
+
response = await call_next(request)
|
|
26
|
+
|
|
27
|
+
return response
|
|
28
|
+
|
|
29
|
+
app.add_middleware(BaseHTTPMiddleware, dispatch=check_api_validity)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
from fastapi import APIRouter
|
|
8
|
+
|
|
9
|
+
router = APIRouter()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# TODO: implement API status retrieval
|
|
13
|
+
def get_api_status():
|
|
14
|
+
pass
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
|
|
5
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
"""Main API executable
|
|
9
|
+
This module contains everything required to boot the Weather Provider API.
|
|
10
|
+
Its '__main__' if-clause handles running it directly from a directory or IDE setting.
|
|
11
|
+
|
|
12
|
+
"""
|
|
13
|
+
from datetime import datetime
|
|
14
|
+
|
|
15
|
+
import structlog
|
|
16
|
+
import uvicorn
|
|
17
|
+
from fastapi import FastAPI
|
|
18
|
+
from starlette.responses import RedirectResponse
|
|
19
|
+
|
|
20
|
+
from weather_provider_api.app_config import get_setting
|
|
21
|
+
from weather_provider_api.core.initializers.error_handling import \
|
|
22
|
+
initialize_error_handling
|
|
23
|
+
from weather_provider_api.core.initializers.headers import \
|
|
24
|
+
initialize_metadata_header_middleware
|
|
25
|
+
from weather_provider_api.core.initializers.logging import initialize_logging
|
|
26
|
+
from weather_provider_api.core.initializers.monitoring import \
|
|
27
|
+
initialize_prometheus_middleware
|
|
28
|
+
from weather_provider_api.core.initializers.mounting import mount_api_version
|
|
29
|
+
from weather_provider_api.core.initializers.validation import \
|
|
30
|
+
initialize_validation_middleware
|
|
31
|
+
from weather_provider_api.versions.v1 import app as v1
|
|
32
|
+
from weather_provider_api.versions.v2 import app as v2
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def main():
|
|
36
|
+
# Enable logging
|
|
37
|
+
initialize_logging()
|
|
38
|
+
logger = structlog.get_logger(__name__)
|
|
39
|
+
logger.info(f'--------------------------------------', datetime=datetime.utcnow())
|
|
40
|
+
logger.info(f'Booting Weather Provider API Systems..', datetime=datetime.utcnow())
|
|
41
|
+
logger.info(f'--------------------------------------', datetime=datetime.utcnow())
|
|
42
|
+
|
|
43
|
+
# Create and configure new application instance
|
|
44
|
+
app = FastAPI(version=get_setting("APP_VERSION"), title=get_setting("APP_NAME"))
|
|
45
|
+
initialize_error_handling(app)
|
|
46
|
+
initialize_metadata_header_middleware(app)
|
|
47
|
+
initialize_validation_middleware(app)
|
|
48
|
+
initialize_prometheus_middleware(app)
|
|
49
|
+
|
|
50
|
+
# Activate enabled API versions
|
|
51
|
+
mount_api_version(app, v1)
|
|
52
|
+
mount_api_version(app, v2)
|
|
53
|
+
|
|
54
|
+
# Redirect users to the docs
|
|
55
|
+
@app.get("/")
|
|
56
|
+
def redirect_to_docs():
|
|
57
|
+
redirect_url = "/api/v2/docs" # replace with docs URL or use weather_provider_api.url_path_for()
|
|
58
|
+
return RedirectResponse(url=redirect_url)
|
|
59
|
+
|
|
60
|
+
logger.info(f'--------------------------------------', datetime=datetime.utcnow())
|
|
61
|
+
logger.info(f'Finished booting; starting uvicorn...', datetime=datetime.utcnow())
|
|
62
|
+
logger.info(f'--------------------------------------', datetime=datetime.utcnow())
|
|
63
|
+
|
|
64
|
+
uvicorn.run(app, host="127.0.0.1", port=8080)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
main()
|
|
69
|
+
|