2018-02-27 00:06:15 +01:00
|
|
|
import requests, json
|
|
|
|
from urllib.parse import urlencode
|
2018-02-26 22:55:01 +01:00
|
|
|
from functools import wraps
|
2018-02-27 00:06:15 +01:00
|
|
|
import api
|
2018-02-26 22:55:01 +01:00
|
|
|
|
2018-02-27 00:06:15 +01:00
|
|
|
# This will be overwritten by config
|
|
|
|
BASE_URL = "http://localhost:8080/api"
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
# Exceptions:
|
|
|
|
class APIError(Exception): pass
|
|
|
|
|
|
|
|
# decorator:
|
|
|
|
def request_post(func):
|
|
|
|
@wraps(func)
|
|
|
|
def new_func(*args, **kwargs):
|
|
|
|
url, data = func(*args, **kwargs)
|
2018-02-27 00:06:15 +01:00
|
|
|
response = requests.post(f"{BASE_URL}/{url}", data=data)
|
|
|
|
data = json.loads(response.text)
|
|
|
|
if "error" not in data or data["error"] != False:
|
|
|
|
print(data)
|
|
|
|
raise APIError(data["error_msg"])
|
|
|
|
return data["success"]
|
2018-02-26 22:55:01 +01:00
|
|
|
return new_func
|
|
|
|
def request_get(func):
|
|
|
|
@wraps(func)
|
|
|
|
def new_func(*args, **kwargs):
|
|
|
|
url = func(*args, **kwargs)
|
2018-02-27 00:06:15 +01:00
|
|
|
response = requests.get(f"{BASE_URL}/{url}")
|
|
|
|
data = json.loads(response.text)
|
|
|
|
if "error" not in data or data["error"] != False:
|
|
|
|
raise APIError(data["error_msg"])
|
|
|
|
return data["value"]
|
2018-02-26 22:55:01 +01:00
|
|
|
return new_func
|
|
|
|
|
|
|
|
# methods:
|
|
|
|
|
|
|
|
@request_post
|
2018-02-27 00:06:15 +01:00
|
|
|
def load_path(path:str):
|
|
|
|
args = urlencode(locals())
|
|
|
|
return f"load?{args}", None
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_get
|
|
|
|
def is_playing():
|
2018-02-27 00:06:15 +01:00
|
|
|
return f"play"
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_post
|
|
|
|
def set_playing(play:bool):
|
2018-02-27 00:06:15 +01:00
|
|
|
args = urlencode(locals())
|
|
|
|
return f"play?{args}", None
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_get
|
|
|
|
def get_volume():
|
2018-02-27 00:06:15 +01:00
|
|
|
return f"volume"
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_post
|
|
|
|
def set_volume(volume:int):# between 0 and 100 (you may also exceed 100)
|
2018-02-27 00:06:15 +01:00
|
|
|
args = urlencode(locals())
|
|
|
|
return f"volume?{args}", None
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_get
|
|
|
|
def get_playlist():
|
2018-02-27 00:06:15 +01:00
|
|
|
return f"playlist"
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_post
|
|
|
|
def playlist_next():
|
2018-02-27 00:06:15 +01:00
|
|
|
return f"playlist/next", None
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_post
|
|
|
|
def playlist_previous():
|
2018-02-27 00:06:15 +01:00
|
|
|
return f"playlist/previous", None
|