<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Snowflake.Ajax
{
    public class JSResponse : IJSResponse
    {
        public IJSRequest Request { get; private set; }
        public dynamic Payload { get; private set; }
        public bool Success { get; set; }
        public JSResponse(IJSRequest request, dynamic payload, bool success = true)
        {
            this.Request = request;
            this.Payload = payload;
            this.Success = success;
        }

        public string GetJson()
        {
            return JSResponse.ProcessJSONP(this.Payload, this.Success, this.Request);
        }
        private static string ProcessJSONP(dynamic output, bool success, IJSRequest request)
        {
            if (request.MethodParameters.ContainsKey("jsoncallback"))
            {
                return request.MethodParameters["jsoncallback"] + "(" + JsonConvert.SerializeObject(new Dictionary<string, object>(){
                    {"payload", output},
                    {"success", success}
                }) + ");";
            }
            else
            {
                return JsonConvert.SerializeObject(new Dictionary<string, object>(){
                    {"payload", output},
                    {"success", success}
                });
            }
        }
    }
}
<commit_msg>JSAPI: Add request and 'methodresponse' type in JSON body to account for other response types using the WebSocket APIs<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Snowflake.Ajax
{
    public class JSResponse : IJSResponse
    {
        public IJSRequest Request { get; private set; }
        public dynamic Payload { get; private set; }
        public bool Success { get; set; }
        public JSResponse(IJSRequest request, dynamic payload, bool success = true)
        {
            this.Request = request;
            this.Payload = payload;
            this.Success = success;
        }

        public string GetJson()
        {
            return JSResponse.ProcessJSONP(this.Payload, this.Success, this.Request);
        }
        private static string ProcessJSONP(dynamic output, bool success, IJSRequest request)
        {
            if (request.MethodParameters.ContainsKey("jsoncallback"))
            {
                return request.MethodParameters["jsoncallback"] + "(" + JsonConvert.SerializeObject(new Dictionary<string, object>(){
                    {"request", request},
                    {"payload", output},
                    {"success", success},
                    {"type", "methodresponse"}
                }) + ");";
            }
            else
            {
                return JsonConvert.SerializeObject(new Dictionary<string, object>(){
                    {"request", request},
                    {"payload", output},
                    {"success", success},
                    {"type", "methodresponse"}
                });
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.0989566866898514, 'fraction_numerical': 0.0, 'mean_word_length': 1.8024800708591675, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4525487', 'n_tokens_mistral': 756, 'n_tokens_neox': 740, 'n_words': 196}