Skip to content
Next Next commit
Refactor landing_page api to new style
  • Loading branch information
totycro committed Nov 5, 2024
commit 47e7c44cb98c053748ee138889102c0e6628354c
244 changes: 120 additions & 124 deletions pygeoapi/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,130 +685,6 @@ def __init__(self, config, openapi):
self.manager = get_manager(self.config)
LOGGER.info('Process manager plugin loaded')

@gzip
@pre_process
@jsonldify
def landing_page(self,
request: Union[APIRequest, Any]) -> Tuple[dict, int, str]:
"""
Provide API landing page

:param request: A request object

:returns: tuple of headers, status code, content
"""

if not request.is_valid():
return self.get_format_exception(request)

fcm = {
'links': [],
'title': l10n.translate(
self.config['metadata']['identification']['title'],
request.locale),
'description':
l10n.translate(
self.config['metadata']['identification']['description'],
request.locale)
}

LOGGER.debug('Creating links')
# TODO: put title text in config or translatable files?
fcm['links'] = [{
'rel': 'about',
'type': 'text/html',
'title': l10n.translate(
self.config['metadata']['identification']['title'],
request.locale),
'href': self.config['metadata']['identification']['url']
}, {
'rel': request.get_linkrel(F_JSON),
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('This document as JSON', request.locale),
'href': f"{self.base_url}?f={F_JSON}"
}, {
'rel': request.get_linkrel(F_JSONLD),
'type': FORMAT_TYPES[F_JSONLD],
'title': l10n.translate('This document as RDF (JSON-LD)', request.locale), # noqa
'href': f"{self.base_url}?f={F_JSONLD}"
}, {
'rel': request.get_linkrel(F_HTML),
'type': FORMAT_TYPES[F_HTML],
'title': l10n.translate('This document as HTML', request.locale),
'href': f"{self.base_url}?f={F_HTML}",
'hreflang': self.default_locale
}, {
'rel': 'service-desc',
'type': 'application/vnd.oai.openapi+json;version=3.0',
'title': l10n.translate('The OpenAPI definition as JSON', request.locale), # noqa
'href': f"{self.base_url}/openapi"
}, {
'rel': 'service-doc',
'type': FORMAT_TYPES[F_HTML],
'title': l10n.translate('The OpenAPI definition as HTML', request.locale), # noqa
'href': f"{self.base_url}/openapi?f={F_HTML}",
'hreflang': self.default_locale
}, {
'rel': 'conformance',
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('Conformance', request.locale),
'href': f"{self.base_url}/conformance"
}, {
'rel': 'data',
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('Collections', request.locale),
'href': self.get_collections_url()
}, {
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/processes',
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('Processes', request.locale),
'href': f"{self.base_url}/processes"
}, {
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/job-list',
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('Jobs', request.locale),
'href': f"{self.base_url}/jobs"
}, {
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/tiling-schemes',
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('The list of supported tiling schemes as JSON', request.locale), # noqa
'href': f"{self.base_url}/TileMatrixSets?f=json"
}, {
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/tiling-schemes',
'type': FORMAT_TYPES[F_HTML],
'title': l10n.translate('The list of supported tiling schemes as HTML', request.locale), # noqa
'href': f"{self.base_url}/TileMatrixSets?f=html"
}]

headers = request.get_response_headers(**self.api_headers)
if request.format == F_HTML: # render

fcm['processes'] = False
fcm['stac'] = False
fcm['collection'] = False

if filter_dict_by_key_value(self.config['resources'],
'type', 'process'):
fcm['processes'] = True

if filter_dict_by_key_value(self.config['resources'],
'type', 'stac-collection'):
fcm['stac'] = True

if filter_dict_by_key_value(self.config['resources'],
'type', 'collection'):
fcm['collection'] = True

content = render_j2_template(self.tpl_config, 'landing_page.html',
fcm, request.locale)
return headers, HTTPStatus.OK, content

if request.format == F_JSONLD:
return headers, HTTPStatus.OK, to_json(
self.fcmld, self.pretty_print)

return headers, HTTPStatus.OK, to_json(fcm, self.pretty_print)

@gzip
@pre_process
def openapi_(self, request: Union[APIRequest, Any]) -> Tuple[
Expand Down Expand Up @@ -1544,6 +1420,126 @@ def _set_content_crs_header(
headers['Content-Crs'] = f'<{content_crs_uri}>'


@jsonldify
def landing_page(api: API,
request: APIRequest) -> Tuple[dict, int, str]:
"""
Provide API landing page

:param request: A request object

:returns: tuple of headers, status code, content
"""

fcm = {
'links': [],
'title': l10n.translate(
api.config['metadata']['identification']['title'],
request.locale),
'description':
l10n.translate(
api.config['metadata']['identification']['description'],
request.locale)
}

LOGGER.debug('Creating links')
# TODO: put title text in config or translatable files?
fcm['links'] = [{
'rel': 'about',
'type': 'text/html',
'title': l10n.translate(
api.config['metadata']['identification']['title'],
request.locale),
'href': api.config['metadata']['identification']['url']
}, {
'rel': request.get_linkrel(F_JSON),
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('This document as JSON', request.locale),
'href': f"{api.base_url}?f={F_JSON}"
}, {
'rel': request.get_linkrel(F_JSONLD),
'type': FORMAT_TYPES[F_JSONLD],
'title': l10n.translate('This document as RDF (JSON-LD)', request.locale), # noqa
'href': f"{api.base_url}?f={F_JSONLD}"
}, {
'rel': request.get_linkrel(F_HTML),
'type': FORMAT_TYPES[F_HTML],
'title': l10n.translate('This document as HTML', request.locale),
'href': f"{api.base_url}?f={F_HTML}",
'hreflang': api.default_locale
}, {
'rel': 'service-desc',
'type': 'application/vnd.oai.openapi+json;version=3.0',
'title': l10n.translate('The OpenAPI definition as JSON', request.locale), # noqa
'href': f"{api.base_url}/openapi"
}, {
'rel': 'service-doc',
'type': FORMAT_TYPES[F_HTML],
'title': l10n.translate('The OpenAPI definition as HTML', request.locale), # noqa
'href': f"{api.base_url}/openapi?f={F_HTML}",
'hreflang': api.default_locale
}, {
'rel': 'conformance',
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('Conformance', request.locale),
'href': f"{api.base_url}/conformance"
}, {
'rel': 'data',
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('Collections', request.locale),
'href': api.get_collections_url()
}, {
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/processes',
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('Processes', request.locale),
'href': f"{api.base_url}/processes"
}, {
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/job-list',
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('Jobs', request.locale),
'href': f"{api.base_url}/jobs"
}, {
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/tiling-schemes',
'type': FORMAT_TYPES[F_JSON],
'title': l10n.translate('The list of supported tiling schemes as JSON', request.locale), # noqa
'href': f"{api.base_url}/TileMatrixSets?f=json"
}, {
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/tiling-schemes',
'type': FORMAT_TYPES[F_HTML],
'title': l10n.translate('The list of supported tiling schemes as HTML', request.locale), # noqa
'href': f"{api.base_url}/TileMatrixSets?f=html"
}]

headers = request.get_response_headers(**api.api_headers)
if request.format == F_HTML: # render

fcm['processes'] = False
fcm['stac'] = False
fcm['collection'] = False

if filter_dict_by_key_value(api.config['resources'],
'type', 'process'):
fcm['processes'] = True

if filter_dict_by_key_value(api.config['resources'],
'type', 'stac-collection'):
fcm['stac'] = True

if filter_dict_by_key_value(api.config['resources'],
'type', 'collection'):
fcm['collection'] = True

content = render_j2_template(api.tpl_config, 'landing_page.html',
fcm, request.locale)
return headers, HTTPStatus.OK, content

if request.format == F_JSONLD:
return headers, HTTPStatus.OK, to_json(
api.fcmld, api.pretty_print)

return headers, HTTPStatus.OK, to_json(fcm, api.pretty_print)


def validate_bbox(value=None) -> list:
"""
Helper function to validate bbox parameter
Expand Down
6 changes: 2 additions & 4 deletions pygeoapi/django_/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from django.http import HttpRequest, HttpResponse

from pygeoapi.api import API, APIRequest, apply_gzip
import pygeoapi.api as core_api
import pygeoapi.api.coverages as coverages_api
import pygeoapi.api.environmental_data_retrieval as edr_api
import pygeoapi.api.itemtypes as itemtypes_api
Expand All @@ -59,10 +60,7 @@ def landing_page(request: HttpRequest) -> HttpResponse:
:returns: Django HTTP Response
"""

response_ = _feed_response(request, 'landing_page')
response = _to_django_response(*response_)

return response
return execute_from_django(core_api.landing_page, request)


def openapi(request: HttpRequest) -> HttpResponse:
Expand Down
4 changes: 2 additions & 2 deletions pygeoapi/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
send_from_directory, Response, Request)

from pygeoapi.api import API, APIRequest, apply_gzip
import pygeoapi.api as core_api
import pygeoapi.api.coverages as coverages_api
import pygeoapi.api.environmental_data_retrieval as edr_api
import pygeoapi.api.itemtypes as itemtypes_api
Expand Down Expand Up @@ -159,7 +160,6 @@ def execute_from_flask(api_function, request: Request, *args,
else:
headers, status, content = api_function(api_, api_request, *args)
content = apply_gzip(headers, content)
# handle jsonld too?

return get_response((headers, status, content))

Expand All @@ -171,7 +171,7 @@ def landing_page():

:returns: HTTP response
"""
return get_response(api_.landing_page(request))
return execute_from_flask(core_api.landing_page, request)


@BLUEPRINT.route('/openapi')
Expand Down
3 changes: 2 additions & 1 deletion pygeoapi/starlette_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import uvicorn

from pygeoapi.api import API, APIRequest, apply_gzip
import pygeoapi.api as core_api
import pygeoapi.api.coverages as coverages_api
import pygeoapi.api.environmental_data_retrieval as edr_api
import pygeoapi.api.itemtypes as itemtypes_api
Expand Down Expand Up @@ -167,7 +168,7 @@ async def landing_page(request: Request):

:returns: Starlette HTTP Response
"""
return await get_response(api_.landing_page, request)
return await execute_from_starlette(core_api.landing_page, request)


async def openapi(request: Request):
Expand Down
Loading