Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating API with Flask-RESTful #216

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 18 additions & 36 deletions local/rest_api_gcbm/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask_autoindex import AutoIndex
from flask_swagger_ui import get_swaggerui_blueprint
from flask_swagger import swagger
from flask_restful import Resource, Api
from datetime import timedelta
from datetime import datetime
from google.cloud import storage, pubsub_v1
Expand Down Expand Up @@ -337,12 +338,9 @@ def gcbm_dynamic():
title = "".join(c for c in title if c.isalnum())
input_dir = f"{os.getcwd()}/input/{title}"

try:
get_config_templates(input_dir)
get_modules_cbm_config(input_dir)
get_provider_config(input_dir)
except:
return {"error": "please upload files before running dynamic endpoint"}, 400
get_config_templates(input_dir)
get_modules_cbm_config(input_dir)
get_provider_config(input_dir)

if not os.path.exists(f"{input_dir}"):
os.makedirs(f"{input_dir}")
Expand Down Expand Up @@ -465,22 +463,6 @@ def gcbm_download():
title = request.form.get("title") or "simulation"
# Sanitize title
title = "".join(c for c in title if c.isalnum())

output_dir = f"{os.getcwd()}/output/{title}.zip"
input_dir = f"{os.getcwd()}/input/{title}"

# if the title has an input simulation and there is no output simulation then they should check the status.
if not os.path.exists(f"{output_dir}") and os.path.exists(f"{input_dir}"):
return {
"message": "You simulation is currently running, check the status via /gcbm/status"
}

# if there is no input simulation and no output simulation then the simulation does not exist.
elif not os.path.exists(f"{output_dir}") and not os.path.exists(f"{input_dir}"):
return {
"message": "You don't have a simulation with this title kindly check the title and try again"
}

return send_file(
f"{os.getcwd()}/output/{title}.zip",
attachment_filename="{title}.zip",
Expand All @@ -503,13 +485,10 @@ def gcbm_list_simulations():
for file in os.listdir(f"{os.getcwd()}/input"):
list.append(file)

return (
{
"data": list,
"message": "To create a new simulation, create a request at gcbm/new. To access the results of the existing simulations, create a request at gcbm/download.",
},
200,
)
return {
"data": list,
"message": "To create a new simulation, create a request at gcbm/new. To access the results of the existing simulations, create a request at gcbm/download.",
}, 200


@app.route("/gcbm/status", methods=["POST"])
Expand Down Expand Up @@ -542,15 +521,18 @@ def status():
return {"finished": message}


@app.route("/check", methods=["GET", "POST"])
def check():
return "Checks OK", 200

class check(Resource):
def get(self):
return {"Checks OK"}, 200
def post(self):
return {"Checks OK"}, 200

@app.route("/", methods=["GET"])
def home():
return "FLINT.Cloud API"
class home(Resource):
def get(self):
return {"FLINT.Cloud API"}, 200

api.add_resource(check, "/check")
api.add_resource(home, '/')

@app.route("/upload/title", methods=["POST"])
def getTitle():
Expand Down