diff --git a/pizzicore/pizzicore.py b/pizzicore/pizzicore.py index f7ea378..13d5fb9 100644 --- a/pizzicore/pizzicore.py +++ b/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)