grzegorz-clients/grzegorz_clients/api.py

180 lines
4.4 KiB
Python
Raw Permalink Normal View History

import requests, json
from urllib.parse import urlencode
2018-02-26 22:55:01 +01:00
from functools import wraps
# This must be set to be able to use it on remote hosts
BASE_URL = "http://localhost:8080/api"
def set_endpoint(base_url:str):
global BASE_URL
BASE_URL = base_url
2018-02-26 22:55:01 +01:00
# Exceptions:
class APIError(Exception): pass
def parse_message(
method: str,
url: str,
status: str,
function_name: str,
2024-05-18 19:02:18 +02:00
json_text: str,
is_get: bool,
) -> dict[str, any]:
prefix = f"[{function_name}] {method} /{url} -> {status}:"
try:
data = json.loads(json_text)
except json.JSONDecodeError:
raise APIError(f"{prefix} Expected json response, got:\n{json_text}")
if type(data) is not dict:
raise APIError(f"{prefix} Expected json response to be a dict, got:\n{json_text}")
if "error" not in data:
raise APIError(f"{prefix} Missing json data 'error', got:\n{json_text}")
if data["error"] != False:
raise APIError(f"{prefix} Got error {str(data['error'])}, got:\n{json_text}")
2024-05-18 19:02:18 +02:00
if not is_get and "success" not in data:
raise APIError(f"{prefix} Missing json data 'success', got:\n{json_text}")
return data
2018-02-26 22:55:01 +01:00
# decorator:
# (TODO): Add logging
def request_delete(func):
@wraps(func)
def new_func(*args, **kwargs):
url, data = func(*args, **kwargs)
response = requests.delete(f"{BASE_URL}/{url}", json=data)
response.raise_for_status() # raises HTTPError, if any
data = parse_message(
"DELETE",
url,
response.status_code,
func.__name__,
response.text,
2024-05-18 19:02:18 +02:00
is_get = False,
)
return data["success"]
return new_func
2018-02-26 22:55:01 +01:00
def request_post(func):
@wraps(func)
def new_func(*args, **kwargs):
url, data = func(*args, **kwargs)
response = requests.post(f"{BASE_URL}/{url}", json=data)
response.raise_for_status() # raises HTTPError, if any
data = parse_message(
"POST",
url,
response.status_code,
func.__name__,
response.text,
2024-05-18 19:02:18 +02:00
is_get = False,
)
return data["success"]
2018-02-26 22:55:01 +01:00
return new_func
2018-02-26 22:55:01 +01:00
def request_get(func):
@wraps(func)
def new_func(*args, **kwargs):
url = func(*args, **kwargs)
response = requests.get(f"{BASE_URL}/{url}")
response.raise_for_status() # raises HTTPError, if any
data = parse_message(
"GET",
url,
response.status_code,
func.__name__,
response.text,
2024-05-18 19:02:18 +02:00
is_get = True,
)
if "value" not in data:
raise APIError(f"[{func.__name__}] Missing json data 'value', got:\n{json.dumps(data)}")
return data["value"]
2018-02-26 22:55:01 +01:00
return new_func
# methods:
@request_post
def load_path(path:str, data: dict = None):
args = urlencode(locals())
return f"load?{args}", data
2018-02-26 22:55:01 +01:00
@request_get
def is_playing():
return "play"
2018-02-26 22:55:01 +01:00
@request_post
def set_playing(play: bool):
args = urlencode(locals())
return f"play?{args}", None
2018-02-26 22:55:01 +01:00
@request_get
def get_volume():
return "volume"
2018-02-26 22:55:01 +01:00
@request_post
2024-03-31 04:46:54 +02:00
def set_volume(volume: float): # between 0 and 100 (you may also exceed 100)
args = urlencode(locals())
return f"volume?{args}", None
2018-02-26 22:55:01 +01:00
@request_get
def get_playlist():
return "playlist"
2018-02-26 22:55:01 +01:00
@request_post
def playlist_next():
return "playlist/next", None
2018-02-26 22:55:01 +01:00
@request_post
def playlist_goto(index: int):
args = urlencode(locals())
return f"playlist/goto?{args}", None
2018-02-26 22:55:01 +01:00
@request_post
def playlist_previous():
return "playlist/previous", None
2018-02-28 22:17:44 +01:00
2018-03-06 14:51:04 +01:00
@request_post
def playlist_shuffle():
return "playlist/shuffle", None
2018-03-06 14:51:04 +01:00
@request_delete
def playlist_clear():
return "playlist", None
@request_delete
def playlist_remove(index: int):
args = urlencode(locals())
return f"playlist?{args}", None
@request_post
def playlist_move(index1: int, index2: int):
args = urlencode(locals())
return f"playlist/move?{args}", None
2019-03-13 11:37:49 +01:00
@request_get
def get_playlist_looping():
return "playlist/loop"
2019-03-13 11:37:49 +01:00
@request_post
def playlist_set_looping(looping: bool):
2019-03-13 11:37:49 +01:00
return f"playlist/loop?loop={str(bool(looping)).lower()}", None
2018-02-28 22:17:44 +01:00
@request_get
def get_playback_pos():
return "time"
2018-02-28 22:17:44 +01:00
@request_post
def seek_absolute(pos: float):
2018-02-28 22:17:44 +01:00
args = urlencode(locals())
return f"time?{args}", None
@request_post
def seek_percent(percent: int):
2018-02-28 22:17:44 +01:00
args = urlencode(locals())
return f"time?{args}", None