forked from luwo1701/Party-Queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
party_queue_api.py
executable file
·137 lines (121 loc) · 5.66 KB
/
party_queue_api.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote
from models import Account
from models import Playlist
from models import Song
from party_queue_api_messages import AccountRequest
from party_queue_api_messages import AccountResponse
from party_queue_api_messages import PlaylistRequest
from party_queue_api_messages import PlaylistResponse
from party_queue_api_messages import MultiplePlaylistResponse
from party_queue_api_messages import AddSongRequest
from party_queue_api_messages import SongMessage
from party_queue_api_messages import VoteSongRequest
from party_queue_api_messages import VoteSongResponse
# TODO: Add authorized clients
package = 'party-queue'
@endpoints.api(name='party_queue', version='v1')
class PartyQueueApi(remote.Service):
""" PARTY QUEUE API """
@staticmethod
def build_playlist_response(playlists):
""" Builds a playlist response for the given playlists """
response = MultiplePlaylistResponse(playlists=[])
for pl in playlists:
playlist = PlaylistResponse(pid=pl.key.id(),
name=pl.name,
songs=[])
songs = Song.find_by_playlist(pl.key).fetch()
for song in songs:
playlist.songs.append(SongMessage(id=song.key.id(),
spotify_id=song.spotify_id,
name=song.name,
vote_count=song.vote_count))
response.playlists.append(playlist)
return response
@endpoints.method(AccountRequest, AccountResponse,
path='signup', http_method='POST',
name='party-queue.signup')
def signup(self, request):
""" Adds a new user to the datastore
"""
# TODO: Add user authentication. Currently, we will create an acct
new_user = Account.add_new_user(request)
if new_user is None:
return AccountResponse(errmsg="Username already exists!")
return AccountResponse(id=new_user.key.id())
@endpoints.method(AccountRequest, AccountResponse,
path='login', http_method='POST',
name='party-queue.login')
def login(self, request):
""" logs in a user based on username """
user = Account.find_by_username(request.username)
if user is None:
print "User not found"
return AccountResponse(errmsg="Username not recognized")
return AccountResponse(id=user.key.id())
@endpoints.method(PlaylistRequest, PlaylistResponse,
http_method='POST',
name='party-queue.create_playlist')
def create_playlist(self, request):
""" Creates a playlist for the user """
# TODO: Max amount of playlists at 20 for a user
user = Account.find_by_id(request.userid)
if user is None:
print "User not found"
return PlaylistResponse(errmsg="User ID not found")
new_pl = Playlist.add_new_playlist(user.key, request.name)
return PlaylistResponse(pid=new_pl.key.id())
@endpoints.method(PlaylistRequest, PlaylistResponse,
http_method='GET',
name='party-queue.get_playlist')
def get_playlist_by_id(self, request):
""" Returns a playlist based on the plalist id """
pl = Playlist.find_by_id(request.pid)
response = PlaylistResponse(pid=pl.key.id(),
name=pl.name,
songs=[])
songs = Song.find_by_playlist(pl.key).fetch()
for song in songs:
response.songs.append(SongMessage(id=song.key.id(),
spotify_id=song.spotify_id,
name=song.name,
vote_count=song.vote_count))
return response
@endpoints.method(PlaylistRequest, MultiplePlaylistResponse,
http_method='GET',
name='party-queue.get_playlists_for_user')
def get_playlists_for_user(self, request):
""" Gets all playlists and songs in each playlist for a user's id """
user = Account.find_by_id(request.userid)
playlists = Playlist.find_by_owner(user.key).fetch(20)
return self.build_playlist_response(playlists)
@endpoints.method(AccountRequest, MultiplePlaylistResponse,
http_method='GET',
name='party-queue.get_playlists_for_user_by_name')
def get_playlists_for_user_by_name(self, request):
""" Gets all playlists and songs in each playlist for a user's id """
user = Account.find_by_username(request.username)
playlists = Playlist.find_by_owner(user.key).fetch(20)
return self.build_playlist_response(playlists)
@endpoints.method(AddSongRequest, PlaylistResponse,
http_method='POST',
name='party-queue.add_song')
def add_song_to_playlist(self, request):
song = Playlist.add_song(request)
return PlaylistResponse(song_id=song.key.id())
@endpoints.method(VoteSongRequest, VoteSongResponse,
http_method='POST',
name='party-queue.upvote')
def upvote_song(self, request):
Song.upvote(request.id)
return VoteSongResponse(id=request.id)
@endpoints.method(VoteSongRequest, VoteSongResponse,
http_method='POST',
name='party-queue.downvote')
def downvote_song(self, request):
Song.downvote(request.id)
return VoteSongResponse(id=request.id)
APPLICATION = endpoints.api_server([PartyQueueApi])