I am currently trying to understand how to create a REST API using Python (Flask) which will allow user to GET a set of data represented in JSON format by browsing the url given as : localhost:5000/DPM. I wrote the following Python script but receive errors as such:
File "mysql.py", line 23, in api.add_resource(DPM, '/DPM') File "C:\Program Files\Anaconda3\lib\site-packages\flask_restful__init__.py", line 404, in add_resource self._register_view(self.app, resource, *urls, **kwargs) File "C:\Program Files\Anaconda3\lib\site-packages\flask_restful__init__.py", line 444, in _register_view resource_func = self.output(resource.as_view(endpoint, *resource_class_args, AttributeError: type object 'DPM' has no attribute 'as_view'
from flask import Flask
from flask_restful import Resource, Api
from sqlalchemy import create_engine
import json
app = Flask(__name__)
api = Api(app)
class DPM:
def __init__(self, time, month):
self.time = time
self.month = month
energy = DPM('[12.18]','11')
def jdefault(o):
return o.__dict__
print(json.dumps(energy, default=jdefault, indent=4))
api.add_resource(DPM, '/DPM')
if __name__ == '__main__':
app.run(debug=True)
Where did I do wrong here?