<commit_before>package api

import (
	"encoding/json"
	"io"
	"io/ioutil"
	"net/http"

	"../job"

	"github.com/222Labs/common/go/logging"
	"github.com/gorilla/mux"
)

var (
	log = logging.GetLogger("kala")
)

type ListJobsResponse struct {
	Jobs map[string]*job.Job `json:"jobs"`
}

// HandleListJobs responds with an array of all Jobs within the server,
// active or disabled.
func HandleListJobs(w http.ResponseWriter, r *http.Request) {
	resp := &ListJobsResponse{
		Jobs: job.AllJobs,
	}

	w.Header().Set("Content-Type", "application/json;charset=UTF-8")
	w.WriteHeader(http.StatusCreated)
	if err := json.NewEncoder(w).Encode(resp); err != nil {
		log.Error("Error occured when marshalling response: %s", err)
		return
	}

}

type AddJobResponse struct {
	Id string `json:"id"`
}

// HandleAddJob takes a job object and unmarshals it to a Job type,
// and then throws the job in the schedulers.
func HandleAddJob(w http.ResponseWriter, r *http.Request) {
	newJob := &job.Job{}

	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		log.Error("Error occured when reading r.Body: %s", err)
		return
	}
	defer r.Body.Close()

	if err := json.Unmarshal(body, newJob); err != nil {
		errStr := "Error occured when unmarshalling data"
		log.Error(errStr+": %s", err)
		http.Error(w, errStr, 400)
		return
	}

	// TODO
	// 2. Verify that "protected" fields were not touched.

	err = newJob.Init()
	if err != nil {
		errStr := "Error occured when initializing the job"
		log.Error(errStr+": %s", err)
		http.Error(w, errStr, 400)
		return
	}
	log.Info("New Job: %#v", newJob)
	job.AllJobs[newJob.Id] = newJob

	resp := &AddJobResponse{
		Id: newJob.Id,
	}

	w.Header().Set("Content-Type", "application/json;charset=UTF-8")<commit_msg>Extracted the unmarshalling of a new job out into a seperate function in api/api.go<commit_after>package api

import (
	"encoding/json"
	"io"
	"io/ioutil"
	"net/http"

	"../job"

	"github.com/222Labs/common/go/logging"
	"github.com/gorilla/mux"
)

var (
	log = logging.GetLogger("kala")
)

type ListJobsResponse struct {
	Jobs map[string]*job.Job `json:"jobs"`
}

// HandleListJobs responds with an array of all Jobs within the server,
// active or disabled.
func HandleListJobs(w http.ResponseWriter, r *http.Request) {
	resp := &ListJobsResponse{
		Jobs: job.AllJobs,
	}

	w.Header().Set("Content-Type", "application/json;charset=UTF-8")
	w.WriteHeader(http.StatusCreated)
	if err := json.NewEncoder(w).Encode(resp); err != nil {
		log.Error("Error occured when marshalling response: %s", err)
		return
	}

}

type AddJobResponse struct {
	Id string `json:"id"`
}

func unmarshalNewJob(r *http.Request) (*job.Job, error) {
	newJob := &job.Job{}

	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		log.Error("Error occured when reading r.Body: %s", err)
		return nil, err
	}
	defer r.Body.Close()

	if err := json.Unmarshal(body, newJob); err != nil {
		log.Error("Error occured when unmarshalling data: %s", err)
		return nil, err
	}

	return newJob, nil
}

// HandleAddJob takes a job object and unmarshals it to a Job type,
// and then throws the job in the schedulers.
func HandleAddJob(w http.ResponseWriter, r *http.Request) {
	newJob, err := unmarshalNewJob(r)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	// TODO
	// 2. Verify that "protected" fields were not touched.

	err = newJob.Init()
	if err != nil {
		errStr := "Error occured when initializing the job"
		log.Error(errStr+": %s", err)
		http.Error(w, errStr, 400)
		return
	}
	job.AllJobs[newJob.Id] = newJob

	resp := &AddJobResponse{
		Id: newJob.Id,
	}

	w.Header().Set("Content-Type", "application/json;charset=UTF-8")
{'fraction_non_alphanumeric': 0.149851230727617, 'fraction_numerical': 0.010278604273735462, 'mean_word_length': 3.9241011984021306, 'pattern_counts': {'":': 3, '<': 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': '6046525', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1364, 'n_words': 359}