github.com/HXSecurity/DongTai-agent-go@v0.4.2/api/base_config.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/HXSecurity/DongTai-agent-go/global"
     6  	"github.com/HXSecurity/DongTai-agent-go/utils"
     7  	"github.com/parnurzeal/gorequest"
     8  	"time"
     9  )
    10  
    11  // NewRequest
    12  // 空白的请求工具 预留做后续的统一参数处理
    13  func NewRequest() *gorequest.SuperAgent {
    14  	request := gorequest.New()
    15  	request.Timeout(30 * time.Second)
    16  	return request
    17  }
    18  
    19  // POST
    20  // 统一的POST前置处理
    21  // url 请求api
    22  // token 身份标识
    23  func POST(url string, body interface{}) *gorequest.SuperAgent {
    24  	s, _ := json.Marshal(body)
    25  	jsonStr := utils.GzipStr(string(s))
    26  	request := NewRequest()
    27  	request.Post(global.Config.DongtaiGoOpenapi+url).
    28  		Set("Content-Type", "application/json").
    29  		Set("Authorization", "Token "+global.Config.DongtaiGoToken).
    30  		Send(jsonStr)
    31  	return request
    32  }
    33  
    34  // GET
    35  // 统一的GET前置处理
    36  // url 请求api
    37  // token 身份标识
    38  func GET(url string, body interface{}) *gorequest.SuperAgent {
    39  	request := NewRequest()
    40  	request.Get(global.Config.DongtaiGoOpenapi+url).Set("Authorization", "Token "+global.Config.DongtaiGoToken).Query(body)
    41  	return request
    42  }