Browse Source

Add a new endpoint for looking up valid countries

Kirk Trombley 4 years ago
parent
commit
3a651f4b08
4 changed files with 47 additions and 9 deletions
  1. 10 1
      README.md
  2. 12 3
      server/app/api/other.py
  3. 19 4
      server/app/point_gen/__init__.py
  4. 6 1
      server/app/schemas.py

+ 10 - 1
README.md

@@ -36,12 +36,21 @@ GET /caches
             }, ...
         ]
     }
+GET /generators
+    Returns 404 vs 200 and {
+        "generators": [
+            {
+                "generationMethod": string,
+                "countryLocks": [ string ],
+            }
+        ]
+    }
 PUT /game
     Accepts {
         "timer": number,
         "rounds": number,
         "countryLock": string || null (default: null),
-        "generationMethod": string (default: "MAPCRUNCH"),
+        "generationMethod": string (default: "RANDOMSTREETVIEW"),
         "ruleSet": string (default: "NORMAL")
     }
     Returns 501 vs 200 and {

+ 12 - 3
server/app/api/other.py

@@ -5,8 +5,8 @@ from fastapi_camelcase import CamelModel
 from pydantic import confloat
 
 from .. import scoring
-from ..schemas import CacheInfo
-from ..point_gen import get_cache_info
+from ..schemas import CacheInfo, GeneratorInfo
+from ..point_gen import get_cache_info, get_generators
 
 router = APIRouter()
 
@@ -30,6 +30,10 @@ class CacheResponse(CamelModel):
     caches: List[CacheInfo]
 
 
+class GeneratorResponse(CamelModel):
+    generators: List[GeneratorInfo]
+
+
 @router.get("/health")
 def health():
     return { "status": "healthy", "version": "3.0" }
@@ -43,4 +47,9 @@ def check_score(points: ScoreCheck):
 
 @router.get("/caches", response_model=CacheResponse)
 def caches():
-    return CacheResponse(caches=get_cache_info())
+    return CacheResponse(caches=get_cache_info())
+
+
+@router.get("/generators", response_model=GeneratorResponse)
+def generators():
+    return GeneratorResponse(generators=get_generators())

+ 19 - 4
server/app/point_gen/__init__.py

@@ -1,10 +1,10 @@
 from typing import List, Tuple
 
-from .random_street_view import SOURCE_GROUP as RSV_SOURCE_GROUP
-from .urban_centers import SOURCE_GROUP as URBAN_CENTER_SOURCE_GROUP
+from .random_street_view import SOURCE_GROUP as RSV_SOURCE_GROUP, VALID_COUNTRIES as RSV_COUNTRIES
+from .urban_centers import SOURCE_GROUP as URBAN_CENTER_SOURCE_GROUP, VALID_COUNTRIES as URBAN_COUNTRIES
 from .shared import ExhaustedSourceError
 
-from ..schemas import GameConfig, GenMethodEnum, CacheInfo
+from ..schemas import GameConfig, GenMethodEnum, CacheInfo, GeneratorInfo
 
 source_groups = {
     GenMethodEnum.rsv: RSV_SOURCE_GROUP,
@@ -28,6 +28,21 @@ def restock_source(config: GameConfig):
 
 def get_cache_info() -> List[CacheInfo]:
     """
-    Get CacheInfo for all caches
+    Get CacheInfo for all caches.
     """
     return [CacheInfo(cache_name=c.get_name(), size=len(c.stock)) for g in source_groups.values() for c in g.cached]
+
+def get_generators() -> List[GeneratorInfo]:
+    """
+    Get all available Generators and their country options
+    """
+    return [
+        GeneratorInfo(
+            generation_method=GenMethodEnum.rsv,
+            country_locks=RSV_COUNTRIES
+        ),
+        GeneratorInfo(
+            generation_method=GenMethodEnum.urban,
+            country_locks=URBAN_COUNTRIES
+        ),
+    ]

+ 6 - 1
server/app/schemas.py

@@ -1,5 +1,5 @@
 from enum import Enum
-from typing import Union
+from typing import Union, List
 
 from fastapi_camelcase import CamelModel
 from pydantic import conint, confloat, constr
@@ -38,3 +38,8 @@ class Guess(CamelModel):
 class CacheInfo(CamelModel):
     cache_name: str
     size: int
+
+
+class GeneratorInfo(CamelModel):
+    generation_method: GenMethodEnum
+    country_locks: List[constr(to_lower=True, min_length=2, max_length=2)]