<commit_before>package utils

import (
	"bytes"
	"encoding/json"
	"net/http"
	"net/http/pprof"

	"github.com/gorilla/mux"
)

// JSON response convenience function
func jsonResponse(w http.ResponseWriter, data interface{}, err error) {
	w.Header().Add("Content-Type", "application/json")

	// Error Response - Return early
	if err != nil {
		jErr, _ := json.Marshal(map[string]interface{}{
			"Error": err.Error(),
		})
		w.WriteHeader(500)
		w.Write(jErr)
	}

	// Try to handle data
	jRes, mErr := json.Marshal(data)
	if mErr != nil {
		jErr, _ := json.Marshal(map[string]interface{}{
			"Data Error": err.Error(),
		})
		w.WriteHeader(500)
		w.Write(jErr)
	}
	w.Write(jRes)
}

// Nests map (for adding envelope)
func envelope(d interface{}, envelope string) map[string]interface{} {
	return map[string]interface{}{
		envelope: d,
	}
}<commit_msg>support http status code in jsonResponse function<commit_after>package utils

import (
	"bytes"
	"encoding/json"
	"net/http"
	"net/http/pprof"

	"github.com/gorilla/mux"
)

// JSON response convenience function
func jsonResponse(w http.ResponseWriter, status int, data interface{}, err error) {
	w.Header().Add("Content-Type", "application/json")

	// Error Response - Return early
	if err != nil {
		jErr, _ := json.Marshal(map[string]interface{}{
			"Error": err.Error(),
		})
		w.WriteHeader(500)
		w.Write(jErr)
	}

	// Try to handle data
	jRes, mErr := json.Marshal(data)
	if mErr != nil {
		jErr, _ := json.Marshal(map[string]interface{}{
			"Data Error": err.Error(),
		})
		w.WriteHeader(500)
		w.Write(jErr)
	}
	w.WriteHeader(status)
	w.Write(jRes)
}

// Nests map (for adding envelope)
func envelope(d interface{}, envelope string) map[string]interface{} {
	return map[string]interface{}{
		envelope: d,
	}
}
{'fraction_non_alphanumeric': 0.16203966005665724, 'fraction_numerical': 0.006798866855524079, 'mean_word_length': 4.074712643678161, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, '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': '5536454', 'n_tokens_mistral': 670, 'n_tokens_neox': 630, 'n_words': 143}