I'm creating the server end of a test API using flask_restful that will capture a request coming into the API with a ID, and 3 parameters. If the Id does not exists, then it is created and populated with the 3 data arguments. The Asset is created, however only the 3rd parameter is added to the dict.
Code I have:
from flask import Flask, request
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
api = Api(app)
assets = {}
def abort_if_asset_doesnt_exist(asset_id):
if asset_id not in assets:
abort(404, message="Asset {} doesn't exist".format(asset_id))
parser = reqparse.RequestParser()
parser.add_argument('data1', type=str)
parser.add_argument('data2', type=str)
parser.add_argument('data3', type=str)
class Asset(Resource):
def get(self, asset_id):
return {asset_id: assets[asset_id]}
def put(self, asset_id):
assets[asset_id] = request.form['data1']
assets[asset_id] = request.form['data2']
assets[asset_id] = request.form['data3']
return {asset_id: assets[asset_id]}
def delete(self, asset_id):
abort_if_todo_doesnt_exist(asset_id)
del assets[asset_id]
return '', 204
api.add_resource(Asset, '/api-v1.0/add/<string:asset_id>', methods=['PUT', 'GET'])
if __name__ == '__main__':
app.run(debug=True)
I want to take the id and create a 'instance' in the dictionary, with it's corresponding data fields attached to it. like below
What I want:
{
"123456" {
"data1":"Mydata1"
"data2":"Mydata2"
"data3":"Mydata3"
}
}
With this I can call the asset and receive it's associated data fields.
The curl (PUT) command I use looks like this.
$ curl http://localhost:5000/api-v1.0/add/123456 -d "data1=Mydata1" -d "data2=Mydata2" -d "data3=Mydata3" -X PUT