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 {
|
||||
|
||||
}
|
||||
|
||||
|
@ -22,10 +22,11 @@ class ValidAlbumDetailRequest(BaseModel):
|
||||
|
||||
class LastFM(FastAPI):
|
||||
"""Last.FM Endpoints"""
|
||||
def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called
|
||||
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.lastfm = importlib.import_module("lastfm_wrapper").LastFM()
|
||||
|
||||
self.endpoints = {
|
||||
|
@ -37,10 +37,11 @@ class ValidLyricRequest(BaseModel):
|
||||
|
||||
class LyricSearch(FastAPI):
|
||||
"""Lyric Search Endpoint"""
|
||||
def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called
|
||||
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.lyrics_engine = importlib.import_module("lyrics_engine").LyricsEngine()
|
||||
|
||||
self.endpoint_name = "lyric_search"
|
||||
@ -87,7 +88,9 @@ class LyricSearch(FastAPI):
|
||||
|
||||
src = data.src.upper()
|
||||
if not src in self.acceptable_request_sources:
|
||||
raise HTTPException(detail="Invalid request source", status_code=403)
|
||||
raise HTTPException(detail="Invalid request source", status_code=403)
|
||||
|
||||
await self.glob_state.increment_counter('lyric_requests')
|
||||
|
||||
search_artist = data.a
|
||||
search_song = data.s
|
||||
@ -128,6 +131,7 @@ class LyricSearch(FastAPI):
|
||||
recipient='anyone')
|
||||
|
||||
if not search_worker or not 'l' in search_worker.keys():
|
||||
await self.glob_state.increment_counter('failedlyric_requests')
|
||||
return {
|
||||
'err': True,
|
||||
'errorText': 'Sources exhausted, lyrics not located.'
|
||||
@ -141,4 +145,5 @@ class LyricSearch(FastAPI):
|
||||
'lyrics': regex.sub(r"\s/\s", "<br>", " ".join(search_worker['l'])),
|
||||
'from_cache': search_worker['method'].strip().lower().startswith("local cache"),
|
||||
'src': search_worker['method'] if add_extras else None,
|
||||
'reqn': await self.glob_state.get_counter('lyric_requests')
|
||||
}
|
||||
|
@ -9,11 +9,12 @@ from fastapi import FastAPI
|
||||
|
||||
class RandMsg(FastAPI):
|
||||
"""Random Message Endpoint"""
|
||||
def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called
|
||||
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.endpoint_name = "randmsg"
|
||||
|
||||
app.add_api_route(f"/{self.endpoint_name}/", self.randmsg_handler, methods=["POST"])
|
||||
|
@ -23,10 +23,11 @@ class ValidShowEpisodeLineRequest(BaseModel):
|
||||
|
||||
class Transcriptions(FastAPI):
|
||||
"""Transcription Endpoints"""
|
||||
def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called
|
||||
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 = {
|
||||
"transcriptions/get_episodes": self.get_episodes_handler,
|
||||
@ -79,6 +80,7 @@ class Transcriptions(FastAPI):
|
||||
'err': True,
|
||||
'errorText': 'Unknown error.'
|
||||
}
|
||||
await self.glob_state.increment_counter('transcript_list_requests')
|
||||
async with sqlite3.connect(database=db_path, timeout=1) as _db:
|
||||
async with _db.execute(db_query) as _cursor:
|
||||
result = await _cursor.fetchall()
|
||||
@ -114,7 +116,8 @@ class Transcriptions(FastAPI):
|
||||
'err': True,
|
||||
'errorText': 'Unknown error'
|
||||
}
|
||||
|
||||
|
||||
await self.glob_state.increment_counter('transcript_requests')
|
||||
async with sqlite3.connect(database=db_path, timeout=1) as _db:
|
||||
params = (episode_id,)
|
||||
async with _db.execute(db_query, params) as _cursor:
|
||||
|
@ -14,10 +14,11 @@ class ValidYTSearchRequest(BaseModel):
|
||||
|
||||
class YT(FastAPI):
|
||||
"""YT Endpoints"""
|
||||
def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called
|
||||
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.ytsearch = importlib.import_module("youtube_search_async").YoutubeSearch()
|
||||
|
||||
self.endpoints = {
|
||||
|
Reference in New Issue
Block a user