Skip to content

Commit

Permalink
updated api.v1.info.get_info to use application logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonzhuyx committed Jun 1, 2019
1 parent 3391c43 commit 7561c2a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
16 changes: 13 additions & 3 deletions docs/templ/example/proj/api/ver/info.py.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
api/{{__API_VERSION__}}/info.py
"""
import flask
import json
import jsonpickle
import os

from {{__PROJECT_FOLDER_AS_PYTHON_TOP_MODULE_NAME__}}.app_connexion import application
from {{__PROJECT_FOLDER_AS_PYTHON_TOP_MODULE_NAME__}}.app_config import \
api_name, api_desc, api_version, \
api_path, \
api_docs_url, \
api_spec, api_spec_file, api_spec_path, \
app_env
from {{__PROJECT_FOLDER_AS_PYTHON_TOP_MODULE_NAME__}}.utils.logger import get_logger

LOGGER = get_logger(__name__)
LOGGER = application.logger


def get_api_doc():
Expand All @@ -39,7 +41,7 @@ def get_info():
"""
base_url = flask.request.host_url.strip('/')

return {
res = {
"name": api_name,
"version": api_version,
"description": api_desc,
Expand All @@ -48,3 +50,11 @@ def get_info():
"swaggerFile": '{}/{}'.format(base_url, api_spec),
"swaggerUi": '{}/{}'.format(base_url, api_docs_url),
}
if app_env != 'prod':
req = json.loads(jsonpickle.encode(
flask.request, unpicklable=False, max_depth=5))
LOGGER.info('Request: %s', json.dumps(req, sort_keys=True, indent=2))
res.update({
"~/request": req
})
return res
2 changes: 1 addition & 1 deletion docs/templ/example/proj/app_connexion.py.templ
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def root():
return redirect('/api/ui', code=302)


# application = app.app # expose global WSGI application object
application = app.app # expose global WSGI application object

config_connexion()

Expand Down
8 changes: 6 additions & 2 deletions docs/templ/example/tests/test_api_ver_info.py.templ
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import os
import logging
import unittest

from mock import patch
from mock import MagicMock, patch
from {{__PROJECT_FOLDER_AS_PYTHON_TOP_MODULE_NAME__}}.api.{{__API_VERSION__}}.info import get_api_doc
from {{__PROJECT_FOLDER_AS_PYTHON_TOP_MODULE_NAME__}}.api.{{__API_VERSION__}}.info import get_info

Expand Down Expand Up @@ -46,9 +46,13 @@ class ApiInfoTester(unittest.TestCase):
pass

@patch('{{__PROJECT_FOLDER_AS_PYTHON_TOP_MODULE_NAME__}}.api.{{__API_VERSION__}}.info.flask')
def test_get_info(self, mock_flask):
@patch('{{__PROJECT_FOLDER_AS_PYTHON_TOP_MODULE_NAME__}}.api.v1.info.jsonpickle')
@patch('{{__PROJECT_FOLDER_AS_PYTHON_TOP_MODULE_NAME__}}.api.v1.info.json')
def test_get_info(self, mock_json, mock_pickle, mock_flask):
"""
test {{__PROJECT_FOLDER_AS_PYTHON_TOP_MODULE_NAME__}}.api.{{__API_VERSION__}}.info.get_info
"""
mock_flask.request = MagicMock()
mock_pickle.encode.return_value = {}
result = get_info()
self.assertEqual(result.get('name'), '{{__PROJECT_TITLE__}}')
16 changes: 13 additions & 3 deletions ml/api/v1/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
ml/api/v1/info.py
"""
import flask
import json
import jsonpickle
import os

from ml.app_connexion import application
from ml.app_config import \
api_name, api_desc, api_version, \
api_path, \
api_docs_url, \
api_spec, api_spec_file, api_spec_path, \
app_env
from ml.utils.logger import get_logger

LOGGER = get_logger(__name__)
LOGGER = application.logger


def get_api_doc():
Expand All @@ -39,7 +41,7 @@ def get_info():
"""
base_url = flask.request.host_url.strip('/')

return {
res = {
"name": api_name,
"version": api_version,
"description": api_desc,
Expand All @@ -48,3 +50,11 @@ def get_info():
"swaggerFile": '{}/{}'.format(base_url, api_spec),
"swaggerUi": '{}/{}'.format(base_url, api_docs_url),
}
if app_env != 'prod':
req = json.loads(jsonpickle.encode(
flask.request, unpicklable=False, max_depth=5))
LOGGER.info('Request: %s', json.dumps(req, sort_keys=True, indent=2))
res.update({
"~/request": req
})
return res
2 changes: 1 addition & 1 deletion ml/app_connexion.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def root():
return redirect('/api/ui', code=302)


# application = app.app # expose global WSGI application object
application = app.app # expose global WSGI application object

config_connexion()

Expand Down
8 changes: 6 additions & 2 deletions tests/test_api_v1_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import unittest

from mock import patch
from mock import MagicMock, patch
from ml.api.v1.info import get_api_doc
from ml.api.v1.info import get_info

Expand Down Expand Up @@ -46,9 +46,13 @@ def test_get_api_doc(self, mock_flask):
pass

@patch('ml.api.v1.info.flask')
def test_get_info(self, mock_flask):
@patch('ml.api.v1.info.jsonpickle')
@patch('ml.api.v1.info.json')
def test_get_info(self, mock_json, mock_pickle, mock_flask):
"""
test ml.api.v1.info.get_info
"""
mock_flask.request = MagicMock()
mock_pickle.encode.return_value = {}
result = get_info()
self.assertEqual(result.get('name'), 'API Service')

0 comments on commit 7561c2a

Please sign in to comment.