Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Merge ced1f01 into 0c149da
Browse files Browse the repository at this point in the history
  • Loading branch information
gleybersonandrade committed Sep 1, 2020
2 parents 0c149da + ced1f01 commit 1d59f5d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions kytos/core/api_server.py
Expand Up @@ -12,7 +12,7 @@
from urllib.error import HTTPError, URLError
from urllib.request import urlopen, urlretrieve

from flask import Flask, jsonify, request, send_file
from flask import Blueprint, Flask, jsonify, request, send_file
from flask_cors import CORS
from flask_socketio import SocketIO, join_room, leave_room
from werkzeug.exceptions import HTTPException
Expand Down Expand Up @@ -98,7 +98,8 @@ def register_rest_endpoint(self, url, function, methods):
stacklevel=2)
if url.startswith('/'):
url = url[1:]
self._start_endpoint(f'/kytos/{url}', function, methods=methods)
self._start_endpoint(self.app, f'/kytos/{url}', function,
methods=methods)

def start_api(self):
"""Start this APIServer instance API.
Expand All @@ -123,7 +124,8 @@ def register_core_endpoint(self, rule, function, **options):
Not used by NApps, but controller.
"""
self._start_endpoint(self._CORE_PREFIX + rule, function, **options)
self._start_endpoint(self.app, self._CORE_PREFIX + rule, function,
**options)

def _register_web_ui(self):
"""Register routes to the admin-ui homepage."""
Expand Down Expand Up @@ -296,10 +298,15 @@ def register_napp_endpoints(self, napp):
Args:
napp (Napp): Napp instance to register new endpoints.
"""
napp_blueprint = Blueprint(napp.napp_id, __name__)

for function in self._get_decorated_functions(napp):
for rule, options in function.route_params:
absolute_rule = self.get_absolute_rule(rule, napp)
self._start_endpoint(absolute_rule, function, **options)
self._start_endpoint(napp_blueprint, absolute_rule, function,
**options)

self.app.register_blueprint(napp_blueprint)

@staticmethod
def _get_decorated_functions(napp):
Expand All @@ -323,14 +330,14 @@ def get_absolute_rule(cls, rule, napp):

# END decorator methods

def _start_endpoint(self, rule, function, **options):
def _start_endpoint(self, app, rule, function, **options):
"""Start ``function``'s endpoint.
Forward parameters to ``Flask.add_url_rule`` mimicking Flask
``@route`` decorator.
"""
endpoint = options.pop('endpoint', None)
self.app.add_url_rule(rule, endpoint, function, **options)
app.add_url_rule(rule, endpoint, function, **options)
self.log.info('Started %s - %s', rule,
', '.join(options.get('methods', self.DEFAULT_METHODS)))

Expand Down

0 comments on commit 1d59f5d

Please sign in to comment.