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, diff --git a/flask_ask/core.py b/flask_ask/core.py index bf006c1..82bd7b8 100644 --- a/flask_ask/core.py +++ b/flask_ask/core.py @@ -57,7 +57,23 @@ 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 + self.contentType = contentType + if isinstance(statusCode, int): + self.statusCode = statusCode + if isinstance(customHeaders, dict): + self.headers = customHeaders + + #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. @@ -816,8 +832,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, request_type) return "", 400 + + # Contains additional functionality with the support of previously existing one. + 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() + 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.getHeaders(request_type)) def _map_intent_to_view_func(self, intent): """Provides appropiate parameters to the intent functions.""" 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