Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def freeze_params(params):
"""
If params is a dictionary or Params we convert it into FrozenParams.
Otherwise we raise a TypeError.
Args:
params (dict, Params): The input params to convert
Returns:
FrozenParams: The converted params object
"""
params = params or FrozenParams()
if isinstance(params, dict):
params = FrozenParams(**params)
elif params.__class__ == Params:
params = FrozenParams(**DialogueResponder.to_json(params))
elif not isinstance(params, FrozenParams):
raise TypeError(
"Invalid type for params argument. "
"Should be dict or {}".format(FrozenParams.__name__)
)
return params
def to_json(instance):
"""Convert the responder into a JSON representation.
Args:
instance (DialogueResponder): The responder object.
Returns:
(dict): The JSON representation.
"""
serialized_obj = {}
for attribute, value in vars(instance).items():
if isinstance(value, (Params, Request, FrozenParams)):
serialized_obj[attribute] = DialogueResponder.to_json(value)
elif isinstance(value, immutables.Map):
serialized_obj[attribute] = dict(value)
else:
serialized_obj[attribute] = value
return serialized_obj
def _post_dm(self, request, dm_response):
# Append this item to the history, but don't recursively store history
prev_request = DialogueResponder.to_json(dm_response)
prev_request.pop("history")
# limit length of history
new_history = (prev_request,) + request.history
dm_response.history = new_history[: self.MAX_HISTORY_LEN]
# validate outgoing params
dm_response.params.validate_param("allowed_intents")
dm_response.params.validate_param("target_dialogue_state")
return dm_response
"""The main endpoint for the MindMeld API"""
request_json = request.get_json()
if request_json is None:
msg = "Invalid Content-Type: Only 'application/json' is supported."
raise BadMindMeldRequestError(msg, status_code=415)
safe_request = {}
for key in ["text", "params", "context", "frame", "history", "verbose"]:
if key in request_json:
safe_request[key] = request_json[key]
response = self._app_manager.parse(**safe_request)
# add request id to response
# use the passed in id if any
request_id = request_json.get("request_id", str(uuid.uuid4()))
response.request_id = request_id
return jsonify(DialogueResponder.to_json(response))