counters info

This commit is contained in:
boyska 2022-08-17 19:17:13 +02:00
parent 63154c0249
commit 428d58f934

View file

@ -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)