<commit_before>package provider

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

// UnmarshalHTTPResponse 处理HTTP结果，返回序列化的json
func UnmarshalHTTPResponse(resp *http.Response, url string, err error, result interface{}) error {
	body, err := GetHTTPResponse(resp, url, err)

	if err == nil {
		// log.Println(string(body))
		err = json.Unmarshal(body, &result)
		if err != nil {
			err = fmt.Errorf("请求接口%s解析json结果失败! ERROR: %s", url, err)
		}
	}

	return err

}

// GetHTTPResponse 处理HTTP结果，返回byte
func GetHTTPResponse(resp *http.Response, url string, err error) ([]byte, error) {
	if err != nil {
		log.Printf("请求接口%s失败! ERROR: %s\n", url, err)
		return nil, err
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		log.Printf("请求接口%s失败! ERROR: %s\n", url, err)
	}

	// 300及以上状态码都算异常
	if resp.StatusCode >= 300 {
		err = fmt.Errorf("请求接口 %s 失败! 返回内容: %s , 返回状态码: %d", url, string(body), resp.StatusCode)
	}

	return body, err
}
<commit_msg>improved<commit_after>package provider

import (
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
)

// UnmarshalHTTPResponse 处理HTTP结果，返回序列化的json
func UnmarshalHTTPResponse(resp *http.Response, url string, err error, result interface{}) error {
	body, err := GetHTTPResponse(resp, url, err)

	if err == nil {
		// log.Println(string(body))
		err = json.Unmarshal(body, &result)
		if err != nil {
			err = fmt.Errorf("请求接口%s解析json结果失败! ERROR: %s", url, err)
		}
	}

	return err

}

var MaxReponseSize = int64(1 << 20) // 1M

// GetHTTPResponse 处理HTTP结果，返回byte
func GetHTTPResponse(resp *http.Response, url string, err error) ([]byte, error) {
	if err != nil {
		log.Printf("请求接口%s失败! ERROR: %s\n", url, err)
		return nil, err
	}

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(io.LimitReader(resp.Body, MaxReponseSize))

	if err != nil {
		log.Printf("请求接口%s失败! ERROR: %s\n", url, err)
	}

	// 300及以上状态码都算异常
	if resp.StatusCode >= 300 {
		err = fmt.Errorf("请求接口 %s 失败! 返回内容: %s , 返回状态码: %d", url, string(body), resp.StatusCode)
	}

	return body, err
}

{'fraction_non_alphanumeric': 0.16013546202225448, 'fraction_numerical': 0.008708272859216255, 'mean_word_length': 3.9121140142517814, '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': '4223061', 'n_tokens_mistral': 933, 'n_tokens_neox': 910, 'n_words': 199}