Move api config options to bapsicle state for frozen packages.

This commit is contained in:
Matthew Stratford 2021-04-10 23:59:49 +01:00
parent a98388421c
commit 0faf3caa2e
8 changed files with 41 additions and 23 deletions

2
.gitignore vendored
View file

@ -24,8 +24,6 @@ build/output/
venv/
config.py
dev/welcome.mp3
build/build-exe-pyinstaller-command.sh

View file

@ -1,3 +1,4 @@
from helpers.state_manager import StateManager
import json
from multiprocessing import Queue, current_process
from helpers.logging_manager import LoggingManager
@ -15,7 +16,7 @@ class APIHandler:
server_to_q: Queue
server_from_q: Queue
def __init__(self, server_from_q: Queue, server_to_q: Queue):
def __init__(self, server_from_q: Queue, server_to_q: Queue, server_config: StateManager):
process_title = "APIHandler"
setproctitle(process_title)
@ -24,7 +25,7 @@ class APIHandler:
self.server_from_q = server_from_q
self.server_to_q = server_to_q
self.logger = LoggingManager("APIHandler")
self.api = MyRadioAPI(self.logger)
self.api = MyRadioAPI(self.logger, server_config)
self.handle()

2
config.py Normal file
View file

@ -0,0 +1,2 @@
# BAPSicle Details
VERSION: float = 0.1

View file

@ -1,7 +0,0 @@
# BAPSicle Details
VERSION: float = 1.0
# API Settings
API_KEY: str = ""
MYRADIO_BASE_URL: str = "https://ury.org.uk/myradio"
MYRADIO_API_URL: str = "https://ury.org.uk/api"

View file

@ -19,29 +19,31 @@
from typing import Optional
import requests
import json
import config
from logging import INFO
import os
from plan import PlanItem
from helpers.os_environment import resolve_external_file_path
from helpers.logging_manager import LoggingManager
from helpers.state_manager import StateManager
class MyRadioAPI:
# TODO Config type
logger = None
def __init__(self, logger: LoggingManager):
def __init__(self, logger: LoggingManager, config: StateManager):
self.logger = logger
self.config = config
def get_non_api_call(self, url):
url = "{}{}".format(config.MYRADIO_BASE_URL, url)
url = "{}{}".format(self.config.state["myradio_base_url"], url)
if "?" in url:
url += "&api_key={}".format(config.API_KEY)
url += "&api_key={}".format(self.config.state["myradio_api_key"])
else:
url += "?api_key={}".format(config.API_KEY)
url += "?api_key={}".format(self.config.state["myradio_api_key"])
self._log("Requesting non-API URL: " + url)
request = requests.get(url, timeout=10)
@ -58,12 +60,12 @@ class MyRadioAPI:
def get_apiv2_call(self, url):
url = "{}/v2{}".format(config.MYRADIO_API_URL, url)
url = "{}/v2{}".format(self.config.state["myradio_api_url"], url)
if "?" in url:
url += "&api_key={}".format(config.API_KEY)
url += "&api_key={}".format(self.config.state["myradio_api_key"])
else:
url += "?api_key={}".format(config.API_KEY)
url += "?api_key={}".format(self.config.state["myradio_api_key"])
self._log("Requesting API V2 URL: " + url)
request = requests.get(url, timeout=10)

View file

@ -519,7 +519,7 @@ class Player:
custom_prefix="ALL:STATUS:")
def __init__(
self, channel: int, in_q: multiprocessing.Queue, out_q: multiprocessing.Queue
self, channel: int, in_q: multiprocessing.Queue, out_q: multiprocessing.Queue, server_config: StateManager
):
process_title = "Player: Channel " + str(channel)
@ -531,7 +531,7 @@ class Player:
self.logger = LoggingManager("Player" + str(channel))
self.api = MyRadioAPI(self.logger)
self.api = MyRadioAPI(self.logger, server_config)
self.state = StateManager(
"Player" + str(channel),

View file

@ -64,6 +64,9 @@ default_state = {
"num_channels": 3,
"ser_port": None,
"ser_connected": False,
"api_key": None,
"myradio_base_url": "https://ury.org.uk/myradio",
"myradio_api_url": "https://ury.org.uk/api"
}
@ -162,6 +165,13 @@ def update_server():
state.update("num_channels", int(request.form["channels"]))
state.update("ws_port", int(request.form["ws_port"]))
state.update("serial_port", request.form["serial_port"])
# Because we're not showing the api key once it's set.
if "myradio_api_key" in request.form and request.form["myradio_api_key"] != "":
state.update("myradio_api_key", request.form["myradio_api_key"])
state.update("myradio_base_url", request.form["myradio_base_url"])
state.update("myradio_api_url", request.form["myradio_api_url"])
# stopServer()
return server_config()
@ -508,10 +518,13 @@ def startServer():
ui_to_q.append(multiprocessing.Queue())
websocket_to_q.append(multiprocessing.Queue())
controller_to_q.append(multiprocessing.Queue())
# TODO Replace state with individual read-only StateManagers or something nicer?
channel_p.append(
multiprocessing.Process(
target=player.Player,
args=(channel, channel_to_q[-1], channel_from_q[-1])
args=(channel, channel_to_q[-1], channel_from_q[-1], state)
# daemon=True
)
)
@ -521,7 +534,7 @@ def startServer():
api_to_q = multiprocessing.Queue()
api_from_q = multiprocessing.Queue()
api_handler = multiprocessing.Process(
target=APIHandler, args=(api_to_q, api_from_q)
target=APIHandler, args=(api_to_q, api_from_q, state)
)
api_handler.start()

View file

@ -24,7 +24,16 @@
<option value="{{port}}" {% if port == data.state.serial_port %}selected{% endif %}>{{port}}</option>
{% endfor %}
</select>
<hr>
<label for="myradio_base_url">MyRadio Base URL:</label>
<input type="text" id="myradio_base_url" name="myradio_base_url" class="form-control" value="{{data.state.myradio_base_url}}">
<br>
<label for="myradio_api_url">MyRadio API URL:</label>
<input type="text" id="myradio_api_url" name="myradio_api_url" class="form-control" value="{{data.state.myradio_api_url}}">
<br>
<label for="myradio_api_key">MyRadio API Key:</label>
<input type="text" id="myradio_api_key" name="myradio_api_key" class="form-control" placeholder="Hidden ({% if data.state.myradio_api_key %}value set, type to replace{% else %}value not set yet{% endif %})" value="">
<hr>
<input type="submit" class="btn btn-primary" value="Save & Restart Server">
</form>
{% endif %}