Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OSUG
RESIF
ws-sacpz-resp
Commits
f6b41cd9
Commit
f6b41cd9
authored
Jul 20, 2020
by
Jerome Touvier
Browse files
refactoring
parent
3dc03485
Pipeline
#47213
passed with stage
in 2 minutes and 46 seconds
Changes
8
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
apps/globals.py
View file @
f6b41cd9
...
...
@@ -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. "
...
...
apps/output.py
View file @
f6b41cd9
...
...
@@ -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
:
tic
0
=
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
(
tic
0
)
}
seconds."
)
n
bytes
=
response
.
headers
.
get
(
"Content-Length"
)
logging
.
info
(
f
"
{
n
bytes
}
bytes rendered in
{
tictac
(
tic
)
}
seconds."
)
def
get_resp
(
params
):
...
...
@@ -75,7 +73,7 @@ def get_resp(params):
params: parameters """
try
:
tic
0
=
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
(
tic
0
)
}
seconds."
)
n
bytes
=
response
.
headers
.
get
(
"Content-Length"
)
logging
.
info
(
f
"
{
n
bytes
}
bytes rendered in
{
tictac
(
tic
)
}
seconds."
)
apps/root.py
View file @
f6b41cd9
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
(
p
dic
))
q
.
put
(
get_resp
(
p
arams
))
elif
webservice
==
"sacpz"
:
q
.
put
(
get_sacpz
(
p
dic
))
q
.
put
(
get_sacpz
(
p
arams
))
q
=
Queue
()
process
=
Process
(
target
=
put_response
,
args
=
(
q
,))
...
...
apps/utils.py
View file @
f6b41cd9
...
...
@@ -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
n
et
in
network
:
if
not
is_valid_network
(
n
et
):
return
error_param
(
params
,
Error
.
NETWORK
+
n
et
)
for
s
ta
in
station
:
if
not
is_valid_station
(
s
ta
):
return
error_param
(
params
,
Error
.
STATION
+
s
ta
)
for
l
oc
in
location
:
if
not
is_valid_location
(
l
oc
):
return
error_param
(
params
,
Error
.
LOCATION
+
l
oc
)
for
c
ha
in
channel
:
if
not
is_valid_channel
(
c
ha
):
return
error_param
(
params
,
Error
.
CHANNEL
+
c
ha
)
# Start time and end time validations
if
params
[
"start"
]
is
not
None
:
...
...
start_resp.py
View file @
f6b41cd9
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"
)
...
...
start_sacpz.py
View file @
f6b41cd9
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"
)
...
...
templates/
ws-
resp
.
wadl.xml
→
templates/resp
_
wadl.xml
View file @
f6b41cd9
...
...
@@ -2,7 +2,7 @@
<application>
<doc
title=
"RESIF resp web service 1.0"
/>
<resources
base=
"http://ws.resif.fr/resifws/
eval
resp/1"
>
<resources
base=
"http://ws.resif.fr/resifws/resp/1"
>
<resource
path=
"/"
>
<method
name=
"GET"
>
<response>
...
...
templates/
ws-
sacpz
.
wadl.xml
→
templates/sacpz
_
wadl.xml
View file @
f6b41cd9
...
...
@@ -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>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment