from flask import Flask, request, jsonify import json import re import string from flasgger import Swagger app = Flask(__name__) swagger = Swagger(app) # Load the API data with open('data.json', 'r') as file: api_data = json.loads(file.read()) @app.route('/api/songs', methods=['GET']) def get_songs(): """ Get a paginated list of songs --- parameters: - name: page in: query type: integer default: 1 description: Page number - name: page_size in: query type: integer default: 10 description: Number of items per page responses: 200: description: Successful response schema: properties: page: type: integer start: type: integer end: type: integer per_page: type: integer total_songs: type: integer total_pages: type: integer songs: type: array items: type: object 400: description: Page not found """ page = request.args.get('page', 1, type=int) per_page = request.args.get('page_size', 10, type=int) songs = api_data['clips'] # Calculate start and end indices for pagination start = (page - 1) * per_page if start > len(songs): return jsonify({"error": "Page not found"}), 400 end = start + per_page # Slice the songs list for the current page paginated_songs = songs[start:end] # Prepare the response response = { 'page': page, 'start': start, 'end': end, 'per_page': per_page, 'total_songs': len(songs), 'total_pages': (len(songs) + per_page - 1), 'songs': paginated_songs } return jsonify(response) if __name__ == '__main__': app.run(debug=True)