-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement unique valid values endpoint
- Loading branch information
Showing
6 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"""handlers/valid_values.py | ||
Handle /valid_values API endpoint. | ||
""" | ||
|
||
from typing import Dict, Mapping, List, Union | ||
|
||
from terracotta import get_settings, get_driver | ||
from terracotta.profile import trace | ||
|
||
|
||
@trace('valid_values_handler') | ||
def valid_values(some_keys: Mapping[str, Union[str, List[str]]] = None) -> Dict[str, List[str]]: | ||
"""List all available valid values""" | ||
settings = get_settings() | ||
driver = get_driver(settings.DRIVER_PATH, provider=settings.DRIVER_PROVIDER) | ||
|
||
with driver.connect(): | ||
valid_values = driver.get_valid_values(some_keys or {}) | ||
|
||
return valid_values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""server/valid_values.py | ||
Flask route to handle /valid_values calls. | ||
""" | ||
|
||
from typing import Any, Dict, List, Union | ||
from flask import request, jsonify, Response | ||
from marshmallow import Schema, fields, INCLUDE, post_load | ||
import re | ||
|
||
from terracotta.server.flask_api import METADATA_API | ||
|
||
|
||
class KeyValueOptionSchema(Schema): | ||
class Meta: | ||
unknown = INCLUDE | ||
|
||
# placeholder values to document keys | ||
key1 = fields.String(example='value1', description='Value of key1', dump_only=True) | ||
key2 = fields.String(example='value2', description='Value of key2', dump_only=True) | ||
|
||
@post_load | ||
def list_items(self, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Union[str, List[str]]]: | ||
# Create lists of values supplied as stringified lists | ||
for key, value in data.items(): | ||
if isinstance(value, str) and re.match(r'^\[.*\]$', value): | ||
data[key] = value[1:-1].split(',') | ||
return data | ||
|
||
|
||
@METADATA_API.route('/valid_values', methods=['GET']) | ||
def get_valid_values() -> Response: | ||
"""Get all valid values combinations (possibly when given a value for some keys) | ||
--- | ||
get: | ||
summary: /datasets | ||
description: | ||
Get keys of all available datasets that match given key constraint. | ||
Constraints may be combined freely. Returns all known datasets if no query parameters | ||
are given. | ||
parameters: | ||
- in: query | ||
schema: DatasetOptionSchema | ||
responses: | ||
200: | ||
description: All available key combinations | ||
schema: | ||
type: array | ||
items: DatasetSchema | ||
400: | ||
description: Query parameters contain unrecognized keys | ||
""" | ||
from terracotta.handlers.valid_values import valid_values | ||
option_schema = KeyValueOptionSchema() | ||
options = option_schema.load(request.args) | ||
|
||
keys = options or None | ||
|
||
payload = { | ||
'valid_values': valid_values(keys) | ||
} | ||
|
||
return jsonify(payload) |