فهرست منبع

update REST API URLs

nico 8 سال پیش
والد
کامیت
5668c3ccbb
2فایلهای تغییر یافته به همراه22 افزوده شده و 18 حذف شده
  1. 10 10
      Docs/rest_api.md
  2. 12 8
      kalliope/core/RestAPI/FlaskAPI.py

+ 10 - 10
Docs/rest_api.md

@@ -4,12 +4,12 @@ Kalliope provides the REST API to manage the synapses. For configuring the API r
 
 ## Synapse API
 
-| Method | URL                      | Action                      |
-|--------|--------------------------|-----------------------------|
-| GET    | /synapses                | List synapses               |
-| GET    | /synapses/<synapse_name> | Show synapse details        |
-| POST   | /synapses/<synapse_name> | Run a synapse by its name   |
-| POST   | /order                   | Run a synapse from an order |
+| Method | URL                               | Action                          |
+|--------|-----------------------------------|---------------------------------|
+| GET    | /synapses                         | List synapses                   |
+| GET    | /synapses/<synapse_name>          | Get synapse details by name     |
+| POST   | /synapses/start/id/<synapse_name> | Run a synapse by its name       |
+| POST   | /synapses/start/order             | Run a synapse from a text order |
 
 ## Curl examples
 
@@ -21,7 +21,7 @@ Normal response codes: 200
 Error response codes: unauthorized(401), itemNotFound(404)
 Curl command:
 ```bash
-curl -i --user admin:secret -X GET  http://localhost:5000/synapses/
+curl -i --user admin:secret -X GET  http://localhost:5000/synapses
 ```
 
 Output example:
@@ -107,7 +107,7 @@ Normal response codes: 201
 Error response codes: unauthorized(401), itemNotFound(404)
 Curl command:
 ```bash
-curl -i --user admin:secret -X POST  http://localhost:5000/synapses/say-hello
+curl -i --user admin:secret -X POST  http://localhost:5000/synapses/start/id/say-hello
 ```
 
 Output example:
@@ -141,7 +141,7 @@ Error response codes: unauthorized(401), itemNotFound(404)
 
 Curl command:
 ```bash
-curl -i --user admin:secret -H "Content-Type: application/json" -X POST -d '{"order":"my order"}' http://localhost:5000/order/
+curl -i --user admin:secret -H "Content-Type: application/json" -X POST -d '{"order":"my order"}' http://localhost:5000/synapses/start/order
 ```
 
 If the order contains accent or quotes, use a file for testing with curl
@@ -151,7 +151,7 @@ cat post.json
 ```
 Then
 ```bash
-curl -i --user admin:secret -H "Content-Type: application/json" -X POST --data @post.json http://localhost:5000/order/
+curl -i --user admin:secret -H "Content-Type: application/json" -X POST --data @post.json http://localhost:5000/synapses/start/order
 ```
 
 Output example if the order have matched and so launched synapses:

+ 12 - 8
kalliope/core/RestAPI/FlaskAPI.py

@@ -13,6 +13,7 @@ from kalliope.core.SynapseLauncher import SynapseLauncher
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
+
 class FlaskAPI(threading.Thread):
     def __init__(self, app, port=5000, brain=None, allowed_cors_origin=False):
         """
@@ -38,11 +39,10 @@ class FlaskAPI(threading.Thread):
         # Add routing rules
         self.app.add_url_rule('/synapses', view_func=self.get_synapses, methods=['GET'])
         self.app.add_url_rule('/synapses/<synapse_name>', view_func=self.get_synapse, methods=['GET'])
-        self.app.add_url_rule('/synapses/<synapse_name>', view_func=self.run_synapse, methods=['POST'])
-        self.app.add_url_rule('/order/', view_func=self.run_order, methods=['POST'])
+        self.app.add_url_rule('/synapses/start/id/<synapse_name>', view_func=self.run_synapse_by_name, methods=['POST'])
+        self.app.add_url_rule('/synapses/start/order', view_func=self.run_synapse_by_order, methods=['POST'])
         self.app.add_url_rule('/shutdown/', view_func=self.shutdown_server, methods=['POST'])
 
-
     def run(self):
         self.app.run(host='0.0.0.0', port="%s" % int(self.port), debug=True, threaded=True, use_reloader=False)
 
@@ -65,6 +65,8 @@ class FlaskAPI(threading.Thread):
     def get_synapses(self):
         """
         get all synapses.
+        test with curl:
+        curl -i --user admin:secret  -X GET  http://127.0.0.1:5000/synapses
         """
         data = jsonify(synapses=[e.serialize() for e in self.brain.synapses])
         return data, 200
@@ -73,6 +75,8 @@ class FlaskAPI(threading.Thread):
     def get_synapse(self, synapse_name):
         """
         get a synapse by its name
+        test with curl:
+        curl --user admin:secret -i -X GET  http://127.0.0.1:5000/synapses/say-hello-en
         """
         synapse_target = self._get_synapse_by_name(synapse_name)
         if synapse_target is not None:
@@ -85,11 +89,11 @@ class FlaskAPI(threading.Thread):
         return jsonify(error=data), 404
 
     @requires_auth
-    def run_synapse(self, synapse_name):
+    def run_synapse_by_name(self, synapse_name):
         """
         Run a synapse by its name
         test with curl:
-        curl -i --user admin:secret -X POST  http://localhost:5000/synapses/say-hello
+        curl -i --user admin:secret -X POST  http://127.0.0.1:5000/synapses/start/id/say-hello-fr
         :param synapse_name:
         :return:
         """
@@ -103,15 +107,15 @@ class FlaskAPI(threading.Thread):
 
         # run the synapse
         SynapseLauncher.start_synapse(synapse_name, brain=self.brain)
-        data = jsonify(synapses=synapse_target)
+        data = jsonify(synapses=synapse_target.serialize())
         return data, 201
 
     @requires_auth
-    def run_order(self):
+    def run_synapse_by_order(self):
         """
         Give an order to Kalliope via API like it was from a spoken one
         Test with curl
-        curl -i --user admin:secret -H "Content-Type: application/json" -X POST -d '{"order":"my order"}' http://localhost:5000/order
+        curl -i --user admin:secret -H "Content-Type: application/json" -X POST -d '{"order":"my order"}' http://localhost:5000/synapses/start/order
         In case of quotes in the order or accents, use a file
         cat post.json:
         {"order":"j'aime"}