single_ursim_control/single_ursim_control/program_loader.py

22 lines
729 B
Python

from program_schema import ProgramSchema, SUPPORTED_PROGRAM_STRUCTURE_VERSION
import requests
import logging
def load_program(url: str) -> dict:
logger = logging.getLogger('loader')
headers = {
'Accept': 'application/json',
}
logger.debug(f"Performing HTTP request: (url: {url}, headers: {headers})")
r = requests.get(url, headers=headers)
r.raise_for_status()
logger.debug(f"HTTP Status: {r.status_code}; Validating downloaded info...")
program_schema = ProgramSchema(many=False)
program = program_schema.load(r.json()) # Might raise marshmallow exceptions
logger.info(f"Loaded {program.get('name')} (Created at: {program.get('created_at')}) from {url}")
return program