From 0e9e9ec015e14288f5d3153f1b21e545676bb2a4 Mon Sep 17 00:00:00 2001 From: Sahil Gulati Date: Thu, 2 May 2019 15:46:34 +0530 Subject: [PATCH 1/7] imported AskResponse from __init__.py --- flask_ask/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/flask_ask/__init__.py b/flask_ask/__init__.py index d878d12..640efbc 100644 --- a/flask_ask/__init__.py +++ b/flask_ask/__init__.py @@ -8,6 +8,7 @@ from .core import ( Ask, + AskResponse, request, session, version, From 84210459dfb8e426e0d78e2ce879369c89e76495 Mon Sep 17 00:00:00 2001 From: Sahil Gulati Date: Thu, 2 May 2019 15:53:59 +0530 Subject: [PATCH 2/7] Added functionality for sending custom response headers and statusCode --- flask_ask/core.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/flask_ask/core.py b/flask_ask/core.py index bf006c1..5e8322b 100644 --- a/flask_ask/core.py +++ b/flask_ask/core.py @@ -816,9 +816,25 @@ def _flask_view_func(self, *args, **kwargs): if result is not None: if isinstance(result, models._Response): return result.render_response() - return result + elif isinstance(result, tuple): + return self.makeVerboseResponse(result) return "", 400 + def makeVerboseResponse(self, result): + # checking tuple's for first index for `models._Response` type + if len(result) == 1 and isinstance(result[0], models._Response): + response = result[0].render_response() + askResponse = AskResponse() + # checking tuple's for first index for `models._Response` and second index for `AskResponse` type + elif len(result) >= 2 and isinstance(result[0], models._Response) and isinstance(result[1], AskResponse): + response = result[0].render_response() + askResponse = result[1] + else: + response = "" + askResponse = AskResponse() + # returning response in the terms tuple which contains response, statusCode and custom headers. + return (response, askResponse.statusCode, askResponse.headers) + def _map_intent_to_view_func(self, intent): """Provides appropiate parameters to the intent functions.""" if intent.name in self._intent_view_funcs: From d393ea7de38855f0b3fc34e573b1ae121b248bb2 Mon Sep 17 00:00:00 2001 From: Sahil Gulati Date: Thu, 2 May 2019 15:59:25 +0530 Subject: [PATCH 3/7] Added AskResponse for giving control over response headers, statusCode and contentType. --- flask_ask/core.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/flask_ask/core.py b/flask_ask/core.py index 5e8322b..e7f45c0 100644 --- a/flask_ask/core.py +++ b/flask_ask/core.py @@ -57,7 +57,16 @@ def dbgdump(obj, default=None, cls=None): _converters = {'date': to_date, 'time': to_time, 'timedelta': to_timedelta} - +# Will hold response related stuff like statusCode, content type and custom headers. +class AskResponse(): + def __init__(self, statusCode = 200, contentType = "application/json", customHeaders = {}): + self.statusCode = 200 + if isinstance(statusCode, int): + self.statusCode = statusCode + if isinstance(customHeaders, dict): + self.headers = customHeaders + self.headers["Content-Type"] = contentType + class Ask(object): """The Ask object provides the central interface for interacting with the Alexa service. @@ -819,7 +828,8 @@ def _flask_view_func(self, *args, **kwargs): elif isinstance(result, tuple): return self.makeVerboseResponse(result) return "", 400 - + + # Contains additional functionality with the support of previously existing one. def makeVerboseResponse(self, result): # checking tuple's for first index for `models._Response` type if len(result) == 1 and isinstance(result[0], models._Response): From a8ab55065ed0cfdd5e9132a0fa3705607b656f7d Mon Sep 17 00:00:00 2001 From: Sahil Gulati Date: Thu, 2 May 2019 16:23:06 +0530 Subject: [PATCH 4/7] Added section for defining some custom and pre-defined headers. --- flask_ask/core.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/flask_ask/core.py b/flask_ask/core.py index e7f45c0..89ef8ab 100644 --- a/flask_ask/core.py +++ b/flask_ask/core.py @@ -59,13 +59,20 @@ def dbgdump(obj, default=None, cls=None): # Will hold response related stuff like statusCode, content type and custom headers. class AskResponse(): - def __init__(self, statusCode = 200, contentType = "application/json", customHeaders = {}): + def __init__(self, statusCode = 200, contentType = "application/json", customHeaders = {}): self.statusCode = 200 + self.contentType = contentType if isinstance(statusCode, int): self.statusCode = statusCode if isinstance(customHeaders, dict): self.headers = customHeaders - self.headers["Content-Type"] = contentType + + #added a function for giving some pre-defined and custom headers. + def getHeaders(self, request_type): + self.headers["x-request-type"] = request_type + self.headers["x-dev-edition"] = "flask-ask-devs" + self.headers["Content-Type"] = self.contentType + return self.headers class Ask(object): """The Ask object provides the central interface for interacting with the Alexa service. @@ -826,7 +833,7 @@ def _flask_view_func(self, *args, **kwargs): if isinstance(result, models._Response): return result.render_response() elif isinstance(result, tuple): - return self.makeVerboseResponse(result) + return self.makeVerboseResponse(result, request_type) return "", 400 # Contains additional functionality with the support of previously existing one. @@ -843,7 +850,7 @@ def makeVerboseResponse(self, result): response = "" askResponse = AskResponse() # returning response in the terms tuple which contains response, statusCode and custom headers. - return (response, askResponse.statusCode, askResponse.headers) + return (response, askResponse.statusCode, askResponse.getHeaders(request_type)) def _map_intent_to_view_func(self, intent): """Provides appropiate parameters to the intent functions.""" From 1f9e5242d0ceef888c44b80873991c4190a70309 Mon Sep 17 00:00:00 2001 From: Sahil Gulati Date: Thu, 2 May 2019 16:25:41 +0530 Subject: [PATCH 5/7] Corrected indentation. --- flask_ask/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_ask/core.py b/flask_ask/core.py index 89ef8ab..70dd012 100644 --- a/flask_ask/core.py +++ b/flask_ask/core.py @@ -59,7 +59,7 @@ def dbgdump(obj, default=None, cls=None): # Will hold response related stuff like statusCode, content type and custom headers. class AskResponse(): - def __init__(self, statusCode = 200, contentType = "application/json", customHeaders = {}): + def __init__(self, statusCode = 200, contentType = "application/json", customHeaders = {}): self.statusCode = 200 self.contentType = contentType if isinstance(statusCode, int): From c975afe185a6652e33cb8d79adcfeaae2f30bdb0 Mon Sep 17 00:00:00 2001 From: Sahil Gulati Date: Thu, 2 May 2019 16:27:21 +0530 Subject: [PATCH 6/7] Corrected parameters for request_type header. --- flask_ask/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_ask/core.py b/flask_ask/core.py index 70dd012..82bd7b8 100644 --- a/flask_ask/core.py +++ b/flask_ask/core.py @@ -837,7 +837,7 @@ def _flask_view_func(self, *args, **kwargs): return "", 400 # Contains additional functionality with the support of previously existing one. - def makeVerboseResponse(self, result): + def makeVerboseResponse(self, result, request_type): # checking tuple's for first index for `models._Response` type if len(result) == 1 and isinstance(result[0], models._Response): response = result[0].render_response() From 2f2505d68cc3d6a69029b2fbfbf86d1456d60841 Mon Sep 17 00:00:00 2001 From: Sahil Gulati Date: Thu, 2 May 2019 16:32:23 +0530 Subject: [PATCH 7/7] Added changes as per new features. --- samples/helloworld/helloworld.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/helloworld/helloworld.py b/samples/helloworld/helloworld.py index 0daba43..d41aa91 100644 --- a/samples/helloworld/helloworld.py +++ b/samples/helloworld/helloworld.py @@ -2,7 +2,7 @@ import os from flask import Flask -from flask_ask import Ask, request, session, question, statement +from flask_ask import Ask, AskResponse, request, session, question, statement app = Flask(__name__) @@ -13,7 +13,7 @@ @ask.launch def launch(): speech_text = 'Welcome to the Alexa Skills Kit, you can say hello' - return question(speech_text).reprompt(speech_text).simple_card('HelloWorld', speech_text) + return (question(speech_text).reprompt(speech_text).simple_card('HelloWorld', speech_text), AskResponse()) @ask.intent('HelloWorldIntent') @@ -25,7 +25,7 @@ def hello_world(): @ask.intent('AMAZON.HelpIntent') def help(): speech_text = 'You can say hello to me!' - return question(speech_text).reprompt(speech_text).simple_card('HelloWorld', speech_text) + return (question(speech_text).reprompt(speech_text).simple_card('HelloWorld', speech_text), AskResponse(statusCode = 500, contentType = "application/json", customHeaders = {"x-error-cause": "Internal server error!"})) @ask.session_ended