From 8fd2ac9c153ccc7440a55cf403c161eda03cfef2 Mon Sep 17 00:00:00 2001 From: hayatedayon Date: Sat, 29 Sep 2018 18:27:10 +0900 Subject: [PATCH] feat(main.py, infra.py): Add get JSON from datastore. fix #433 @1.0h --- infra.py | 11 +++++++++++ main.py | 7 +++++-- tests/test_main.py | 5 +++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/infra.py b/infra.py index fca4418..561979a 100644 --- a/infra.py +++ b/infra.py @@ -66,6 +66,17 @@ def reset_datastore(user): pass +def get_datastore(user_id): + user = User.get_by_id(user_id) + if user: + obj = {u'words': user.words, + u'last_word': user.last_word, + u'count': user.count, + } + return obj + return {} + + def get_last_word_datastore(user): try: return user.last_word[-1] diff --git a/main.py b/main.py index 100e496..5fe5451 100644 --- a/main.py +++ b/main.py @@ -23,6 +23,7 @@ import webapp2 import domain +import infra GOOGLE_ASSISTANT_WELCOME_INTENT = u'Google Assistant Welcome Intent' ASK_CONTINUE_INTENT = u'Ask Continue Intent' @@ -32,8 +33,10 @@ class MainPage(webapp2.RequestHandler): def get(self): - self.response.headers['Content-Type'] = 'text/plain' - self.response.write(u'Hello, World!') + userId = self.request.get('id') + obj = infra.get_datastore(userId) + self.response.headers['Content-Type'] = 'application/json' + self.response.write(json.dumps(obj).encode('utf-8')) def post(self): json_data = self.request.body diff --git a/tests/test_main.py b/tests/test_main.py index 1fd5133..ad3de9f 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -40,10 +40,11 @@ def tearDown(self): def test_get(self): app = webtest.TestApp(main.app) - response = app.get('/') + response = app.get('/?id=TestId') + obj = json.loads(response.body) assert response.status_int == 200 - assert response.body == u'Hello, World!' + assert obj == {} def test_post_google_assistant_welcome_intent(self): app = webtest.TestApp(main.app)