From 0a0b88e367fc05e117a27e8c46601b97e195b013 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sabine=20Schr=C3=B6der?= <s.schroeder@fz-juelich.de>
Date: Wed, 2 Sep 2020 15:58:28 +0200
Subject: [PATCH] #11 and #3: added more readable REST API for
 controlled_vocabulary (and also for requests of only one entry))

---
 CI/prepare_documentation_precommit.sh |   2 +-
 docs/toardb_fastapi.md                | 149 +++++++++++++++++++-------
 toardb/toardb.py                      |  62 +++++++----
 3 files changed, 154 insertions(+), 59 deletions(-)

diff --git a/CI/prepare_documentation_precommit.sh b/CI/prepare_documentation_precommit.sh
index e653861..1bd9b2d 100755
--- a/CI/prepare_documentation_precommit.sh
+++ b/CI/prepare_documentation_precommit.sh
@@ -8,7 +8,7 @@ python3 prepare_openapi_json.py toardb_fastapi.yaml
 openapi-markdown --skip-info -i sample.json
 
 # also get controlled_vocabulary
-curl "http://127.0.0.1:8000/controlled_vocabulary" >controlled_vocabulary.json
+curl "http://127.0.0.1:8000/controlled_vocabulary/" >controlled_vocabulary.json
 # giving credit to torsimany (although it was not working, it was a good starting point)
 torsimany controlled_vocabulary.json
 # some more pretty printing for the "tables"
diff --git a/docs/toardb_fastapi.md b/docs/toardb_fastapi.md
index 2ed6e9b..edf375e 100644
--- a/docs/toardb_fastapi.md
+++ b/docs/toardb_fastapi.md
@@ -64,11 +64,11 @@
 | state | string |  | Yes |
 | coordinate_validation_status | string |  | Yes |
 | coordinate_validation_date | dateTime |  | Yes |
+| coordinate_validator_id | integer |  | Yes |
 | type_of_environment | string |  | Yes |
 | type_of_area | string |  | Yes |
 | timezone | string |  | Yes |
 | additional_metadata | string (json-string) |  | No |
-| coordinate_validator_id | integer |  | Yes |
 | roles | [ [StationmetaRole](#stationmetarole) ] |  | No |
 | annotations | [ [StationmetaAnnotation](#stationmetaannotation) ] |  | No |
 | aux_images | [ [StationmetaAuxImage](#stationmetaauximage) ] |  | No |
@@ -131,11 +131,29 @@
 | state | string |  | Yes |
 | coordinate_validation_status | string |  | Yes |
 | coordinate_validation_date | dateTime |  | Yes |
+| coordinate_validator_id | integer |  | Yes |
 | type_of_environment | string |  | Yes |
 | type_of_area | string |  | Yes |
 | timezone | string |  | Yes |
 | additional_metadata | string (json-string) |  | No |
+
+#### StationmetaCoreBase
+
+| Name | Type | Description | Required |
+| ---- | ---- | ----------- | -------- |
+| id | integer |  | No |
+| codes | [ string ] | list of station's codes | Yes |
+| name | string |  | Yes |
+| coordinates | [Coordinates](#coordinates) |  | Yes |
+| country | string |  | Yes |
+| state | string |  | Yes |
+| coordinate_validation_status | string |  | Yes |
+| coordinate_validation_date | dateTime |  | Yes |
 | coordinate_validator_id | integer |  | Yes |
+| type_of_environment | string |  | Yes |
+| type_of_area | string |  | Yes |
+| timezone | string |  | Yes |
+| additional_metadata | string (json-string) |  | No |
 
 #### StationmetaGlobal
 
@@ -167,7 +185,7 @@
 | id | integer |  | Yes |
 | role | string |  | Yes |
 | status | string |  | Yes |
-| contact_id | integer |  | Yes |
+| contact | [Contact](#contact) |  | Yes |
 
 #### Timeseries
 
@@ -190,7 +208,7 @@
 | roles | [ [TimeseriesRole](#timeseriesrole) ] |  | No |
 | annotations | [ [TimeseriesAnnotation](#timeseriesannotation) ] |  | No |
 | variable | [Variable](#variable) |  | Yes |
-| station |  |  | No |
+| station | [StationmetaCoreBase](#stationmetacorebase) |  | Yes |
 | programme | [TimeseriesProgramme](#timeseriesprogramme) |  | Yes |
 
 #### TimeseriesAnnotation
@@ -222,7 +240,7 @@
 | id | integer |  | Yes |
 | role | string |  | Yes |
 | status | string |  | Yes |
-| contact_id | integer |  | Yes |
+| contact | [Contact](#contact) |  | Yes |
 
 #### Variable
 
@@ -238,21 +256,21 @@
 
 ## controlled vocabulary
 
-#### Rc_Vocabulary ####
-  * RC_vocabulary: [  
+#### Role Codes ####
+  * Role Codes: [  
  [0, 'PointOfContact', 'point of contact'],  
  [1, 'PrincipalInvestigator', 'principal investigator'],  
  [2, 'Originator', 'originator'],  
  [3, 'Contributor', 'contributor'],  
  [4, 'Collaborator', 'collaborator'],  
  [5, 'ResourceProvider', 'resource provider']]
-#### Rs_Vocabulary ####
-  * RS_vocabulary: [  
+#### Role Status ####
+  * Role Status: [  
  [0, 'active', 'active'],  
  [1, 'inactive', 'inactive'],  
  [2, 'unknown', 'unknown']]
-#### Ok_Vocabulary ####
-  * OK_vocabulary: [  
+#### Kind Of Organizations ####
+  * Kind of Organizations: [  
  [1, 'Government', 'government'],  
  [2, 'Research', 'research'],  
  [3, 'International', 'international'],  
@@ -260,13 +278,13 @@
  [5, 'Commercial', 'commercial'],  
  [6, 'Individual', 'individual'],  
  [7, 'Other', 'other']]
-#### Da_Vocabulary ####
-  * DA_vocabulary: [  
+#### Data Access Rights ####
+  * Data Access Rights: [  
  [0, 'ByAttribution', 'by attribution'],  
  [1, 'ShareAlike', 'share alike'],  
  [2, 'Restricted', 'restricted']]
-#### Sf_Vocabulary ####
-  * SF_vocabulary: [  
+#### Sampling Frequencies ####
+  * Sampling Frequencies: [  
  [0, 'Hourly', 'hourly'],  
  [1, 'ThreeHourly', '3-hourly'],  
  [2, 'SixHourly', '6-hourly'],  
@@ -276,8 +294,8 @@
  [6, 'Yearly', 'yearly'],  
  [7, 'Irregular', 'irregular data samples of constant length'],  
  [8, 'Irregular2', 'irregular data samples of varying length']]
-#### At_Vocabulary ####
-  * AT_vocabulary: [  
+#### Aggregation Types ####
+  * Aggregation Types: [  
  [0, 'Mean', 'mean'],  
  [1, 'Mean1Of2', 'mean of two values'],  
  [2, 'Mean1OfWeek', 'weekly mean'],  
@@ -285,16 +303,16 @@
  [4, 'MeanMonth', 'monthly mean'],  
  [5, 'None', 'none'],  
  [6, 'Unknown', 'unknown']]
-#### Ds_Vocabulary ####
-  * DS_vocabulary: [  
+#### Data Sources ####
+  * Data Sources: [  
  [0, 'Model', 'model'],  
  [1, 'Measurement', 'measurement']]
-#### Mm_Vocabulary ####
-  * MM_vocabulary: [  
+#### Measurement Methods ####
+  * Measurement Methods: [  
  [0, 'UVAbsorption', 'UV absorption'],  
  [1, 'UnknownInstrument', 'unknown instrument']]
-#### Cz_Vocabulary ####
-  * CZ_vocabulary: [  
+#### Climatic Zones ####
+  * Climatic Zones: [  
  [-1, 'Undefined', 'undefined'],  
  [0, 'Unclassified', 'unclassified'],  
  [1, 'WarmTemperateMoist', 'warm temperate moist'],  
@@ -309,36 +327,36 @@
  [10, 'TropicalWet', 'tropical wet'],  
  [11, 'TropicalMoist', 'tropical moist'],  
  [12, 'TropicalDry', 'tropical dry']]
-#### Cv_Vocabulary ####
-  * CV_vocabulary: [  
+#### Coordinate Validity ####
+  * Coordinate Validity: [  
  [0, 'NotChecked', 'not checked'],  
  [1, 'Verified', 'verified'],  
  [2, 'Plausible', 'plausible'],  
  [3, 'Doubtful', 'doubtful'],  
  [4, 'Unverifyable', 'not verifyable']]
-#### St_Vocabulary ####
-  * ST_vocabulary: [  
+#### Station Types ####
+  * Station Types: [  
  [0, 'Unknown', 'unknown'],  
  [1, 'Background', 'background'],  
  [2, 'Traffic', 'traffic'],  
  [3, 'Industrial', 'industrial'],  
  [4, 'Other', 'other']]
-#### Ta_Vocabulary ####
-  * TA_vocabulary: [  
+#### Station Type Of Area ####
+  * Station Type of Area: [  
  [0, 'Unknown', 'unknown'],  
  [1, 'Urban', 'urban'],  
  [2, 'Suburban', 'suburban'],  
  [3, 'Rural', 'rural'],  
  [4, 'Remote', 'remote']]
-#### Tc_Vocabulary ####
-  * TC_vocabulary: [  
+#### Station Toar Categories ####
+  * Station TOAR Categories: [  
  [-1, 'Unknown', 'unknown'],  
  [0, 'Unclassified', 'unclassified'],  
  [1, 'RuralLowElevation', 'rural low elevation'],  
  [2, 'RuralHighElevation', 'rural high elevation'],  
  [3, 'Urban', 'urban']]
-#### Tr_Vocabulary ####
-  * TR_vocabulary: [  
+#### Station Htap Regions ####
+  * Station HTAP Regions: [  
  [-1, 'HTAPTier1Undefined', '-1 (undefined)'],  
  [1, 'HTAPTier1World', '1 (World)'],  
  [2, 'HTAPTier1OCN', '2 (OCN Non-arctic/Antarctic Ocean)'],  
@@ -357,8 +375,8 @@
  [15, 'HTAPTier1CAS', '15 (CAS Central Asia)'],  
  [16, 'HTAPTier1NPO', '16 (NPO Arctic Circle (North of 66 N) + Greenland)'],  
  [17, 'HTAPTier1SPO', '17 (SPO Antarctic)']]
-#### Dl_Vocabulary ####
-  * DL_vocabulary: [  
+#### Station Dominant Landcover Types ####
+  * Station Dominant Landcover Types: [  
  [-1, 'Undefined', '-1 (undefined)'],  
  [0, 'Water', '0 (Water)'],  
  [1, 'EGNeedleleaf', '1  (Evergreen Needleleaf forest)'],  
@@ -378,13 +396,13 @@
  [15, 'Snow', '15  (Snow and ice)'],  
  [16, 'Barren', '16  (Barren or sparsely vegetated)'],  
  [255, 'Fill', '255 (Fill Value/Unclassified)']]
-#### Rt_Vocabulary ####
-  * RT_vocabulary: [  
+#### Result Types ####
+  * Result Types: [  
  [0, 'String', 'str'],  
  [1, 'Integer', 'int'],  
  [2, 'Float', 'float']]
-#### Df_Vocabulary ####
-  * DF_vocabulary: [  
+#### Data Flags ####
+  * Data Flags: [  
  [0, 'OK', 'OK'],  
  [1, 'OKPreliminary', 'OK preliminary'],  
  [2, 'OKModified', 'OK modified'],  
@@ -405,18 +423,71 @@
 
 ## REST API
 
-### /controlled_vocabulary
+### /controlled_vocabulary/
+
+#### GET
+##### Summary:
+
+Info
+
+##### Responses
+
+| Code | Description |
+| ---- | ----------- |
+| 200 | Successful Response |
+
+### /controlled_vocabulary/{name}
+
+#### GET
+##### Summary:
+
+Info
+
+##### Parameters
+
+| Name | Located in | Description | Required | Schema |
+| ---- | ---------- | ----------- | -------- | ---- |
+| name | path |  | Yes | string |
+
+##### Responses
+
+| Code | Description |
+| ---- | ----------- |
+| 200 | Successful Response |
+| 422 | Validation Error |
+
+### /database_statistics/
+
+#### GET
+##### Summary:
+
+Info
+
+##### Responses
+
+| Code | Description |
+| ---- | ----------- |
+| 200 | Successful Response |
+
+### /database_statistics/{name}
 
 #### GET
 ##### Summary:
 
 Info
 
+##### Parameters
+
+| Name | Located in | Description | Required | Schema |
+| ---- | ---------- | ----------- | -------- | ---- |
+| name | path |  | Yes | string |
+
 ##### Responses
 
 | Code | Description |
 | ---- | ----------- |
 | 200 | Successful Response |
+| 422 | Validation Error |
 
 ### /variables/
 
diff --git a/toardb/toardb.py b/toardb/toardb.py
index 20e0138..01a9446 100644
--- a/toardb/toardb.py
+++ b/toardb/toardb.py
@@ -19,27 +19,51 @@ from pydantic import BaseSettings
 
 app = FastAPI()
 
-@app.get("/controlled_vocabulary")
+@app.get("/controlled_vocabulary/")
 async def info():
-    return {
-        "RC_vocabulary": settings.RC_vocab,
-        "RS_vocabulary": settings.RS_vocab,
-        "OK_vocabulary": settings.OK_vocab,
-        "DA_vocabulary": settings.DA_vocab,
-        "SF_vocabulary": settings.SF_vocab,
-        "AT_vocabulary": settings.AT_vocab,
-        "DS_vocabulary": settings.DS_vocab,
-        "MM_vocabulary": settings.MM_vocab,
-        "CZ_vocabulary": settings.CZ_vocab,
-        "CV_vocabulary": settings.CV_vocab,
-        "ST_vocabulary": settings.ST_vocab,
-        "TA_vocabulary": settings.TA_vocab,
-        "TC_vocabulary": settings.TC_vocab,
-        "TR_vocabulary": settings.TR_vocab,
-        "DL_vocabulary": settings.DL_vocab,
-        "RT_vocabulary": settings.RT_vocab,
-        "DF_vocabulary": settings.DF_vocab,
+    controlled_vocabulary = {
+        "Role Codes": settings.RC_vocab,
+        "Role Status": settings.RS_vocab,
+        "Kind of Organizations": settings.OK_vocab,
+        "Data Access Rights": settings.DA_vocab,
+        "Sampling Frequencies": settings.SF_vocab,
+        "Aggregation Types": settings.AT_vocab,
+        "Data Sources": settings.DS_vocab,
+        "Measurement Methods": settings.MM_vocab,
+        "Climatic Zones": settings.CZ_vocab,
+        "Coordinate Validity": settings.CV_vocab,
+        "Station Types": settings.ST_vocab,
+        "Station Type of Area": settings.TA_vocab,
+        "Station TOAR Categories": settings.TC_vocab,
+        "Station HTAP Regions": settings.TR_vocab,
+        "Station Dominant Landcover Types": settings.DL_vocab,
+        "Result Types": settings.RT_vocab,
+        "Data Flags": settings.DF_vocab,
     }
+    return controlled_vocabulary
+
+@app.get("/controlled_vocabulary/{name}")
+async def info(name: str):
+    controlled_vocabulary = {
+        "Role Codes": settings.RC_vocab,
+        "Role Status": settings.RS_vocab,
+        "Kind of Organizations": settings.OK_vocab,
+        "Data Access Rights": settings.DA_vocab,
+        "Sampling Frequencies": settings.SF_vocab,
+        "Aggregation Types": settings.AT_vocab,
+        "Data Sources": settings.DS_vocab,
+        "Measurement Methods": settings.MM_vocab,
+        "Climatic Zones": settings.CZ_vocab,
+        "Coordinate Validity": settings.CV_vocab,
+        "Station Types": settings.ST_vocab,
+        "Station Type of Area": settings.TA_vocab,
+        "Station TOAR Categories": settings.TC_vocab,
+        "Station HTAP Regions": settings.TR_vocab,
+        "Station Dominant Landcover Types": settings.DL_vocab,
+        "Result Types": settings.RT_vocab,
+        "Data Flags": settings.DF_vocab,
+    }
+    return controlled_vocabulary[name]
 
 db_stats= {
         "users": 358,
-- 
GitLab