<commit_before>package curl

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

type Requests struct {
	Method string
	UrlStr string
	Header map[string]string
	Raw    string
}

//RollingCurl http请求url
func RollingCurl(r Requests) (string, error) {
	client := &http.Client{}

	req, err := http.NewRequest(
		r.Method,
		r.UrlStr,
		strings.NewReader(r.Raw),
	)

	if err != nil {
		return "", err
	}

	for hkey, hval := range r.Header {
		req.Header.Set(hkey, hval)
	}

	resp, err := client.Do(req)
	if err != nil {
		return "", err
	}

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	return string(body), nil
}

//example
/*
func main() {
	res, err := RollingCurl(
		Requests{
			Method: "POST",
			UrlStr: "http://localhost/study/curl/servera.php",
			Header: map[string]string{
				"Content-Type": "application/json;charset=UTF-8;",
			},
			Raw: `{"name":"KII","age":24}`,
		},
	)
	if err != nil {
		fmt.Println(err)
	}
	var result = make(map[string]interface{})
	json.Unmarshal([]byte(res), &result)<commit_msg>update curl 1.add timeout 2.add failed times<commit_after>package curl

import (
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

type Requests struct {
	Method string
	UrlStr string
	Header map[string]string
	Raw    string
}

//RollingCurl http请求url
func RollingCurl(r Requests) (string, error) {
	i := 0
RELOAD:
	client := &http.Client{
		Timeout: 15 * time.Second,
	}

	req, err := http.NewRequest(
		r.Method,
		r.UrlStr,
		strings.NewReader(r.Raw),
	)

	if err != nil {
		return "", err
	}

	for hkey, hval := range r.Header {
		req.Header.Set(hkey, hval)
	}

	resp, err := client.Do(req)
	if err != nil {
		i++
		if i < 3 {
			goto RELOAD
		}
		return "", err
	}

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	return string(body), nil
}

//example
/*
func main() {
	res, err := RollingCurl(
		Requests{
			Method: "POST",
			UrlStr: "http://localhost/study/curl/servera.php",
			Header: map[string]string{
				"Content-Type": "application/json;charset=UTF-8;",
			},
			Raw: `{"name":"KII","age":24}`,
		},
	)
	if err != nil {
		fmt.Println(err)
	}
	var result = make(map[string]interface{})
	json.Unmarshal([]byte(res), &result)
{'fraction_non_alphanumeric': 0.17079646017699116, 'fraction_numerical': 0.005309734513274336, 'mean_word_length': 3.3397312859884836, 'pattern_counts': {'":': 6, '<': 4, '<?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': '3041077', 'n_tokens_mistral': 975, 'n_tokens_neox': 890, 'n_words': 200}