Untitled

 avatar
unknown
plain_text
4 months ago
1.8 kB
1
Indexable
from flask import Flask, jsonify, request

app = Flask(__name__)

port = 8000
host = 'localhost'

songs = [
    {
        'Nome':'So This Is Love?',
        'Artista': 'Van Halen',
        'Album': 'Fair Warning',
        'Estilo': "80's Rock"
    },
    {
        'Nome':'Girls Got Rhythm',
        'Artista': 'AC DC',
        'Album': 'Highway to Hell',
        'Estilo': 'Rock'
    },
    {
        'Nome':'Begging',
        'Artista': 'The Libertines',
        'Album': 'Up the Bracket',
        'Estilo': 'Indie Rock'
    }
]

@app.route('/')
def index():
    return "<h1>Seja Bem Vindo ao Musify</h1>"

@app.route('/songs')
def get_songs():
    return jsonify(songs)

@app.route('/songs/<int:id>', methods=['GET'])
def get_song(id):
    try:
        if songs[id] is not None:        
            return jsonify(songs[id])
    except:
        return jsonify('Não foi possível encontrar a música.')

@app.route('/songs', methods=['POST'])
def new_song():
    post = request.get_json()
    songs.append(post)
    return jsonify(songs, 200)

@app.route('/songs/<int:id>', methods=['PUT'])
def change_song(id):
    try:
        if songs[id] is not None:
            song = request.get_json()
            songs[id].update(song)
            return jsonify(songs[id], 200)
    except:
        return jsonify('Não foi possível encontrar a música.')

@app.route('/songs/<int:id>', methods=['DELETE'])
def delete_song(id):
    try:
        if songs[id] is not None:
            song = songs[id]
            del songs[id]
            return jsonify(f'Foí removida a música {song} da playlist.')
    except:
        return jsonify('Não foi possível encontrar a música.')

app.run(port=port, host=host, debug=True)
Editor is loading...
Leave a Comment