package Request

import (
	"github.com/imroc/req"
	log "github.com/sirupsen/logrus"
	"time"
)

type Request struct {
}

func init() {
	req.Debug =false
}

//post request
func Post(url string, jsonBody string) (string, error) {
	//log.Infof("request: %s, post:%s \n",url,jsonBody)
	//req.Debug = true
	req.SetFlags(req.LstdFlags | req.Lcost)
	req.SetTimeout(time.Duration(60) * time.Second)

	authHeader := req.Header{
		"Content-Type": "application/json;charset=utf-8",
		"Accept":       "application/json",
		// "Authorization": auth,
	}

	r, err := req.Post(url, authHeader, jsonBody)
	// fmt.Printf("resp: %+v", r)
	if err != nil {
		//fmt.Println("query err:", err, " url:", url, " json:", jsonBody)
		log.Errorln("query err:", err, " url:", url, " json:", jsonBody)
		return "", err
	} else {
		return r.String(), err
	}
}

func Get(url string) (string, error) {
	//log.Infoln("request : ",url)
	//req.Debug = false
	req.SetFlags(req.LstdFlags | req.Lcost)
	req.SetTimeout(time.Duration(60) * time.Second)

	authHeader := req.Header{
		"Content-Type": "application/json;charset=utf-8",
		"Accept":       "application/json",
		// "Authorization": auth,
	}

	r, err := req.Get(url, authHeader)
	// fmt.Printf("resp: %+v", r)
	if err != nil {
		//fmt.Println("query err:", err, " url:", url)
		log.Errorln("query err:", err, " url:", url)
		return "", err
	} else {
		return r.String(), err
	}
}