Skip to content

Commit 7bf8ff8

Browse files
author
doublebyte1
committed
- initial boiler plate for Styles API
1 parent deb043f commit 7bf8ff8

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

pygeoapi/api/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,17 @@ def all_apis() -> dict:
133133
"""
134134

135135
from . import (coverages, environmental_data_retrieval, itemtypes, maps,
136-
processes, tiles, stac)
136+
processes, styles, tiles, stac)
137137

138138
return {
139139
'coverage': coverages,
140140
'edr': environmental_data_retrieval,
141141
'itemtypes': itemtypes,
142142
'map': maps,
143143
'process': processes,
144+
'style': styles,
144145
'tile': tiles,
145-
'stac': stac
146+
'stac': stac,
146147
}
147148

148149

pygeoapi/api/styles.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# =================================================================
2+
3+
# Authors: Joana Simoes <jo@doublebyte.net>
4+
#
5+
# Copyright (c) 2024 Joana Simoes
6+
#
7+
# Permission is hereby granted, free of charge, to any person
8+
# obtaining a copy of this software and associated documentation
9+
# files (the "Software"), to deal in the Software without
10+
# restriction, including without limitation the rights to use,
11+
# copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
# copies of the Software, and to permit persons to whom the
13+
# Software is furnished to do so, subject to the following
14+
# conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be
17+
# included in all copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24+
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26+
# OTHER DEALINGS IN THE SOFTWARE.
27+
#
28+
# =================================================================
29+
30+
31+
import logging
32+
from http import HTTPStatus
33+
from typing import Tuple
34+
35+
from pygeoapi.util import to_json
36+
37+
from . import APIRequest, API, F_JSON, SYSTEM_LOCALE
38+
39+
LOGGER = logging.getLogger(__name__)
40+
41+
CONFORMANCE_CLASSES = [
42+
'http://www.opengis.net/spec/ogcapi-styles-1/0.0/conf/core',
43+
'http://www.opengis.net/spec/ogcapi-styles-1/0.0/conf/html',
44+
'http://www.opengis.net/spec/ogcapi-styles-1/0.0/conf/mapbox-style'
45+
]
46+
47+
48+
def get_styles(api: API, request: APIRequest) -> Tuple[dict, int, str]:
49+
"""
50+
Fetches the set of styles available.
51+
For each style it returns the id, a title, links to the stylesheet of the style in each supported encoding,
52+
and the link to the metadata.
53+
54+
:param request: A request object
55+
56+
:returns: tuple of headers, status code, content
57+
"""
58+
59+
format_ = request.format or F_JSON
60+
61+
# Force response content type and language (en-US only) headers
62+
headers = request.get_response_headers(SYSTEM_LOCALE, **api.api_headers)
63+
64+
# TODO: implement this
65+
66+
data = '{"styles": [{"title": "night", "id": "night"}]}'
67+
68+
if format_ == F_JSON:
69+
headers['Content-Type'] = 'application/json'
70+
return headers, HTTPStatus.OK, to_json(data, api.pretty_print)
71+
else:
72+
return api.get_format_exception(request)
73+
74+
75+
def get_oas_30(cfg: dict, locale: str) -> tuple[list[dict[str, str]], dict[str, dict]]: # noqa
76+
"""
77+
Get OpenAPI fragments
78+
79+
:param cfg: `dict` of configuration
80+
:param locale: `str` of locale
81+
82+
:returns: `tuple` of `list` of tag objects, and `dict` of path objects
83+
"""
84+
85+
from pygeoapi.openapi import OPENAPI_YAML
86+
87+
paths = {}
88+
89+
paths['/styles'] = {
90+
'get': {
91+
'summary': 'lists the available styles',
92+
'description': 'This operation fetches the set of styles available.',
93+
'tags': ['Discover and fetch styles'],
94+
'operationId': 'getStyles',
95+
'externalDocs': {
96+
'description': 'The specification that describes this operation: OGC API - Styles (DRAFT)',
97+
'url': 'https://docs.ogc.org/DRAFTS/20-009.html'
98+
},
99+
'parameters': [
100+
{'$ref': '#/components/parameters/access_token'},
101+
{'$ref': '#/components/parameters/fStyles'}
102+
],
103+
'responses': {
104+
'200': {'$ref': f"{OPENAPI_YAML['oapif-1']}#/components/responses/Features"}, # noqa
105+
'400': {'$ref': f"{OPENAPI_YAML['oapif-1']}#/components/responses/InvalidParameter"}, # noqa
106+
# TODO: add 406
107+
'500': {'$ref': f"{OPENAPI_YAML['oapif-1']}#/components/responses/ServerError"} # noqa
108+
}
109+
}
110+
}
111+
112+
return [{'name': 'styles'}], {'paths': paths}

pygeoapi/flask_app.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import pygeoapi.api.processes as processes_api
4646
import pygeoapi.api.stac as stac_api
4747
import pygeoapi.api.tiles as tiles_api
48+
import pygeoapi.api.styles as styles_api
4849
from pygeoapi.openapi import load_openapi_document
4950
from pygeoapi.config import get_config
5051
from pygeoapi.util import get_mimetype, get_api_rules
@@ -522,6 +523,14 @@ def get_collection_edr_query(collection_id, instance_id=None,
522523
skip_valid_check=True,
523524
)
524525

526+
@BLUEPRINT.route('/styles')
527+
def styles_api_my_function():
528+
"""
529+
Get Styles endpoint
530+
:returns: HTTP response
531+
"""
532+
533+
return execute_from_flask(styles_api.get_styles, request)
525534

526535
@BLUEPRINT.route('/stac')
527536
def stac_catalog_root():

0 commit comments

Comments
 (0)