<commit_before>package chlib

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"time"

	"github.com/kfeofantov/chkit-v2/helpers"
)

type HttpApiConfig struct {
	Server  string            `mapconv:"server"`
	Headers map[string]string `mapconv:"headers"`
	Timeout time.Duration     `mapconv:"timeout"`
}

type HttpApiHandler struct {
	cfg HttpApiConfig
}

type HttpApiResult map[string]interface{}

const httpApiBucket = "httpApi"

func init() {
	cfg := HttpApiConfig{Headers: map[string]string{"Authorization": ""}}
	initializers[httpApiBucket] = helpers.StructToMap(cfg)
}

func GetHttpApiCfg() (cfg HttpApiConfig, err error) {
	m, err := readFromBucket(httpApiBucket)
	if err != nil {
		return cfg, fmt.Errorf("load http api config: %s", err)
	}
	err = helpers.FillStruct(&cfg, m)
	if err != nil {
		return cfg, fmt.Errorf("http api config fill: %s", err)
	}
	return cfg, nil
}

func UpdateHttpApiCfg(cfg HttpApiConfig) error {
	return pushToBucket(httpApiBucket, helpers.StructToMap(cfg))
}

func NewHttpApiHandler(cfg HttpApiConfig, uuid string) *HttpApiHandler {
	handler := HttpApiHandler{cfg: cfg}
	handler.cfg.Headers["Channel"] = uuid
	return &handler
}

func (h *HttpApiHandler) makeRequest(url, method string, jsonToSend json.RawMessage) (result HttpApiResult, err error) {
	client := http.Client{Timeout: h.cfg.Timeout}
	request, err := http.NewRequest(method, url, bytes.NewBuffer(jsonToSend))
	for k, v := range h.cfg.Headers {
		request.Header.Add(k, v)
	}
	if err != nil {
		return result, fmt.Errorf("http request create error: %s", err)
	}
	resp, err := client.Do(request)
	if err != nil {
		return result, fmt.Errorf("http request execute error: %s", err)
	}
	defer resp.Body.Close()
	err = json.NewDecoder(resp.Body).Decode(&result)
	return result, err
}

func (h *HttpApiHandler) Login(jsonToSend json.RawMessage) (result HttpApiResult, err error) {
	url := fmt.Sprintf("%s/session/login", h.cfg.Server)
	result, err = h.makeRequest(url, http.MethodPost, jsonToSend)
	return
}<commit_msg>add uuid field to httpApiConfig<commit_after>package chlib

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"time"

	"github.com/kfeofantov/chkit-v2/helpers"
)

type HttpApiConfig struct {
	Server  string            `mapconv:"server"`
	Headers map[string]string `mapconv:"headers"`
	Timeout time.Duration     `mapconv:"timeout"`
	Uuid    string            `mapconv:"-"`
}

type HttpApiHandler struct {
	cfg HttpApiConfig
}

type HttpApiResult map[string]interface{}

const httpApiBucket = "httpApi"

func init() {
	cfg := HttpApiConfig{Headers: map[string]string{"Authorization": ""}}
	initializers[httpApiBucket] = helpers.StructToMap(cfg)
}

func GetHttpApiCfg() (cfg HttpApiConfig, err error) {
	m, err := readFromBucket(httpApiBucket)
	if err != nil {
		return cfg, fmt.Errorf("load http api config: %s", err)
	}
	err = helpers.FillStruct(&cfg, m)
	if err != nil {
		return cfg, fmt.Errorf("http api config fill: %s", err)
	}
	return cfg, nil
}

func UpdateHttpApiCfg(cfg HttpApiConfig) error {
	return pushToBucket(httpApiBucket, helpers.StructToMap(cfg))
}

func NewHttpApiHandler(cfg HttpApiConfig) *HttpApiHandler {
	handler := HttpApiHandler{cfg: cfg}
	handler.cfg.Headers["Channel"] = cfg.Uuid
	return &handler
}

func (h *HttpApiHandler) makeRequest(url, method string, jsonToSend json.RawMessage) (result HttpApiResult, err error) {
	client := http.Client{Timeout: h.cfg.Timeout}
	request, err := http.NewRequest(method, url, bytes.NewBuffer(jsonToSend))
	for k, v := range h.cfg.Headers {
		request.Header.Add(k, v)
	}
	if err != nil {
		return result, fmt.Errorf("http request create error: %s", err)
	}
	resp, err := client.Do(request)
	if err != nil {
		return result, fmt.Errorf("http request execute error: %s", err)
	}
	defer resp.Body.Close()
	err = json.NewDecoder(resp.Body).Decode(&result)
	return result, err
}

func (h *HttpApiHandler) Login(jsonToSend json.RawMessage) (result HttpApiResult, err error) {
	url := fmt.Sprintf("%s/session/login", h.cfg.Server)
	result, err = h.makeRequest(url, http.MethodPost, jsonToSend)
	return
}
{'fraction_non_alphanumeric': 0.13536047081902894, 'fraction_numerical': 0.0004904364884747426, 'mean_word_length': 4.869064748201438, 'pattern_counts': {'":': 2, '<': 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': '571492', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1403, 'n_words': 351}