-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
42 lines (33 loc) · 1.35 KB
/
main.py
File metadata and controls
42 lines (33 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flask import jsonify
from tidal import create_playlist, delete_playlist, get_playlist_info
from utils import validate_playlist
def tidal(request):
"""Responds to HTTP requests
GET:
Returns playlist info and a list of tracks on the playlist.
POST:
Creates a playlist for with given track details and returns playlist link.
DELETE:
Deletes playlist of provided link and returns success message.
"""
if request.method == "GET":
playlist_link = request.args.get("link")
response = get_playlist_info(playlist_link)
return jsonify(response), 200
if request.method == "POST":
data = request.get_json()
# Validate data
result = validate_playlist(data)
if not result["is_valid"]:
return jsonify({"message": "Invalid values", "errors": result["errors"]}), 400
playlist_info = {"name": data["name"],
"description": data["description"]}
tracks = data["tracks"]
response = create_playlist(
track_list=tracks, playlist_info=playlist_info)
return jsonify(response), 201
if request.method == "DELETE":
playlist_link = request.args.get("link")
delete_playlist(playlist_link)
return jsonify({"message": "Done"}), 200
return jsonify({"message": "Try again"}), 400