<commit_before>package api

import (
	"bytes"
	"fmt"
	"net/http"
	"net/url"
	"text/template"

	"github.com/apihub/apihub/account"
	. "github.com/apihub/apihub/log"
	"github.com/apihub/apihub/requests"
)

type Event interface {
	Data() []byte
	Name() string
}

func (api *Api) EventNotifier(ev Event) {
	api.Events <- ev
}

func (api *Api) ListenEvents() {
	Logger.Info("Started listening to events in background.")

	go func() {
		for event := range api.Events {
			// TODO: Need to load team specific!
			allHookw, err := account.FindHooksByEvent(event.Name())

			if len(allHookw) > 0 && err == nil {
				Logger.Debug(fmt.Sprintf("Start sending hooks to the following list: %+v.", allHookw))
				for _, hook := range allHookw {
					data, err := parseData(event, hook)
					if err != nil {
						Logger.Warn("Could not parse Event data: %+v. Default format will be delivered.", err)
						data = event.Data()
					}

					go func(config account.HookConfig, data []byte) {
						sendWebHook(config, data)
					}(hook.Config, data)
				}
			}
		}
	}()
}

func parseData(event Event, hook account.Hook) ([]byte, error) {
	var err error
	tmpl := template.New(event.Name())
	data := bytes.NewBufferString("")

	if hook.Text != "" {
		tmpl, err = tmpl.Parse(hook.Text)
	}

	err = tmpl.Execute(data, event)
	if err != nil {
		return nil, err
	}
<commit_msg>Extract method.<commit_after>package api

import (
	"bytes"
	"fmt"
	"net/http"
	"net/url"
	"text/template"

	"github.com/apihub/apihub/account"
	. "github.com/apihub/apihub/log"
	"github.com/apihub/apihub/requests"
)

type Event interface {
	Data() []byte
	Name() string
}

func (api *Api) EventNotifier(ev Event) {
	api.Events <- ev
}

func (api *Api) ListenEvents() {
	Logger.Info("Started listening to events in background.")

	go func() {
		for event := range api.Events {
			// Send Hook events
			go func(event Event) {
				sendHooks(event)
			}(event)

		}
	}()
}

func sendHooks(event Event) {
	// TODO: Need to load team specific!
	allHookw, err := account.FindHooksByEvent(event.Name())

	if len(allHookw) > 0 && err == nil {
		Logger.Debug(fmt.Sprintf("Start sending hooks to the following list: %+v.", allHookw))
		for _, hook := range allHookw {
			data, err := parseData(event, hook)
			if err != nil {
				Logger.Warn("Could not parse Event data: %+v. Default format will be delivered.", err)
				data = event.Data()
			}

			go func(config account.HookConfig, data []byte) {
				sendWebHook(config, data)
			}(hook.Config, data)
		}
	}
}

func parseData(event Event, hook account.Hook) ([]byte, error) {
	var err error
	tmpl := template.New(event.Name())
	data := bytes.NewBufferString("")

	if hook.Text != "" {
		tmpl, err = tmpl.Parse(hook.Text)
	}

	err = tmpl.Execute(data, event)
	if err != nil {
		return nil, err
	}

{'fraction_non_alphanumeric': 0.15365329512893983, 'fraction_numerical': 0.0007163323782234957, 'mean_word_length': 3.3302325581395347, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5234495', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1009, 'n_words': 264}