✨ api
This commit is contained in:
21
app.py
21
app.py
@@ -1,5 +1,5 @@
|
||||
import os
|
||||
from flask import Flask, render_template, abort
|
||||
from flask import Flask, render_template, abort, jsonify, request
|
||||
from comics_data import COMICS
|
||||
|
||||
app = Flask(__name__)
|
||||
@@ -59,9 +59,28 @@ def about():
|
||||
return render_template('about.html', title='About')
|
||||
|
||||
|
||||
@app.route('/api/comics')
|
||||
def api_comics():
|
||||
"""API endpoint - returns all comics as JSON"""
|
||||
return jsonify(COMICS)
|
||||
|
||||
|
||||
@app.route('/api/comics/<int:comic_id>')
|
||||
def api_comic(comic_id):
|
||||
"""API endpoint - returns a specific comic as JSON"""
|
||||
comic = get_comic_by_number(comic_id)
|
||||
if not comic:
|
||||
return jsonify({'error': 'Comic not found'}), 404
|
||||
return jsonify(comic)
|
||||
|
||||
|
||||
@app.errorhandler(404)
|
||||
def page_not_found(e):
|
||||
"""404 error handler"""
|
||||
# Return JSON for API requests
|
||||
if request.path.startswith('/api/'):
|
||||
return jsonify({'error': 'Not found'}), 404
|
||||
# Return HTML for regular pages
|
||||
return render_template('404.html', title='Page Not Found'), 404
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user