package infra

import (
	"bytes"
	"io"
	"net/http"

	"git.apinb.com/bsm-sdk/core/errcode"
)

var FTS fts

type fts struct {
	Endpoint string
}

func (o *fts) SaveTo(buf *bytes.Buffer, haeder map[string]string) (string, error) {
	// 创建一个 POST 请求
	req, err := http.NewRequest("POST", o.Endpoint, buf)
	if err != nil {
		return "", errcode.NewError(110, "Request to the URL")
	}

	// 设置请求头为二进制流
	req.Header.Set("Content-Type", "application/octet-stream")
	for key, val := range haeder {
		req.Header.Set(key, val)
	}

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return "", errcode.NewError(111, "Client Do")
	}
	defer resp.Body.Close()

	// 读取响应体
	respBody, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", errcode.NewError(112, "Read response body")
	}
	return string(respBody), nil
}