Skip to content

Commit

Permalink
Merge pull request ai-cfia#28 from ai-cfia/RussellJimmies/issue26
Browse files Browse the repository at this point in the history
Closes ai-cfia#26
  • Loading branch information
RussellJimmies authored Oct 20, 2023
2 parents 688f877 + 4dffcba commit dc8469f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
3 changes: 1 addition & 2 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
NACHET_AZURE_STORAGE_CONNECTION_STRING=
NACHET_MODEL_ENDPOINT_REST_URL=
NACHET_MODEL_ENDPOINT_ACCESS_KEY=
NACHET_DATA=
NACHET_HEALTH_MESSAGE=
NACHET_DATA=
41 changes: 25 additions & 16 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

NACHET_DATA = os.getenv("NACHET_DATA")

NACHET_HEALTH_MESSAGE = os.getenv("NACHET_HEALTH_MESSAGE")

SEED_CACHE = {}

# Check: do environment variables exist?
Expand Down Expand Up @@ -213,20 +211,37 @@ async def inference_request():
return jsonify(["InferenceRequestError: " + str(error)]), 400


@app.get("/seed-info/<seed_name>")
async def get_seed_info(seed_name):
@app.get("/seed-data/<seed_name>")
async def get_seed_data(seed_name):
"""
Returns JSON containing requested seed information
Returns JSON containing requested seed data
"""
if seed_name in SEED_CACHE:
return jsonify(SEED_CACHE[seed_name])
return jsonify(SEED_CACHE[seed_name]), 200
else:
return jsonify(f"No information found for {seed_name}.")

return jsonify(f"No information found for {seed_name}."), 400


@app.get("/reload-seed-data")
async def reload_seed_data():
"""
Reloads seed data JSON document from Nachet-Data
"""
try:
await fetch_seed_json()
return jsonify(["Seed data reloaded successfully"]), 200
except Exception as e:
return jsonify({"error": str(e)}), 500


@app.get("/health")
async def health():
return "ok", 200


async def fetch_seed_json():
"""
Fetches JSON document of all seed info from Nachet-Data and caches it
Fetches JSON document of all seed data from Nachet-Data and caches it
"""
global SEED_CACHE
try:
Expand All @@ -235,18 +250,12 @@ async def fetch_seed_json():
result = response.read()
result_json = json.loads(result.decode("utf-8"))
SEED_CACHE = result_json

except urllib.error.HTTPError as error:
return jsonify({"error": f"Failed to retrieve the JSON. \
HTTP Status Code: {error.code}"}), 400
except Exception as e:
return jsonify({"error": str(e)}), 500


@app.get("/health")
async def health():
return NACHET_HEALTH_MESSAGE, 200



@app.before_serving
async def before_serving():
Expand Down

0 comments on commit dc8469f

Please sign in to comment.