Skip to content

Commit

Permalink
Add --bind_host. Simplify away a few helper-functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ways committed Jan 3, 2024
1 parent 7421c79 commit a788bbb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import uvicorn
from fastapi import FastAPI
from routes.routes import routes

from initialize import BIND_HOST

app = FastAPI(openapi_url="/openapi.json", docs_url="/api")
logger = logging.getLogger("uvicorn.access")
Expand All @@ -30,4 +30,4 @@ async def lifespan() -> AsyncGenerator[None, None]:


if __name__ == "__main__":
uvicorn.run("app:app", host="0.0.0.0", port=5000)
uvicorn.run("app:app", host=BIND_HOST, port=5000)
19 changes: 10 additions & 9 deletions app/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
logger = logging.getLogger()


@lru_cache()
def get_base_url() -> str:
"""Return base url."""
return BASE_URL


def parse_args() -> argparse.Namespace:
"""Parse arguments for grib filename and URL."""
parser = argparse.ArgumentParser()
Expand All @@ -33,6 +27,12 @@ def parse_args() -> argparse.Namespace:
default="http://localhost:5000/",
required=False,
)
parser.add_argument(
"--bind_host",
help="Which host to bind to.",
default="0.0.0.0",
required=False,
)
return parser.parse_args()


Expand All @@ -53,10 +53,10 @@ def open_grib():
global dataset

filename = build_gribfile_name(get_data_path(), time=datetime.now())
if len(get_filename()) > 0:
filename = get_filename()
if len(DATAFILE) > 0:
filename = DATAFILE
else:
if not check_gribfile_exists(data_path=get_data_path(), fname=get_filename()):
if not check_gribfile_exists(data_path=get_data_path(), fname=DATAFILE):
filename = download_gribfile(data_path=get_data_path())

try:
Expand Down Expand Up @@ -158,3 +158,4 @@ def download_gribfile(data_path: str, api_url: str = API_URL) -> str:
args = parse_args()
DATAFILE = args.file
BASE_URL = args.base_url
BIND_HOST = args.bind_host
7 changes: 3 additions & 4 deletions app/routes/collections_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from covjson_pydantic.coverage import Coverage
from covjson_pydantic.ndarray import NdArray

from initialize import get_base_url, get_dataset
from initialize import get_dataset, BASE_URL

from grib import (
get_vertical_extent,
Expand All @@ -26,7 +26,6 @@
ISOBARIC_LABEL,
)

base_url = get_base_url()
router = APIRouter()
logger = logging.getLogger()

Expand All @@ -35,12 +34,12 @@
def create_collection(collection_id: str = "") -> dict:
"""Creates the collections page."""
link_self = edr_pydantic.link.Link(
href=base_url, hreflang="en", rel="self", type="aplication/json"
href=BASE_URL, hreflang="en", rel="self", type="aplication/json"
)

dataset = get_dataset()
vertical_levels = get_vertical_extent(dataset)
collection_url = f"{base_url}collections/isobaric"
collection_url = f"{BASE_URL}collections/isobaric"

isobaric_col = Collection(
id="isobaric",
Expand Down
4 changes: 2 additions & 2 deletions app/routes/landing_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from edr_pydantic.capabilities import LandingPageModel, Provider, Contact
from edr_pydantic.link import Link

from initialize import get_base_url
from initialize import BASE_URL


@lru_cache
Expand Down Expand Up @@ -57,4 +57,4 @@ def create_landing_page(base_url) -> dict:
@router.get("/")
async def get_landing_page():
"""Link path to function."""
return create_landing_page(base_url=get_base_url())
return create_landing_page(base_url=BASE_URL)
20 changes: 14 additions & 6 deletions app/test_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""This is mostly to check all URLs reply without crash after code changes."""

import unittest
from fastapi.testclient import TestClient

Expand All @@ -12,23 +14,29 @@ def test_landingpage(self):
self.assertEqual(response.status_code, 200)
self.assertIn("EDR isobaric from Grib", response.text)


def test_conformance(self):
response = client.get("/conformance")
self.assertEqual(response.status_code, 200)
self.assertIn('{"conformsTo":["http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core",', response.text)

self.assertIn(
'{"conformsTo":["http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core",',
response.text,
)

def test_collections(self):
response = client.get("/collections")
self.assertEqual(response.status_code, 200)
self.assertIn('{"links":[{"href":"http://localhost:5000/","hreflang":"en","rel":"self","type":"aplication/json"}],"collections":[{"id":"isobaric","title":"', response.text)

self.assertIn(
'{"links":[{"href":"http://localhost:5000/","hreflang":"en","rel":"self","type":"aplication/json"}],"collections":[{"id":"isobaric","title":"',
response.text,
)

def test_point(self):
response = client.get("/collections/position?coords=POINT(60.1699 11.9384)")
self.assertEqual(response.status_code, 200)
self.assertIn('"vertical":{"interval":[["850.0"],["100.0"]],"values":["850.0","750.0","700.0","600.0","500.0","450.0","400.0","350.0","300.0","275.0","250.0","225.0","200.0","150.0","100.0"],"vrs":"Vertical Reference System: PressureLevel"}},', response.text)
self.assertIn(
'"vertical":{"interval":[["850.0"],["100.0"]],"values":["850.0","750.0","700.0","600.0","500.0","450.0","400.0","350.0","300.0","275.0","250.0","225.0","200.0","150.0","100.0"],"vrs":"Vertical Reference System: PressureLevel"}},',
response.text,
)


if __name__ == "__main__":
Expand Down

0 comments on commit a788bbb

Please sign in to comment.