Add global state module
This commit is contained in:
69
endpoints/counters.py
Normal file
69
endpoints/counters.py
Normal file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3.12
|
||||
|
||||
#!/usr/bin/env python3.12
|
||||
|
||||
import importlib
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
class ValidCounterIncrementRequest(BaseModel):
|
||||
"""
|
||||
- **counter**: counter to update
|
||||
"""
|
||||
|
||||
counter: str
|
||||
|
||||
|
||||
class ValidCounterRetrievalRequest(BaseModel):
|
||||
"""
|
||||
- **counter**: counter to retrieve (if none is provided, all counters are returned)
|
||||
"""
|
||||
|
||||
counter: str = "all"
|
||||
|
||||
|
||||
class Counters(FastAPI):
|
||||
"""Counter Endpoints"""
|
||||
def __init__(self, app: FastAPI, util, constants, glob_state): # pylint: disable=super-init-not-called
|
||||
self.app = app
|
||||
self.util = util
|
||||
self.constants = constants
|
||||
self.glob_state = glob_state
|
||||
|
||||
self.endpoints = {
|
||||
"counters/get": self.get_counter_handler,
|
||||
"counters/increment": self.increment_counter_handler
|
||||
#tbd
|
||||
}
|
||||
|
||||
for endpoint, handler in self.endpoints.items():
|
||||
app.add_api_route(f"/{endpoint}/", handler, methods=["POST"])
|
||||
|
||||
async def get_counter_handler(self, data: ValidCounterRetrievalRequest):
|
||||
"""
|
||||
/get/
|
||||
Get current counter value
|
||||
"""
|
||||
|
||||
counter = data.counter
|
||||
if not counter == 'all':
|
||||
count = await self.glob_state.get_counter(counter)
|
||||
else:
|
||||
count = await self.glob_state.get_all_counters()
|
||||
return {
|
||||
'counter': counter,
|
||||
'count': count
|
||||
|
||||
}
|
||||
|
||||
async def increment_counter_handler(self, data: ValidCounterIncrementRequest):
|
||||
"""
|
||||
/increment/
|
||||
Increment counter value (requires PUT KEY)
|
||||
"""
|
||||
|
||||
return {
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user