Browse Source

counters info

boyska 1 year ago
parent
commit
428d58f934
1 changed files with 13 additions and 2 deletions
  1. 13 2
      pizzicore/pizzicore.py

+ 13 - 2
pizzicore/pizzicore.py

@@ -111,6 +111,10 @@ counter_store = Store(
 security = HTTPBasic()
 
 
+class CountersDescription(BaseModel):
+    counters: int
+
+
 class Value(BaseModel):
     counter: int
     value: int
@@ -128,13 +132,17 @@ def get_current_role(credentials: HTTPBasicCredentials = Depends(security)):
         )
     return "admin"
 
+@app.get("/v1/counter/")
+async def get_counter_number():
+    return CountersDescription(counters=len(counter_store.values))
+
 
 @app.get("/v1/counter/{cid}")
 async def get_value(cid: int):
     try:
         val = counter_store.get(cid)
     except KeyError:
-        raise HTTPException(status_code=404, detail="Item not found")
+        raise HTTPException(status_code=404, detail="Counter not found")
     else:
         return Value(counter=cid, value=val)
 
@@ -143,7 +151,10 @@ async def get_value(cid: int):
 async def increment(cid: int, role: str = Depends(get_current_role)):
     if role != "admin":
         raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
-    val = counter_store.incr(cid)
+    try:
+        val = counter_store.incr(cid)
+    except KeyError:
+        raise HTTPException(status_code=404, detail="Counter not found")
     return Value(counter=cid, value=val)