Commit 5bc0c93a authored by Jerome Touvier's avatar Jerome Touvier
Browse files

Merge branch 'refactoring1' into 'master'

refactoring

See merge request !5
parents 3dc03485 f6b41cd9
Pipeline #47219 passed with stage
in 2 minutes and 40 seconds
......@@ -50,7 +50,6 @@ class HTTP:
_403_ = "Forbidden access. "
_404_ = "No data matches the selection. "
_408_ = "Request exceeds timeout. "
_409_ = "Too much data. "
_413_ = "Request too large. "
_414_ = "Request URI too large. "
_500_ = "Internal server error. "
......
......@@ -14,7 +14,6 @@ from apps.globals import FDSN_CLIENT
from apps.globals import STATIONXML_CONVERTER
from apps.globals import USER_AGENT_RESP
from apps.globals import USER_AGENT_SACPZ
from apps.utils import error_500
from apps.utils import error_request
from apps.utils import tictac
......@@ -39,7 +38,7 @@ def get_sacpz(params):
params: parameters """
try:
tic0 = time.time()
tic = time.time()
response = None
cid = f"{params['network']}.{params['station']}.{params['location']}.{params['channel']}"
......@@ -63,11 +62,10 @@ def get_sacpz(params):
return response
except Exception as ex:
logging.exception(str(ex))
return error_500(Error.UNSPECIFIED)
finally:
if response:
bytes = response.headers.get("Content-Length")
logging.info(f"{bytes} bytes rendered in {tictac(tic0)} seconds.")
nbytes = response.headers.get("Content-Length")
logging.info(f"{nbytes} bytes rendered in {tictac(tic)} seconds.")
def get_resp(params):
......@@ -75,7 +73,7 @@ def get_resp(params):
params: parameters """
try:
tic0 = time.time()
tic = time.time()
response = None
cid = f"{params['network']}.{params['station']}.{params['location']}.{params['channel']}"
......@@ -122,8 +120,7 @@ def get_resp(params):
return response
except Exception as ex:
logging.exception(str(ex))
return error_500(Error.UNSPECIFIED)
finally:
if response:
bytes = response.headers.get("Content-Length")
logging.info(f"{bytes} bytes rendered in {tictac(tic0)} seconds.")
nbytes = response.headers.get("Content-Length")
logging.info(f"{nbytes} bytes rendered in {tictac(tic)} seconds.")
import logging
import queue
import re
from copy import copy
from multiprocessing import Process, Queue
from flask import request
from apps.constants import ALIAS
from apps.constants import Parameters
from apps.globals import Error
......@@ -61,18 +62,17 @@ def check_parameters(params):
return (params, {"msg": HTTP._200_, "details": Error.VALID_PARAM, "code": 200})
def checks_get(request):
def checks_get():
# get default parameters
params = Parameters().todict()
# check if the parameters are unknown
(p, result) = check_request(request, params, ALIAS)
(p, result) = check_request(params, ALIAS)
if result["code"] != 200:
return (p, result)
# determine selected features
params["base_url"] = request.base_url
params["request"] = tuple(request.args)
for key, val in params.items():
......@@ -87,22 +87,21 @@ def checks_get(request):
return check_parameters(params)
def output(request, webservice):
def output(webservice):
try:
process = None
result = {"msg": HTTP._400_, "details": Error.UNKNOWN_PARAM, "code": 400}
logging.debug(request.url)
(pdic, result) = checks_get(request)
(params, result) = checks_get()
if result["code"] == 200:
def put_response(q):
if webservice == "resp":
q.put(get_resp(pdic))
q.put(get_resp(params))
elif webservice == "sacpz":
q.put(get_sacpz(pdic))
q.put(get_sacpz(params))
q = Queue()
process = Process(target=put_response, args=(q,))
......
......@@ -69,7 +69,7 @@ def is_valid_channel(channel):
def is_valid_bool_string(string):
if string is None:
return False
return True if string.lower() in (STRING_TRUE + STRING_FALSE) else False
return bool(string.lower() in STRING_TRUE + STRING_FALSE)
def is_valid_nodata(nodata):
......@@ -90,14 +90,17 @@ def error_param(params, dmesg):
# Error request function
def error_request(msg=" ", details=" ", code=" "):
def error_request(msg="", details="", code=500):
request_date = datetime.utcnow().strftime("%Y-%b-%d %H:%M:%S UTC")
message_error = f"Error {code}: {msg} Please mention your request URL when asking for support\n\n\
More Details: {details}\n\n\
Request: {request.url}\n\
Request Submitted: {request_date} \n\
Service version: version {VERSION}"
return Response(message_error, status=code, mimetype="*/*")
message_error = f"""Error {code}: {msg}\n
{details}\n
Request:
{request.url}\n
Request Submitted:
{request_date}\n
Service version:
version:{VERSION}"""
return Response(message_error, status=code, mimetype="text/plain")
# Error 413 response alias
......@@ -111,7 +114,7 @@ def error_500(dmesg):
# check request
def check_request(request, params, alias):
def check_request(params, alias):
keys = list(params.keys())
for key, val in request.args.items():
if key not in keys:
......@@ -148,7 +151,7 @@ def check_base_parameters(params, max_days=None):
val = params[key]
if not is_valid_bool_string(val):
return error_param(params, f"Invalid {key} value: {val} {Error.BOOL}.")
params[key] = True if val.lower() in STRING_TRUE else False
params[key] = bool(val.lower() in STRING_TRUE)
# Float parameter validations
for key in params["constraints"]["floats"]:
......@@ -164,18 +167,18 @@ def check_base_parameters(params, max_days=None):
location = params["location"].split(",")
channel = params["channel"].split(",")
for n in network:
if not is_valid_network(n):
return error_param(params, Error.NETWORK + n)
for s in station:
if not is_valid_station(s):
return error_param(params, Error.STATION + s)
for l in location:
if not is_valid_location(l):
return error_param(params, Error.LOCATION + l)
for c in channel:
if not is_valid_channel(c):
return error_param(params, Error.CHANNEL + c)
for net in network:
if not is_valid_network(net):
return error_param(params, Error.NETWORK + net)
for sta in station:
if not is_valid_station(sta):
return error_param(params, Error.STATION + sta)
for loc in location:
if not is_valid_location(loc):
return error_param(params, Error.LOCATION + loc)
for cha in channel:
if not is_valid_channel(cha):
return error_param(params, Error.CHANNEL + cha)
# Start time and end time validations
if params["start"] is not None:
......
import logging
import os
from flask import Flask, make_response, render_template, request
from flask import Flask, make_response, render_template
from apps.constants import VERSION
from apps.root import output
app = Flask(__name__)
fmt = "[%(asctime)s] %(levelname)s [%(filename)s:%(lineno)d] [%(funcName)s] %(message)s"
loglevel = logging.INFO if os.environ.get("RUNMODE") == "prodution" else logging.DEBUG
logging.basicConfig(format=fmt, level=loglevel)
FMT = "[%(asctime)s] %(levelname)s [%(filename)s:%(lineno)d] [%(funcName)s] %(message)s"
LOGLEVEL = logging.INFO if os.environ.get("RUNMODE") == "prodution" else logging.DEBUG
logging.basicConfig(format=FMT, level=LOGLEVEL)
# ************************************************************************
# **************************** SERVICE ROUTES ****************************
# ************************************************************************
@app.route("/query", methods=["GET"])
def resp_root_get():
return output(request, "resp")
def query():
return output("resp")
@app.route("/application.wadl")
def resp_wadl():
template = render_template("ws-resp.wadl.xml")
def wadl():
template = render_template("resp_wadl.xml")
response = make_response(template)
response.headers["Content-Type"] = "application/xml"
return response
......@@ -33,14 +37,26 @@ def version():
return response
@app.route("/commit", strict_slashes=False)
def commit():
try:
with open("./static/commit.txt") as commit_file:
COMMIT_SHORT_SHA = commit_file.readline()
except Exception:
COMMIT_SHORT_SHA = "unspecified"
response = make_response(COMMIT_SHORT_SHA)
response.headers["Content-Type"] = "text/plain"
return response
@app.route("/")
@app.route("/local=fr")
def resp_doc():
def doc():
return render_template("resp_doc.html")
@app.route("/local=en")
def resp_doc_en():
def doc_en():
return render_template("resp_doc_en.html")
......
import logging
import os
from flask import Flask, make_response, render_template, request
from flask import Flask, make_response, render_template
from apps.constants import VERSION
from apps.root import output
app = Flask(__name__)
fmt = "[%(asctime)s] %(levelname)s [%(filename)s:%(lineno)d] [%(funcName)s] %(message)s"
loglevel = logging.INFO if os.environ.get("RUNMODE") == "prodution" else logging.DEBUG
logging.basicConfig(format=fmt, level=loglevel)
FMT = "[%(asctime)s] %(levelname)s [%(filename)s:%(lineno)d] [%(funcName)s] %(message)s"
LOGLEVEL = logging.INFO if os.environ.get("RUNMODE") == "prodution" else logging.DEBUG
logging.basicConfig(format=FMT, level=LOGLEVEL)
# ************************************************************************
# **************************** SERVICE ROUTES ****************************
# ************************************************************************
@app.route("/query", methods=["GET"])
def sacpz_root_get():
return output(request, "sacpz")
def query():
return output("sacpz")
@app.route("/application.wadl")
def sacpz_wadl():
template = render_template("ws-sacpz.wadl.xml")
def wadl():
template = render_template("sacpz_wadl.xml")
response = make_response(template)
response.headers["Content-Type"] = "application/xml"
return response
......@@ -33,14 +37,26 @@ def version():
return response
@app.route("/commit", strict_slashes=False)
def commit():
try:
with open("./static/commit.txt") as commit_file:
COMMIT_SHORT_SHA = commit_file.readline()
except Exception:
COMMIT_SHORT_SHA = "unspecified"
response = make_response(COMMIT_SHORT_SHA)
response.headers["Content-Type"] = "text/plain"
return response
@app.route("/")
@app.route("/local=fr")
def sacpz_doc():
def doc():
return render_template("sacpz_doc.html")
@app.route("/local=en")
def sacpz_doc_en():
def doc_en():
return render_template("sacpz_doc_en.html")
......
......@@ -2,7 +2,7 @@
<application>
<doc title="RESIF resp web service 1.0"/>
<resources base="http://ws.resif.fr/resifws/evalresp/1">
<resources base="http://ws.resif.fr/resifws/resp/1">
<resource path="/">
<method name="GET">
<response>
......
......@@ -2,7 +2,7 @@
<application>
<doc title="RESIF sacpz web service 1.0"/>
<resources base="http://ws.resif.fr/resifws/evalresp/1">
<resources base="http://ws.resif.fr/resifws/sacpz/1">
<resource path="/">
<method name="GET">
<response>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment