Skip to content

Commit

Permalink
feat(main.py, infra.py): Add get JSON from datastore. fix #433 @1.0h
Browse files Browse the repository at this point in the history
  • Loading branch information
hayatedayon committed Sep 29, 2018
1 parent f9a5233 commit 8fd2ac9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
11 changes: 11 additions & 0 deletions infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 5 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8fd2ac9

Please sign in to comment.