<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace WebServer
{
    public abstract class JSONServlet : Servlet
    {
        public delegate void WriteDelegate(params Object[] body);
        public delegate String BodyDelegate(params Object[] body);

        private StreamWriter _streamWriter;

        protected WriteDelegate Write { get; private set; }

        protected override bool OnPreService()
        {
            Response.ContentType = "application/json";

            _streamWriter = new StreamWriter(Response.OutputStream);

            Write = x => {
                var str = String.Join(",", x);
                _streamWriter.WriteLine(str);
            };

            return true;
        }

        protected override void OnPostService()
        {
            _streamWriter.Flush();
        }

        protected String Error(String message)
        {
            return Object(Pair("success", false), Pair("error", Str(message)));
        }

        protected String Success(params Object[] body)
        {
            return Object(new Object[] { Pair("success", true) }.Concat(body).ToArray() );
        }

        protected String Format(String format, params object[] args)
        {
            return String.Format(format, args);
        }

        protected String Pair(String key, bool value)
        {
            return Format("\"{0}\":{1}", key, value.ToString().ToLower());
        }

        protected String Pair(String key, Object value)
        {
            return Format("\"{0}\":{1}", key, value);
        }

        protected String Str(object str)
        {
            return Format("\"{0}\"", str.ToString().Replace("\\", "\\\\").Replace("\"", "\\\""));
        }

        protected BodyDelegate Object<commit_msg>Added shouldRetry argument to Error helper method<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace WebServer
{
    public abstract class JSONServlet : Servlet
    {
        public delegate void WriteDelegate(params Object[] body);
        public delegate String BodyDelegate(params Object[] body);

        private StreamWriter _streamWriter;

        protected WriteDelegate Write { get; private set; }

        protected override bool OnPreService()
        {
            Response.ContentType = "application/json";

            _streamWriter = new StreamWriter(Response.OutputStream);

            Write = x => {
                var str = String.Join(",", x);
                _streamWriter.WriteLine(str);
            };

            return true;
        }

        protected override void OnPostService()
        {
            _streamWriter.Flush();
        }

        protected String Error(String message, bool shouldRetry = false)
        {
            return Object(Pair("success", false), Pair("error", Str(message)), Pair("retry", shouldRetry));
        }

        protected String Success(params Object[] body)
        {
            return Object(new Object[] { Pair("success", true) }.Concat(body).ToArray() );
        }

        protected String Format(String format, params object[] args)
        {
            return String.Format(format, args);
        }

        protected String Pair(String key, bool value)
        {
            return Format("\"{0}\":{1}", key, value.ToString().ToLower());
        }

        protected String Pair(String key, Object value)
        {
            return Format("\"{0}\":{1}", key, value);
        }

        protected String Str(object str)
        {
            return Format("\"{0}\"", str.ToString().Replace("\\", "\\\\").Replace("\"", "\\\""));
        }

        protected BodyDelegate Object
{'fraction_non_alphanumeric': 0.12139917695473251, 'fraction_numerical': 0.00257201646090535, 'mean_word_length': 2.074308300395257, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, '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': '6459715', 'n_tokens_mistral': 984, 'n_tokens_neox': 944, 'n_words': 262}