request.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package request
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "log"
  9. "net/http"
  10. "strings"
  11. "syno-common/server/utils"
  12. "time"
  13. )
  14. type Option struct {
  15. Url string
  16. Resp any
  17. Accept string
  18. Headers map[string]string
  19. ApiKey string
  20. ApiSecret string
  21. Sign func(option Option) map[string]string
  22. }
  23. // Request 封装请求库
  24. type Request struct {
  25. BaseURL string
  26. Timeout time.Duration
  27. }
  28. func defaultSign(option Option) map[string]string {
  29. if option.Headers == nil {
  30. option.Headers = map[string]string{}
  31. }
  32. apiKey := option.ApiKey
  33. apiSecret := option.ApiSecret
  34. timestamp := time.Now().Unix()
  35. nonce := utils.RandString(16)
  36. fmt.Printf("key: %v, secret: %v,time: %v,nonce: %v\n", apiKey, apiSecret, timestamp, nonce)
  37. cipherText := fmt.Sprintf("%v%v%v", apiKey, timestamp, nonce)
  38. sign, err := utils.AesEncrypt(cipherText, apiSecret)
  39. if err != nil {
  40. log.Printf("sign fail: %v", err)
  41. } else {
  42. option.Headers["x-api-key"] = apiKey
  43. option.Headers["x-api-timestamp"] = fmt.Sprintf("%v", timestamp)
  44. option.Headers["x-api-nonce"] = nonce
  45. option.Headers["x-api-sign"] = sign
  46. log.Printf("sign ok: %v", sign)
  47. }
  48. //log.Printf("headers: %v", option.Headers)
  49. return option.Headers
  50. }
  51. func NewRequest(baseUrl string, timeout time.Duration) Request {
  52. return Request{
  53. BaseURL: baseUrl,
  54. Timeout: timeout,
  55. }
  56. }
  57. func (r *Request) DoPostJson(requestData any, option Option) (any, error) {
  58. data, err := json.Marshal(requestData)
  59. if err != nil {
  60. return nil, err
  61. }
  62. url := fmt.Sprintf("%s%s", r.BaseURL, option.Url)
  63. if !strings.HasPrefix(option.Url, "/") && !strings.HasSuffix(r.BaseURL, "/") {
  64. url = fmt.Sprintf("%s/%s", r.BaseURL, option.Url)
  65. }
  66. req, err := http.NewRequest("POST", url, bytes.NewReader(data))
  67. if err != nil {
  68. return nil, err
  69. }
  70. userHeaders := option.Headers
  71. if option.ApiKey != "" {
  72. if option.Sign == nil {
  73. userHeaders = defaultSign(option)
  74. } else {
  75. userHeaders = option.Sign(option)
  76. }
  77. }
  78. log.Printf("headers: %v", userHeaders)
  79. req.Header.Set("Content-Type", "application/json")
  80. if userHeaders == nil {
  81. userHeaders = map[string]string{}
  82. }
  83. for header := range userHeaders {
  84. req.Header.Set(header, userHeaders[header])
  85. }
  86. client := &http.Client{
  87. Timeout: r.Timeout,
  88. }
  89. rsp, err := client.Do(req)
  90. if err != nil {
  91. return nil, err
  92. }
  93. if rsp.StatusCode < 200 || rsp.StatusCode > 299 {
  94. err = errors.New(rsp.Status)
  95. log.Printf("status: %v", rsp.StatusCode)
  96. return nil, err
  97. }
  98. body, err := io.ReadAll(rsp.Body)
  99. if err != nil {
  100. log.Printf("read body fail: %v", err)
  101. return nil, errors.New("body parse fail")
  102. }
  103. if option.Resp != nil {
  104. err := json.Unmarshal(body, option.Resp)
  105. if err != nil {
  106. log.Printf("parse body fail: %v", err)
  107. return nil, err
  108. }
  109. return option.Resp, nil
  110. }
  111. if option.Accept == "" || option.Accept == "application/json" {
  112. var respData = map[string]interface{}{}
  113. err := json.Unmarshal(body, &respData)
  114. //log.Printf("resp data: %v", respData)
  115. if err != nil {
  116. log.Printf("parse body fail: %v", err)
  117. return nil, err
  118. }
  119. return respData, nil
  120. }
  121. return body, nil
  122. }
  123. func Test(apiKey string, secret string) {
  124. request := NewRequest("http://10.10.0.1:20003", time.Second*10)
  125. data := map[string]interface{}{}
  126. option := Option{
  127. Url: "/client/login",
  128. Resp: nil,
  129. Accept: "",
  130. ApiKey: apiKey,
  131. ApiSecret: secret,
  132. }
  133. resp, err := request.DoPostJson(data, option)
  134. if err != nil {
  135. log.Fatalln(err)
  136. }
  137. log.Printf("resp: %v", resp)
  138. }