From 517c1b071d625658aca1a61462c8c48d55b26127 Mon Sep 17 00:00:00 2001 From: arianagiroux Date: Sun, 24 Jul 2022 14:33:09 -0600 Subject: [PATCH] Added a more sophisticated json loading interface - this should provide more verbose output in the case that the json file either doesn't exist or isn't formatted correctly --- app.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index eabbb00..5e51b4f 100644 --- a/app.py +++ b/app.py @@ -32,10 +32,21 @@ structure: import chevron import requests import json +from os import path from pprint import pprint from flask import Flask, request -stream_data = json.load(open('resources/data.json', 'r')) +# load json data, or raise exception. +if path.exists('resources/data.json'): + try: + stream_data = json.load(open('resources/data.json', 'r')) + except json.JSONDecodeError as e: + raise RuntimeError('JSON data did not pass validation.') +else: + raise RuntimeError('Could not find ./resources/data.json, required for ' + 'runtime!') + +# initialize requests session session = requests.Session() session.auth = ('admin', stream_data['stream_key'])