<commit_before>package httpapi

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/srid/log"
	"io/ioutil"
	"net/http"
	"reflect"
)

// APIHandler handles a single API endpoint
type APIHandler struct {
	// RequestStruct is a struct to store the fields of request
	// paramemters, passed as JSON from the client.
	RequestStruct interface{}
}

func (h APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	log.Infof("httpapi %s -- %s", h.RequestStruct, r)
	request := reflect.New(reflect.TypeOf(h.RequestStruct)).Interface().(RequestParams)

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		// FIXME: best way to report errors?
		log.Error(err)
		return
	}
	if err := json.Unmarshal(body, request); err != nil {
		log.Error(err)
		return
	}
	log.Info(request)
	data, err := request.HandleRequest()
	if err != nil {
		log.Error(err)
		fmt.Fprintf(w, "FAIL %s", err)
		return
	}
	fmt.Fprintf(w, data)
}

type RequestParams interface {
	// HandleRequest is called when a request comes in. It must return<commit_msg>change request log format<commit_after>package httpapi

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/srid/log"
	"io/ioutil"
	"net/http"
	"reflect"
)

// APIHandler handles a single API endpoint
type APIHandler struct {
	// RequestStruct is a struct to store the fields of request
	// paramemters, passed as JSON from the client.
	RequestStruct interface{}
}

func (h APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	log.Infof("httpapi -- %+v", r)
	request := reflect.New(reflect.TypeOf(h.RequestStruct)).Interface().(RequestParams)

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		// FIXME: best way to report errors?
		log.Error(err)
		return
	}
	if err := json.Unmarshal(body, request); err != nil {
		log.Error(err)
		return
	}

	data, err := request.HandleRequest()
	if err != nil {
		log.Error(err)
		fmt.Fprintf(w, "FAIL %s", err)
		return
	}
	fmt.Fprintf(w, data)
}

type RequestParams interface {
	// HandleRequest is called when a request comes in. It must return
{'fraction_non_alphanumeric': 0.1330387825233186, 'fraction_numerical': 0.0, 'mean_word_length': 3.840855106888361, 'pattern_counts': {'":': 0, '<': 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': '6829749', 'n_tokens_mistral': 735, 'n_tokens_neox': 713, 'n_words': 205}