Solution
from json import JSONEncoder
class PetEncoder(JSONEncoder):
def default(self, o):
return o.__dict__
# ... ... ...
# A route to return all of the available entries in our catalog.
@app.route('/api/v1/resources/pets/all', methods=['GET'])
def api_all():
allpets = [PetEncoder().encode(pet) for pet in pets]
return jsonify(allpets)
import flask
from flask import request, jsonify
import uuid
from json import JSONEncoder
app = flask.Flask(__name__)
app.config["DEBUG"] = True
class PetEncoder(JSONEncoder):
def default(self, o):
return o.__dict__
class Pet:
def __init__(self,id, name, dateOfBirth):
self.id = id.hex
self.name = name
self.dateOfBirth = dateOfBirth
# Create some test data for our catalog in the form of a list of dictionaries.
pets = [ Pet(uuid.uuid4(),'Hamster', '02.12.2019' )]
pets.append( Pet(uuid.uuid4(),'Fish', '07.03.1985' ))
pets.append( Pet(uuid.uuid4(),'Dog', '26.11.2000' ))
pets.append( Pet(uuid.uuid4(),'Cat', '17.05.2021' ))
@app.route('/', methods=['GET'])
def home():
return "Petshop Archive
This site is a prototype API for the petshop archive.
"
# A route to return all of the available entries in our catalog.
@app.route('/api/v1/resources/pets/all', methods=['GET'])
def api_all():
allpets = [PetEncoder().encode(pet) for pet in pets]
return jsonify(allpets)
if __name__ == '__main__':
app.run(host='0.0.0.0')