This is an automated email from the ASF dual-hosted git repository.

wiener pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new c76570e  [STREAMPIPES-187] initial work on rest endpoints
c76570e is described below

commit c76570e5c4e050dfc431e9e1fa0d806a915c9a17
Author: Patrick Wiener <[email protected]>
AuthorDate: Sun Aug 2 10:07:33 2020 +0200

    [STREAMPIPES-187] initial work on rest endpoints
---
 .../streampipes/api/__init__.py                    |  0
 .../streampipes/api/resources/__init__.py          |  0
 .../streampipes/api/resources/sepa.py              | 32 ++++++++
 .../streampipes/api/resources/welcome.py           | 31 ++++++++
 streampipes-wrapper-python/streampipes/api/rest.py | 85 +++-------------------
 5 files changed, 73 insertions(+), 75 deletions(-)

diff --git a/streampipes-wrapper-python/streampipes/api/__init__.py 
b/streampipes-wrapper-python/streampipes/api/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/streampipes-wrapper-python/streampipes/api/resources/__init__.py 
b/streampipes-wrapper-python/streampipes/api/resources/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/streampipes-wrapper-python/streampipes/api/resources/sepa.py 
b/streampipes-wrapper-python/streampipes/api/resources/sepa.py
new file mode 100644
index 0000000..f7eeefb
--- /dev/null
+++ b/streampipes-wrapper-python/streampipes/api/resources/sepa.py
@@ -0,0 +1,32 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from flask import Blueprint, request, make_response, jsonify
+from streampipes.manager import ProcessorDispatcher
+
+sepa_blueprint = Blueprint('sepa', __name__)
+
+
+@sepa_blueprint.route('/invoke', methods=['POST'])
+def invoke():
+    resp = ProcessorDispatcher.start(**request.json)
+    return make_response(jsonify(resp), 200)
+
+
+@sepa_blueprint.route('/detach', methods=['POST'])
+def detach():
+    resp = ProcessorDispatcher.stop(**request.json)
+    return make_response(jsonify(resp), 200)
diff --git a/streampipes-wrapper-python/streampipes/api/resources/welcome.py 
b/streampipes-wrapper-python/streampipes/api/resources/welcome.py
new file mode 100644
index 0000000..227e939
--- /dev/null
+++ b/streampipes-wrapper-python/streampipes/api/resources/welcome.py
@@ -0,0 +1,31 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from flask import Blueprint, make_response, jsonify, request
+
+welcome_blueprint = Blueprint('welcome', __name__)
+
+
+@welcome_blueprint.route('/', methods=['GET'])
+def get_welcome_page_html():
+
+    if request.content_type == 'application/json':
+        resp = {'welcome': 'hello-world!'}
+        return make_response(jsonify(resp), 200)
+
+    elif request.content_type == 'text/html':
+        return "<p>Got it!</p>"
+
diff --git a/streampipes-wrapper-python/streampipes/api/rest.py 
b/streampipes-wrapper-python/streampipes/api/rest.py
index 6871f01..702011e 100644
--- a/streampipes-wrapper-python/streampipes/api/rest.py
+++ b/streampipes-wrapper-python/streampipes/api/rest.py
@@ -15,7 +15,8 @@
 # limitations under the License.
 #
 """ API endpoints """
-import json
+import sys
+import threading
 
 from flask import Flask
 from waitress import serve
@@ -26,86 +27,20 @@ class FlaskProductionConfig(object):
     DEVELOPMENT = False
 
 
-class API(object):
-    """ EventProcessorAPI contains relevant RESTful endpoints to start and 
stop """
-    app = None
+class PipelineElementApi(object):
 
-    def __init__(self, port: int):
-        self.port = port
+    def __init__(self):
         self.app = Flask(__name__, instance_relative_config=False)
         self.app.config.from_object(FlaskProductionConfig)
 
         with self.app.app_context():
             # import endpoints
-            from streampipes.api import endpoints
+            from streampipes.api.resources import welcome
+            from streampipes.api.resources import sepa
 
             # register blueprints
-            self.app.register_blueprint(endpoints.welcome_blueprint)
-            self.app.register_blueprint(endpoints.sepa_blueprint)
+            self.app.register_blueprint(welcome.welcome_blueprint)
+            self.app.register_blueprint(sepa.sepa_blueprint)
 
-    def run(self):
-        serve(self.app, host='0.0.0.0', port=self.port)
-
-
-# from flask import request, jsonify, Flask, Response, make_response
-# from waitress import serve
-#
-# from streampipes.manager import ProcessorDispatcher
-#
-#
-# class EndpointAction(object):
-#     def __init__(self, action):
-#         self.action = action
-#
-#     def __call__(self, *args, **kwargs):
-#         """ call corresponding handler method """
-#         if request.get_json() is not None:
-#             resp = self.action(**request.get_json())
-#         else:
-#             resp = self.action()
-#
-#         return make_response(jsonify(resp), 200)
-#
-#
-# class API(object):
-#     """ EventProcessorAPI contains relevant RESTful endpoints to start and 
stop """
-#     app = None
-#
-#     def __init__(self, port: int):
-#         self.app = Flask('python-processor')
-#         self.port = port
-#         self.add_endpoints()
-#
-#     def run(self):
-#         serve(self.app, host='0.0.0.0', port=self.port)
-#
-#     def add_endpoints(self):
-#         """ define and add event processor API endpoints """
-#         self.add_endpoint(endpoint='/', endpoint_name='/', methods=['GET'], 
handler=self.welcome)
-#         self.add_endpoint(endpoint='/invoke', endpoint_name='/invoke', 
methods=['POST'], handler=self.invoke)
-#         self.add_endpoint(endpoint='/detach', endpoint_name='/detach', 
methods=['POST'], handler=self.detach)
-#
-#     def add_endpoint(self, endpoint=None, endpoint_name=None, handler=None, 
methods=None):
-#         self.app.add_url_rule(endpoint, endpoint_name, 
EndpointAction(handler), methods=methods)
-#
-#     @staticmethod
-#     def welcome():
-#         return {'welcome': 'hello-world!'}
-#
-#     @staticmethod
-#     def invoke(**kwargs):
-#         """ Receives invocation graph from pipeline management in the backend
-#
-#         :param json: contains invocation graph.
-#         :return: None.
-#         """
-#         return ProcessorDispatcher.start(**kwargs)
-#
-#     @staticmethod
-#     def detach(**kwargs):
-#         """ Receives elementId to be terminated from pipeline management in 
the backend
-#
-#         :param json: contains elementId to be terminated
-#         :return: None.
-#         """
-#         return ProcessorDispatcher.stop(**kwargs)
\ No newline at end of file
+    def run(self, port: int):
+        threading.Thread(target=serve, kwargs={'app': self.app, 'host': 
'0.0.0.0', 'port': port}).start()

Reply via email to