Skip to content

Commit

Permalink
Merge pull request #30 from internetofwater/simplify-openapi
Browse files Browse the repository at this point in the history
trim down openapi doc generation
  • Loading branch information
gzt5142 authored Dec 17, 2024
2 parents 70626d6 + 41c29f1 commit 3eb0be7
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 190 deletions.
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ services:
- "8080:80"
environment:
NLDI_PATH: /api/nldi
NLDI_URL: http://localhost:80/api/nldi
NLDI_URL: http://localhost:80/
NLDI_DB_HOST: nldi-db
NLDI_DB_PORT: 5432
NLDI_DB_NAME: nldi
NLDI_DB_USERNAME: nldi
NLDI_DB_PASSWORD: changeMe
PYGEOAPI_URL: https://labs.waterdata.usgs.gov/api/nldi/pygeoapi/
springFrameworkLogLevel:
networks:
- nldi

Expand Down
1 change: 1 addition & 0 deletions docker/x.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export NLDI_SCHEMA_OWNER_PASSWORD=changeMe
export NHDPLUS_SCHEMA_OWNER_USERNAME=nhdplus
export NLDI_READ_ONLY_USERNAME=read_only_user
export NLDI_READ_ONLY_PASSWORD=changeMe
export springFrameworkLogLevel=DEBUG
304 changes: 164 additions & 140 deletions src/nldi/api/main.py

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/nldi/api/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ identifier:
schema:
type: string

sourceid:
name: sourceid
in: path
description: Source identifier
required: true
schema:
type: string

comid:
name: comid,
in: path,
description: NHDPlus common identifier
required: True,
schema:
type: integer
example: 13294314

simplified:
name: simplified
in: query
Expand Down
42 changes: 7 additions & 35 deletions src/nldi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,6 @@

from . import LOGGER, util

# def align_crawler_sources(cfg_file: Union[pathlib.Path, io.TextIOWrapper]) -> bool:
# """
# Align Crawler Source from the configuration file

# :param cfg_file: Configuration Path instance
# :type cfg_file: Union[pathlib.Path, io.TextIOWrapper]
# :return: Success/Failure of alignment
# :rtype: bool
# """
# LOGGER.debug("Aligning sources from config file.")
# config = util.load_yaml(cfg_file)
# if not config:
# LOGGER.error("Unable to load configuration file.")
# return False

# if not config.get("sources"):
# LOGGER.debug("No sources to align with, continuing")
# return True

# provider_def = {
# "database": deepcopy(config["server"]["data"]),
# "base_url": get_base_url(config),
# }

# LOGGER.debug("Aligning configuration with crawler source table")
# try:
# crawler_source = CrawlerSourceLookup(provider_def)
# crawler_source.align_sources(config["sources"])
# except ProviderQueryError:
# LOGGER.error("Provider Query Error -- Do you have permissions to update Crawler source table?")
# return False

# return True


class Configuration(UserDict):
"""Data class for holding global configuration."""
Expand All @@ -71,6 +37,7 @@ def __init__(self, cfg_src: Union[pathlib.Path, str, io.TextIOWrapper]):
for k, v in dict(
# NLDI_PATH="/api/nldi",
NLDI_URL="http://localhost:8081/",
NLDI_PATH="/api/nldi",
NLDI_DB_HOST="172.17.0.2", # This is the IP address for the Docker container
NLDI_DB_PORT="5432",
NLDI_DB_NAME="nldi",
Expand All @@ -82,7 +49,12 @@ def __init__(self, cfg_src: Union[pathlib.Path, str, io.TextIOWrapper]):
ENV[k] = v

self.data = util.load_yaml(cfg_src)
self.data["base_url"] = util.url_join(self.data["server"]["url"], "")

self.data["base_url"] = util.url_join(
self.data["server"].get("url", "http://localhost/"),
self.data["server"].get("prefix", ""),
"",
)

if isinstance(cfg_src, io.TextIOWrapper):
self._config_src = "<stream>"
Expand Down
36 changes: 24 additions & 12 deletions src/nldi/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
NLDI_API = None ## Global -- this will be assigned inside the app factory later.

# region Preamble
log.initialize(LOGGER, level="DEBUG")
##TODO: The log level should be a configurable option, perhaps set with -V or --verbose switch.

## Loading CONFIG here, after loggers and whatnot are set up.
if "NLDI_CONFIG" not in os.environ:
raise RuntimeError("NLDI_CONFIG environment variable not set")
CONFIG = Configuration(os.environ.get("NLDI_CONFIG"))

log.initialize(LOGGER, level=CONFIG["logging"]["level"]) # NOTE: unfortunately, we have chosen to
# make the log level configurable in the config file, which means we can't set the level until the
# log file is read. So no logging during the config process.

LOGGER.info(f"NLDI v{__version__} API Server >> Starting Up")
log.versions(logger=LOGGER)

Expand All @@ -49,6 +50,10 @@ def log_incoming_request() -> None:
LOGGER.debug(f"{flask.request.method} {flask.request.url}")
# TODO: other pre-request activities can go here.

rp = flask.request.path
if rp != '/' and rp.endswith('/'):
return flask.redirect(rp[:-1])

## Sets up a callback to update headers after the request is processed.
@flask.after_this_request
def update_headers(r: flask.Response) -> flask.Response:
Expand Down Expand Up @@ -378,10 +383,14 @@ def get_navigation_modes(source_name: str | None = None, identifier: str | None
source_name = source_name.lower()

# verify that the source exists:
try:
_ = NLDI_API.sources.get_by_id(source_name)
except KeyError:
return flask.Response(status=http.HTTPStatus.NOT_FOUND, response="Source not found: {source_name}")
if source_name == "comid":
pass
else:
try:
_ = NLDI_API.sources.get_by_id(source_name)
except KeyError:
LOGGER.error(f"Failed lookup for {source_name=} / {identifier=}")
return flask.Response(status=http.HTTPStatus.NOT_FOUND, response=f"Source not found: {source_name}")

nav_url = util.url_join(NLDI_API.base_url, "linked-data", source_name, identifier, "navigation")
content = {
Expand Down Expand Up @@ -410,10 +419,13 @@ def get_navigation_info(source_name: str | None = None, identifier: str | None =
source_name = source_name.lower()

# verify that the source exists:
try:
_ = NLDI_API.sources.get_by_id(source_name)
except KeyError:
return flask.Response(status=http.HTTPStatus.NOT_FOUND, response="Source not found: {source_name}")
if source_name == "comid":
pass
else:
try:
_ = NLDI_API.sources.get_by_id(source_name)
except KeyError:
return flask.Response(status=http.HTTPStatus.NOT_FOUND, response=f"Source not found: {source_name}")

nav_url = util.url_join(NLDI_API.base_url, "linked-data", source_name, identifier, "navigation")

Expand Down Expand Up @@ -623,7 +635,7 @@ def app_factory(api: API) -> flask.Flask:

app.url_map.strict_slashes = False
CORS(app)
app.register_blueprint(ROOT, url_prefix="/api/nldi")
app.register_blueprint(ROOT, url_prefix=CONFIG["server"]["prefix"])

## This is the app we'll be serving...
return app
Expand Down
4 changes: 2 additions & 2 deletions tests/data/sources_config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
server:
url: ${NLDI_URL}
prefix: ${NLDI_PATH}
pretty_print: false
data:
host: ${NLDI_DB_HOST}
Expand All @@ -9,8 +10,7 @@ server:
password: ${NLDI_DB_PASSWORD}

logging:
level: ERROR
logfile: /tmp/nldi.log
level: DEBUG

pygeoapi_url: https://labs.waterdata.usgs.gov/api/nldi/pygeoapi

Expand Down

0 comments on commit 3eb0be7

Please sign in to comment.