I have a simple Api using Flask, that returns a list of strings using the code below:
from flask import Flask, request
from flask_restful import Resource, Api
from json import dumps
app = Flask(__name__)
api = Api(app)
class Product(Resource):
def get(self):
return {'products':['A','B','C','D','E','F','G','H','I','J','K']}
class Accounting(Resource):
def get(self):
return {'accounting':['1','2','3','4','5','6','7','8','9']}
api.add_resource(Product, '/')
api.add_resource(Accounting, '/')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=True)
When i use the following code, i can access the list that is defined in the Flask api. This lists out the contents of the "product" resource
<?php
$json = file_get_contents('http://product-service/');
$obj = json_decode($json);
$products = $obj->products;
echo "$products[0]";
?>
The problem i am having is in accessing the second resource called "accounting". I cannot see the resources or access them when using the following code (with PHP) or even when i just go to the home address of the webpage.
<?php
$json = file_get_contents('http://product-service/');
$obj = json_decode($json);
$accounts = $obj->accounting;
echo "$accounts[0]";
?>
Would anyone be able to point me in the right direction?
Thanks in advance.
Michael
You are mounting two APIs under one route: the root route "/", I would recommend to seperate the products and accounting into separate API points:
from flask import Flask, request
from flask_restful import Resource, Api
from json import dumps
app = Flask(__name__)
api = Api(app)
class Product(Resource):
def get(self):
return {'products':['A','B','C','D','E','F','G','H','I','J','K']}
class Accounting(Resource):
def get(self):
return {'accounting':['1','2','3','4','5','6','7','8','9']}
api.add_resource(Product, '/products')
api.add_resource(Accounting, '/accounting')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=True)