mirror of
https://github.com/krateng/maloja.git
synced 2023-08-10 21:12:55 +03:00
Improved native API error feedback
This commit is contained in:
parent
b255d424ee
commit
15f815ffe9
@ -37,6 +37,28 @@ api.__apipath__ = "mlj_1"
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
errors = {
|
||||||
|
database.MissingScrobbleParameters: lambda e: (400,{
|
||||||
|
"status":"failure",
|
||||||
|
"error":{
|
||||||
|
'type':'missing_scrobble_data',
|
||||||
|
'value':e.params,
|
||||||
|
'desc':"A scrobble requires these parameters."
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
Exception: lambda e: (500,{
|
||||||
|
"status":"failure",
|
||||||
|
"error":{
|
||||||
|
'type':'unknown_error',
|
||||||
|
'value':e.__repr__(),
|
||||||
|
'desc':"The server has encountered an exception."
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def add_common_args_to_docstring(filterkeys=False,limitkeys=False,delimitkeys=False,amountkeys=False):
|
def add_common_args_to_docstring(filterkeys=False,limitkeys=False,delimitkeys=False,amountkeys=False):
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
timeformats = "Possible formats include '2022', '2022/08', '2022/08/01', '2022/W42', 'today', 'thismonth', 'monday', 'august'"
|
timeformats = "Possible formats include '2022', '2022/08', '2022/08/01', '2022/W42', 'today', 'thismonth', 'monday', 'august'"
|
||||||
@ -84,11 +106,16 @@ def test_server(key=None):
|
|||||||
response.set_header("Access-Control-Allow-Origin","*")
|
response.set_header("Access-Control-Allow-Origin","*")
|
||||||
if key is not None and not apikeystore.check_key(key):
|
if key is not None and not apikeystore.check_key(key):
|
||||||
response.status = 403
|
response.status = 403
|
||||||
return {"status":"error","error":"Wrong API key"}
|
return {
|
||||||
|
"status":"error",
|
||||||
|
"error":"Wrong API key"
|
||||||
|
}
|
||||||
|
|
||||||
else:
|
else:
|
||||||
response.status = 200
|
response.status = 200
|
||||||
return {"status":"ok"}
|
return {
|
||||||
|
"status":"ok"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@api.get("serverinfo")
|
@api.get("serverinfo")
|
||||||
@ -131,7 +158,9 @@ def get_scrobbles_external(**keys):
|
|||||||
result = result[offset:]
|
result = result[offset:]
|
||||||
if k_amount.get('perpage') is not math.inf: result = result[:k_amount.get('perpage')]
|
if k_amount.get('perpage') is not math.inf: result = result[:k_amount.get('perpage')]
|
||||||
|
|
||||||
return {"list":result}
|
return {
|
||||||
|
"list":result
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@api.get("numscrobbles")
|
@api.get("numscrobbles")
|
||||||
@ -146,7 +175,10 @@ def get_scrobbles_num_external(**keys):
|
|||||||
ckeys = {**k_filter, **k_time, **k_amount}
|
ckeys = {**k_filter, **k_time, **k_amount}
|
||||||
|
|
||||||
result = database.get_scrobbles_num(**ckeys)
|
result = database.get_scrobbles_num(**ckeys)
|
||||||
return {"amount":result}
|
|
||||||
|
return {
|
||||||
|
"amount":result
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -162,7 +194,10 @@ def get_tracks_external(**keys):
|
|||||||
ckeys = {**k_filter}
|
ckeys = {**k_filter}
|
||||||
|
|
||||||
result = database.get_tracks(**ckeys)
|
result = database.get_tracks(**ckeys)
|
||||||
return {"list":result}
|
|
||||||
|
return {
|
||||||
|
"list":result
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -174,7 +209,10 @@ def get_artists_external():
|
|||||||
:return: list (List)
|
:return: list (List)
|
||||||
:rtype: Dictionary"""
|
:rtype: Dictionary"""
|
||||||
result = database.get_artists()
|
result = database.get_artists()
|
||||||
return {"list":result}
|
|
||||||
|
return {
|
||||||
|
"list":result
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -191,7 +229,10 @@ def get_charts_artists_external(**keys):
|
|||||||
ckeys = {**k_time}
|
ckeys = {**k_time}
|
||||||
|
|
||||||
result = database.get_charts_artists(**ckeys)
|
result = database.get_charts_artists(**ckeys)
|
||||||
return {"list":result}
|
|
||||||
|
return {
|
||||||
|
"list":result
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -206,7 +247,10 @@ def get_charts_tracks_external(**keys):
|
|||||||
ckeys = {**k_filter, **k_time}
|
ckeys = {**k_filter, **k_time}
|
||||||
|
|
||||||
result = database.get_charts_tracks(**ckeys)
|
result = database.get_charts_tracks(**ckeys)
|
||||||
return {"list":result}
|
|
||||||
|
return {
|
||||||
|
"list":result
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -222,7 +266,10 @@ def get_pulse_external(**keys):
|
|||||||
ckeys = {**k_filter, **k_time, **k_internal, **k_amount}
|
ckeys = {**k_filter, **k_time, **k_internal, **k_amount}
|
||||||
|
|
||||||
results = database.get_pulse(**ckeys)
|
results = database.get_pulse(**ckeys)
|
||||||
return {"list":results}
|
|
||||||
|
return {
|
||||||
|
"list":results
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -238,7 +285,10 @@ def get_performance_external(**keys):
|
|||||||
ckeys = {**k_filter, **k_time, **k_internal, **k_amount}
|
ckeys = {**k_filter, **k_time, **k_internal, **k_amount}
|
||||||
|
|
||||||
results = database.get_performance(**ckeys)
|
results = database.get_performance(**ckeys)
|
||||||
return {"list":results}
|
|
||||||
|
return {
|
||||||
|
"list":results
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -254,7 +304,10 @@ def get_top_artists_external(**keys):
|
|||||||
ckeys = {**k_time, **k_internal}
|
ckeys = {**k_time, **k_internal}
|
||||||
|
|
||||||
results = database.get_top_artists(**ckeys)
|
results = database.get_top_artists(**ckeys)
|
||||||
return {"list":results}
|
|
||||||
|
return {
|
||||||
|
"list":results
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -272,7 +325,10 @@ def get_top_tracks_external(**keys):
|
|||||||
# IMPLEMENT THIS FOR TOP TRACKS OF ARTIST AS WELL?
|
# IMPLEMENT THIS FOR TOP TRACKS OF ARTIST AS WELL?
|
||||||
|
|
||||||
results = database.get_top_tracks(**ckeys)
|
results = database.get_top_tracks(**ckeys)
|
||||||
return {"list":results}
|
|
||||||
|
return {
|
||||||
|
"list":results
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -325,7 +381,7 @@ def post_scrobble(
|
|||||||
"""Submit a new scrobble.
|
"""Submit a new scrobble.
|
||||||
|
|
||||||
:param string artist: Artist. Can be submitted multiple times as query argument for multiple artists.
|
:param string artist: Artist. Can be submitted multiple times as query argument for multiple artists.
|
||||||
:param list artists: List of artists. Overwritten by artist parameter.
|
:param list artists: List of artists.
|
||||||
:param string title: Title of the track.
|
:param string title: Title of the track.
|
||||||
:param string album: Name of the album. Optional.
|
:param string album: Name of the album. Optional.
|
||||||
:param list albumartists: Album artists. Optional.
|
:param list albumartists: Album artists. Optional.
|
||||||
@ -339,7 +395,7 @@ def post_scrobble(
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
rawscrobble = {
|
rawscrobble = {
|
||||||
'track_artists':artist if artist is not None else artists,
|
'track_artists':(artist or []) + artists,
|
||||||
'track_title':title,
|
'track_title':title,
|
||||||
'album_name':album,
|
'album_name':album,
|
||||||
'album_artists':albumartists,
|
'album_artists':albumartists,
|
||||||
@ -351,15 +407,15 @@ def post_scrobble(
|
|||||||
# for logging purposes, don't pass values that we didn't actually supply
|
# for logging purposes, don't pass values that we didn't actually supply
|
||||||
rawscrobble = {k:rawscrobble[k] for k in rawscrobble if rawscrobble[k]}
|
rawscrobble = {k:rawscrobble[k] for k in rawscrobble if rawscrobble[k]}
|
||||||
|
|
||||||
result = database.incoming_scrobble(
|
try:
|
||||||
rawscrobble,
|
result = database.incoming_scrobble(
|
||||||
client='browser' if auth_result.get('doreah_native_auth_check') else auth_result.get('client'),
|
rawscrobble,
|
||||||
api='native/v1',
|
client='browser' if auth_result.get('doreah_native_auth_check') else auth_result.get('client'),
|
||||||
fix=(nofix is None)
|
api='native/v1',
|
||||||
)
|
fix=(nofix is None)
|
||||||
|
)
|
||||||
|
|
||||||
if result:
|
responsedict = {
|
||||||
response = {
|
|
||||||
'status': 'success',
|
'status': 'success',
|
||||||
'track': {
|
'track': {
|
||||||
'artists':result['track']['artists'],
|
'artists':result['track']['artists'],
|
||||||
@ -367,14 +423,24 @@ def post_scrobble(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if extra_kwargs:
|
if extra_kwargs:
|
||||||
response['warnings'] = [
|
responsedict['warnings'] = [
|
||||||
{'type':'invalid_keyword_ignored','value':k}
|
{'type':'invalid_keyword_ignored','value':k,
|
||||||
|
'desc':"This key was not recognized by the server and has been discarded."}
|
||||||
for k in extra_kwargs
|
for k in extra_kwargs
|
||||||
]
|
]
|
||||||
else:
|
if artist and artists:
|
||||||
response = {"status":"failure"}
|
responsedict['warnings'] = [
|
||||||
|
{'type':'mixed_schema','value':['artist','artists'],
|
||||||
|
'desc':"These two fields are meant as alternative methods to submit information. Use of both is discouraged, but works at the moment."}
|
||||||
|
]
|
||||||
|
return responsedict
|
||||||
|
except Exception as e:
|
||||||
|
for etype in errors:
|
||||||
|
if isinstance(e,etype):
|
||||||
|
errorhandling = errors[etype](e)
|
||||||
|
response.status = errorhandling[0]
|
||||||
|
return errorhandling[1]
|
||||||
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,6 +51,11 @@ class DatabaseNotBuilt(HTTPError):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class MissingScrobbleParameters(Exception):
|
||||||
|
def __init__(self,params=[]):
|
||||||
|
self.params = params
|
||||||
|
|
||||||
|
|
||||||
def waitfordb(func):
|
def waitfordb(func):
|
||||||
def newfunc(*args,**kwargs):
|
def newfunc(*args,**kwargs):
|
||||||
if not dbstatus['healthy']: raise DatabaseNotBuilt()
|
if not dbstatus['healthy']: raise DatabaseNotBuilt()
|
||||||
@ -86,10 +91,14 @@ cla = CleanerAgent()
|
|||||||
|
|
||||||
def incoming_scrobble(rawscrobble,fix=True,client=None,api=None,dbconn=None):
|
def incoming_scrobble(rawscrobble,fix=True,client=None,api=None,dbconn=None):
|
||||||
|
|
||||||
if (not "track_artists" in rawscrobble) or (len(rawscrobble['track_artists']) == 0) or (not "track_title" in rawscrobble):
|
missing = []
|
||||||
|
for necessary_arg in ["track_artists","track_title"]:
|
||||||
|
if not necessary_arg in rawscrobble or len(rawscrobble[necessary_arg]) == 0:
|
||||||
|
missing.append(necessary_arg)
|
||||||
|
if len(missing) > 0:
|
||||||
log(f"Invalid Scrobble [Client: {client} | API: {api}]: {rawscrobble} ",color='red')
|
log(f"Invalid Scrobble [Client: {client} | API: {api}]: {rawscrobble} ",color='red')
|
||||||
#return {"status":"failure"}
|
raise MissingScrobbleParameters(missing)
|
||||||
return False
|
|
||||||
|
|
||||||
log(f"Incoming scrobble [Client: {client} | API: {api}]: {rawscrobble}")
|
log(f"Incoming scrobble [Client: {client} | API: {api}]: {rawscrobble}")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user