From fae2ea5180ced44517a1c78c359a23c09800f050 Mon Sep 17 00:00:00 2001 From: MiguelPeralvo Date: Thu, 10 May 2018 12:36:03 +0100 Subject: [PATCH] Fix dependency issue based on https://github.com/ssola/python-flask-microservice/issues/3 and https://github.com/ssola/python-flask-microservice/pull/4/commits/ee517d76c48bcca99b8404f43a6f0ada9af6bfd8. --- api/items.py | 10 +++++----- app.py | 15 ++++++++++++++- services/__init__.py | 0 services/provider.py | 12 ++++++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 services/__init__.py create mode 100644 services/provider.py diff --git a/api/items.py b/api/items.py index 4dcfd80..5e0db39 100644 --- a/api/items.py +++ b/api/items.py @@ -1,7 +1,7 @@ -items = { - 0: {"name": "First item"} -} +from flask_injector import inject +from services.provider import ItemsProvider -def search() -> list: - return items \ No newline at end of file +@inject +def search(data_provider: ItemsProvider) -> list: + return data_provider.get() \ No newline at end of file diff --git a/app.py b/app.py index c5f0b4f..689a4ad 100644 --- a/app.py +++ b/app.py @@ -1,8 +1,21 @@ -from connexion.resolver import RestyResolver import connexion +from injector import Binder +from flask_injector import FlaskInjector +from connexion.resolver import RestyResolver + +from services.provider import ItemsProvider + + +def configure(binder: Binder) -> Binder: + binder.bind( + ItemsProvider, + ItemsProvider([{"Name": "Test 1"}]) + ) + if __name__ == '__main__': app = connexion.App(__name__, specification_dir='swagger/') app.add_api('app.yaml', resolver=RestyResolver('api')) + FlaskInjector(app=app.app, modules=[configure]) app.run(port=9090) \ No newline at end of file diff --git a/services/__init__.py b/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/services/provider.py b/services/provider.py new file mode 100644 index 0000000..c331eaa --- /dev/null +++ b/services/provider.py @@ -0,0 +1,12 @@ +class ItemsProvider(object): + def __init__(self, items: list = []): + self._items = items + + def get(self, number_of_items: int = 5) -> list: + if not self._items: + return [] + + if number_of_items > len(self._items): + number_of_items = len(self._items) + + return self._items[0:number_of_items] \ No newline at end of file