38
38
import os
39
39
from pathlib import Path
40
40
from typing import Union
41
-
41
+ import random
42
42
import click
43
43
from jsonschema import validate as jsonschema_validate
44
44
import yaml
@@ -641,7 +641,8 @@ def get_oas_30_parameters(cfg: dict, locale_: str):
641
641
'description' : 'Configuration resource identifier' ,
642
642
'required' : True ,
643
643
'schema' : {
644
- 'type' : 'string'
644
+ 'type' : 'string' ,
645
+ 'default' : 'obs'
645
646
}
646
647
}
647
648
}
@@ -666,6 +667,24 @@ def get_config_schema():
666
667
return yaml_load (fh2 )
667
668
668
669
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
+
669
688
def get_put_config (cfg : dict ) -> dict :
670
689
"""
671
690
Creates the payload for the PUT admin config request
@@ -675,10 +694,17 @@ def get_put_config(cfg: dict) -> dict:
675
694
:returns: dict of OpenAPI definition
676
695
"""
677
696
678
- cfg ['metadata' ]['identification' ]['title' ]['en' ] = 'New pygeoapi Title'
679
- cfg ['metadata' ]['identification' ]['title' ]['fr' ] = 'Nouveau pygeoapi Titre'
697
+ put = deepcopy (cfg )
680
698
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
682
708
683
709
684
710
def get_patch_config (cfg : dict ) -> dict :
@@ -690,10 +716,99 @@ def get_patch_config(cfg: dict) -> dict:
690
716
:returns: dict of OpenAPI definition
691
717
"""
692
718
693
- cfg ['metadata' ]['identification' ]['title' ]['en' ] = 'Patched pygeoapi'
694
- cfg ['resources' ] = {}
719
+ patch = deepcopy (cfg )
695
720
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
697
812
698
813
699
814
def get_admin (cfg : dict ) -> dict :
@@ -801,6 +916,7 @@ def get_admin(cfg: dict) -> dict:
801
916
'description' : 'Adds resource to configuration' ,
802
917
'content' : {
803
918
'application/json' : {
919
+ 'example' : get_post_resource (cfg ),
804
920
'schema' : schema_dict ['properties' ]['resources' ]['patternProperties' ]['^.*$' ] # noqa
805
921
}
806
922
},
@@ -847,6 +963,7 @@ def get_admin(cfg: dict) -> dict:
847
963
'description' : 'Updates admin configuration resource' ,
848
964
'content' : {
849
965
'application/json' : {
966
+ 'example' : get_put_resource (cfg ),
850
967
'schema' : schema_dict ['properties' ]['resources' ]['patternProperties' ]['^.*$' ] # noqa
851
968
}
852
969
},
@@ -870,6 +987,7 @@ def get_admin(cfg: dict) -> dict:
870
987
'description' : 'Updates admin configuration resource' ,
871
988
'content' : {
872
989
'application/json' : {
990
+ 'example' : get_patch_resource (cfg ),
873
991
'schema' : schema_dict ['properties' ]['resources' ]['patternProperties' ]['^.*$' ] # noqa
874
992
}
875
993
},
0 commit comments