Skip to content

Commit 2dcbf85

Browse files
author
doublebyte1
committed
- added openapi example payload for admin API put, patch and post routes
1 parent 6e5bff8 commit 2dcbf85

File tree

1 file changed

+126
-8
lines changed

1 file changed

+126
-8
lines changed

pygeoapi/openapi.py

Lines changed: 126 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import os
3939
from pathlib import Path
4040
from typing import Union
41-
41+
import random
4242
import click
4343
from jsonschema import validate as jsonschema_validate
4444
import yaml
@@ -641,7 +641,8 @@ def get_oas_30_parameters(cfg: dict, locale_: str):
641641
'description': 'Configuration resource identifier',
642642
'required': True,
643643
'schema': {
644-
'type': 'string'
644+
'type': 'string',
645+
'default': 'obs'
645646
}
646647
}
647648
}
@@ -666,6 +667,24 @@ def get_config_schema():
666667
return yaml_load(fh2)
667668

668669

670+
def remove_timestamps(cfg: dict) -> dict:
671+
"""
672+
Removes timestamps from sample configuration, as they are being
673+
translated to strings
674+
675+
:param cfg: `dict` of configuration
676+
677+
:returns: dict of OpenAPI definition
678+
"""
679+
680+
for key in cfg['resources'].keys():
681+
extents = cfg['resources'][key].get('extents')
682+
if extents is not None and 'temporal' in extents.keys():
683+
del extents['temporal']
684+
685+
return cfg
686+
687+
669688
def get_put_config(cfg: dict) -> dict:
670689
"""
671690
Creates the payload for the PUT admin config request
@@ -675,10 +694,17 @@ def get_put_config(cfg: dict) -> dict:
675694
:returns: dict of OpenAPI definition
676695
"""
677696

678-
cfg['metadata']['identification']['title']['en'] = 'New pygeoapi Title'
679-
cfg['metadata']['identification']['title']['fr'] = 'Nouveau pygeoapi Titre'
697+
put = deepcopy(cfg)
680698

681-
return cfg
699+
put['metadata']['identification']['title']['en'] = 'New pygeoapi Title'
700+
put['metadata']['identification']['title']['fr'] = 'Nouveau pygeoapi Titre'
701+
702+
# If there are resources with timestamps, we have to remove the
703+
# timestamps from the configuration.
704+
if 'resources' in put.keys():
705+
remove_timestamps(put)
706+
707+
return put
682708

683709

684710
def get_patch_config(cfg: dict) -> dict:
@@ -690,10 +716,99 @@ def get_patch_config(cfg: dict) -> dict:
690716
:returns: dict of OpenAPI definition
691717
"""
692718

693-
cfg['metadata']['identification']['title']['en'] = 'Patched pygeoapi'
694-
cfg['resources'] = {}
719+
patch = deepcopy(cfg)
695720

696-
return cfg
721+
patch['metadata']['identification']['title']['en'] = 'Patched pygeoapi'
722+
patch['resources'] = {}
723+
724+
return patch
725+
726+
727+
def gen_collection_name():
728+
"""
729+
Generates a collection name, based on a list.
730+
Inspired by moby/docker:
731+
https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go
732+
733+
:returns: collection name
734+
"""
735+
736+
names = ['agnesi', 'allen', 'almeida', 'antonelli',
737+
'austin', 'borg', 'clark']
738+
739+
return random.choice(names)
740+
741+
742+
def get_post_resource(cfg: dict) -> dict:
743+
"""
744+
Creates the payload for the POST resource admin request
745+
746+
:param cfg: `dict` of configuration
747+
748+
:returns: dict of OpenAPI definition
749+
"""
750+
751+
collection = gen_collection_name()
752+
753+
if len(cfg['resources']) < 1 or cfg['resources']['obs'] is None:
754+
return ''
755+
756+
post = {collection: {}}
757+
post[collection] = deepcopy(cfg['resources']['obs'])
758+
759+
if 'temporal' in post[collection]['extents'].keys():
760+
del post[collection]['extents']['temporal']
761+
762+
post[collection]['title'] = 'More observations'
763+
post[collection]['description'] = 'More cool observations'
764+
765+
return post
766+
767+
768+
def get_put_resource(cfg: dict) -> dict:
769+
"""
770+
Creates the payload for the PUT resource admin request
771+
772+
:param cfg: `dict` of configuration
773+
774+
:returns: dict of OpenAPI definition
775+
"""
776+
777+
if len(cfg['resources']) < 1 or cfg['resources']['obs'] is None:
778+
return ''
779+
780+
put = deepcopy(cfg['resources']['obs'])
781+
782+
if 'temporal' in put['extents'].keys():
783+
del put['extents']['temporal']
784+
785+
put['title'] = 'New observations'
786+
put['description'] = 'New observations description'
787+
788+
return put
789+
790+
791+
def get_patch_resource(cfg: dict) -> dict:
792+
"""
793+
Creates the payload for the PATCH resource admin request
794+
795+
:param cfg: `dict` of configuration
796+
797+
:returns: dict of OpenAPI definition
798+
"""
799+
800+
if len(cfg['resources']) < 1 or cfg['resources']['obs'] is None:
801+
return ''
802+
803+
patch = deepcopy(cfg['resources']['obs'])
804+
805+
if 'temporal' in patch['extents'].keys():
806+
del patch['extents']['temporal']
807+
808+
patch['title'] = 'Patched collection title'
809+
del patch['providers']
810+
811+
return patch
697812

698813

699814
def get_admin(cfg: dict) -> dict:
@@ -801,6 +916,7 @@ def get_admin(cfg: dict) -> dict:
801916
'description': 'Adds resource to configuration',
802917
'content': {
803918
'application/json': {
919+
'example': get_post_resource(cfg),
804920
'schema': schema_dict['properties']['resources']['patternProperties']['^.*$'] # noqa
805921
}
806922
},
@@ -847,6 +963,7 @@ def get_admin(cfg: dict) -> dict:
847963
'description': 'Updates admin configuration resource',
848964
'content': {
849965
'application/json': {
966+
'example': get_put_resource(cfg),
850967
'schema': schema_dict['properties']['resources']['patternProperties']['^.*$'] # noqa
851968
}
852969
},
@@ -870,6 +987,7 @@ def get_admin(cfg: dict) -> dict:
870987
'description': 'Updates admin configuration resource',
871988
'content': {
872989
'application/json': {
990+
'example': get_patch_resource(cfg),
873991
'schema': schema_dict['properties']['resources']['patternProperties']['^.*$'] # noqa
874992
}
875993
},

0 commit comments

Comments
 (0)