Exemple #1
0
def respond_with_json(request, code, json_object, send_cors=False,
                      response_code_message=None, pretty_print=False):
    if not pretty_print:
        json_bytes = encode_pretty_printed_json(json_object)
    else:
        json_bytes = encode_canonical_json(json_object)

    return respond_with_json_bytes(request, code, json_bytes, send_cors,
                                   response_code_message=response_code_message)
Exemple #2
0
def respond_with_json(request, code, json_object, send_cors=False,
                      response_code_message=None, pretty_print=False,
                      version_string=""):
    if pretty_print:
        json_bytes = encode_pretty_printed_json(json_object) + "\n"
    else:
        json_bytes = encode_canonical_json(json_object)

    return respond_with_json_bytes(
        request, code, json_bytes,
        send_cors=send_cors,
        response_code_message=response_code_message,
        version_string=version_string
    )
Exemple #3
0
def respond_with_json(request, code, json_object, send_cors=False,
                      response_code_message=None, pretty_print=False,
                      version_string="", canonical_json=True):
    if pretty_print:
        json_bytes = encode_pretty_printed_json(json_object) + "\n"
    else:
        if canonical_json:
            json_bytes = encode_canonical_json(json_object)
        else:
            json_bytes = encode_json(
                json_object, using_frozen_dicts=synapse.events.USE_FROZEN_DICTS
            )

    return respond_with_json_bytes(
        request, code, json_bytes,
        send_cors=send_cors,
        response_code_message=response_code_message,
        version_string=version_string
    )
Exemple #4
0
    def _send_response(self, request, code, response_json_object,
                       response_code_message=None):
        # could alternatively use request.notifyFinish() and flip a flag when
        # the Deferred fires, but since the flag is RIGHT THERE it seems like
        # a waste.
        if request._disconnected:
            logger.warn(
                "Not sending response to request %s, already disconnected.",
                request)
            return

        if not self._request_user_agent_is_curl(request):
            json_bytes = encode_canonical_json(response_json_object)
        else:
            json_bytes = encode_pretty_printed_json(response_json_object)

        # TODO: Only enable CORS for the requests that need it.
        respond_with_json_bytes(request, code, json_bytes, send_cors=True,
                                response_code_message=response_code_message)
Exemple #5
0
def respond_with_json(request,
                      code,
                      json_object,
                      send_cors=False,
                      response_code_message=None,
                      pretty_print=False,
                      version_string="",
                      canonical_json=True):
    if pretty_print:
        json_bytes = encode_pretty_printed_json(json_object) + "\n"
    else:
        if canonical_json:
            json_bytes = encode_canonical_json(json_object)
        else:
            json_bytes = encode_json(
                json_object,
                using_frozen_dicts=synapse.events.USE_FROZEN_DICTS)

    return respond_with_json_bytes(request,
                                   code,
                                   json_bytes,
                                   send_cors=send_cors,
                                   response_code_message=response_code_message,
                                   version_string=version_string)