<commit_before>/*
Copyright 2016 The Fission Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kubewatcher

import (
	"io"
	"log"
	"net/http"
	"strings"
)

type (
	Poster struct {
		routerUrl      string
		requestChannel chan *postRequest
	}
	postRequest struct {
		eventType   string
		relativeUrl string
		body        io.Reader
	}
)

func MakePoster(routerUrl string) *Poster {
	p := &Poster{
		routerUrl:      strings.TrimSuffix(routerUrl, "/"),
		requestChannel: make(chan *postRequest, 32), // buffered channel
	}
	go p.svc()
	return p
}

func (p *Poster) svc() {
	for {
		r := <-p.requestChannel

		url := p.routerUrl + r.relativeUrl
		req, err := http.NewRequest("POST", url, r.body)
		if err != nil {
			log.Printf("Failed to create request to %v", r.relativeUrl)
		}
		req.Header.Add("X-Kubernetes-Event-Type", r.eventType)
		req.Header.Add("X-Fission-Request-Async", "true")

		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			log.Printf("request failed: %v", r)
			// TODO retries, persistence, etc.
		}

		resp.Body.Close()
		if resp.StatusCode != 200 {
			log.Printf("request failed: %v", resp.StatusCode)
			// TODO retries etc.
		}
	}
}

func (p *Poster) Post(eventType, relativeUrl string, body io.Reader) {
	p.requestChannel <- &postRequest{
		eventType:   eventType,
		relativeUrl: relativeUrl,
		body:        body,
	}
}
<commit_msg>Kube requests: add content-type<commit_after>/*
Copyright 2016 The Fission Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kubewatcher

import (
	"io"
	"log"
	"net/http"
	"strings"
)

type (
	Poster struct {
		routerUrl      string
		requestChannel chan *postRequest
	}
	postRequest struct {
		eventType   string
		relativeUrl string
		body        io.Reader
	}
)

func MakePoster(routerUrl string) *Poster {
	p := &Poster{
		routerUrl:      strings.TrimSuffix(routerUrl, "/"),
		requestChannel: make(chan *postRequest, 32), // buffered channel
	}
	go p.svc()
	return p
}

func (p *Poster) svc() {
	for {
		r := <-p.requestChannel

		url := p.routerUrl + r.relativeUrl
		log.Printf("Making request to %v", url)
		req, err := http.NewRequest("POST", url, r.body)
		if err != nil {
			log.Printf("Failed to create request to %v", r.relativeUrl)
		}
		req.Header.Add("Content-Type", "application/json")
		req.Header.Add("X-Kubernetes-Event-Type", r.eventType)

		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			log.Printf("request failed: %v", r)
			// TODO retries, persistence, etc.
		}

		resp.Body.Close()
		if resp.StatusCode != 200 {
			log.Printf("request failed: %v", resp.StatusCode)
			// TODO retries etc.
		}
	}
}

func (p *Poster) Post(eventType, relativeUrl string, body io.Reader) {
	p.requestChannel <- &postRequest{
		eventType:   eventType,
		relativeUrl: relativeUrl,
		body:        body,
	}
}

{'fraction_non_alphanumeric': 0.11484519714210109, 'fraction_numerical': 0.006880127017729558, 'mean_word_length': 3.5107398568019095, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3488943', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1227, 'n_words': 398}