counters info
This commit is contained in:
parent
63154c0249
commit
428d58f934
1 changed files with 13 additions and 2 deletions
|
@ -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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue