-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.py
155 lines (141 loc) · 3.67 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import flask
from flask import request, jsonify
app = flask.Flask(__name__)
app.config["DEBUG"] = True
# Create some test data for our catalog in the form of a list of dictionaries.
tracks = [
{
"AlbumId": 1,
"Bytes": 11170334,
"Composer": "Angus Young, Malcolm Young, Brian Johnson",
"GenreId": 1,
"MediaTypeId": 1,
"Milliseconds": 343719,
"Name": "For Those About To Rock (We Salute You)",
"TrackId": 1,
"UnitPrice": 0.99
},
{
"AlbumId": 2,
"Bytes": 5510424,
"Composer": "Angus Young, Malcolm Young, Brian Johnson",
"GenreId": 1,
"MediaTypeId": 2,
"Milliseconds": 342562,
"Name": "Balls to the Wall",
"TrackId": 2,
"UnitPrice": 0.99
},
{
"AlbumId": 3,
"Bytes": 3990994,
"Composer": "F. Baltes, S. Kaufman, U. Dirkscneider & W. Hoffman",
"GenreId": 1,
"MediaTypeId": 2,
"Milliseconds": 230619,
"Name": "Fast As a Shark",
"TrackId": 3,
"UnitPrice": 0.99
},
{
"AlbumId": 3,
"Bytes": 4331779,
"Composer": "F. Baltes, R.A. Smith-Diesel, S. Kaufman, U. Dirkscneider & W. Hoffman",
"GenreId": 1,
"MediaTypeId": 2,
"Milliseconds": 252051,
"Name": "Restless and Wild",
"TrackId": 4,
"UnitPrice": 0.99
},
{
"AlbumId": 3,
"Bytes": 6290521,
"Composer": "Deaffy & R.A. Smith-Diesel",
"GenreId": 1,
"MediaTypeId": 2,
"Milliseconds": 375418,
"Name": "Princess of the Dawn",
"TrackId": 5,
"UnitPrice": 0.99
},
{
"AlbumId": 1,
"Bytes": 6713451,
"Composer": "Angus Young, Malcolm Young, Brian Johnson",
"GenreId": 1,
"MediaTypeId": 1,
"Milliseconds": 205662,
"Name": "Put The Finger On You",
"TrackId": 6,
"UnitPrice": 0.99
},
{
"AlbumId": 1,
"Bytes": 7636561,
"Composer": "Angus Young, Malcolm Young, Brian Johnson",
"GenreId": 1,
"MediaTypeId": 1,
"Milliseconds": 233926,
"Name": "Let's Get It Up",
"TrackId": 7,
"UnitPrice": 0.99
},
{
"AlbumId": 1,
"Bytes": 6852860,
"Composer": "Angus Young, Malcolm Young, Brian Johnson",
"GenreId": 1,
"MediaTypeId": 1,
"Milliseconds": 210834,
"Name": "Inject The Venom",
"TrackId": 8,
"UnitPrice": 0.99
},
{
"AlbumId": 1,
"Bytes": 6599424,
"Composer": "Angus Young, Malcolm Young, Brian Johnson",
"GenreId": 1,
"MediaTypeId": 1,
"Milliseconds": 203102,
"Name": "Snowballed",
"TrackId": 9,
"UnitPrice": 0.99
},
{
"AlbumId": 1,
"Bytes": 8611245,
"Composer": "Angus Young, Malcolm Young, Brian Johnson",
"GenreId": 1,
"MediaTypeId": 1,
"Milliseconds": 263497,
"Name": "Evil Walks",
"TrackId": 10,
"UnitPrice": 0.99
}
]
@app.route('/', methods=['GET'])
def home():
return '''<h1>Welcome to New Project</h1>
<p>A prototype API for distant reading of Music Albums and Playlists.</p>'''
# A route to return all of the available entries in our catalog.
@app.route('/api/v1/resources/tracks/all', methods=['GET'])
def api_all():
return jsonify(tracks)
@app.route('/api/v1/resources/tracks', methods=['GET'])
def api_id():
#Check ID and If no ID is provided, display an error in the browser.
if 'id' in request.args:
id = int(request.args['id'])
else:
return "Error: No id field provided. Please specify an id."
# Empty List
results = []
# Check trackid in catalog and return the result
for track in tracks:
if track['TrackId'] == id:
results.append(track)
# Convert our list of python dictionaries to the JSON format.
return jsonify(results)
app.run()