Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of functionality of custom response headers, request type, content type and status code. #251

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions flask_ask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .core import (
Ask,
AskResponse,
request,
session,
version,
Expand Down
37 changes: 35 additions & 2 deletions flask_ask/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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."""
Expand Down
6 changes: 3 additions & 3 deletions samples/helloworld/helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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')
Expand All @@ -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
Expand Down