http_client.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package gopay
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "encoding/xml"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "net/http"
  11. "strings"
  12. "sync"
  13. )
  14. const (
  15. POST = "POST"
  16. GET = "GET"
  17. TypeJSON = "json"
  18. TypeXML = "xml"
  19. TypeUrlencoded = "urlencoded"
  20. TypeForm = "form"
  21. TypeFormData = "form-data"
  22. )
  23. var Types = map[string]string{
  24. TypeJSON: "application/json",
  25. TypeXML: "application/xml",
  26. TypeForm: "application/x-www-form-urlencoded",
  27. TypeFormData: "application/x-www-form-urlencoded",
  28. TypeUrlencoded: "application/x-www-form-urlencoded",
  29. }
  30. type Client struct {
  31. HttpClient *http.Client
  32. Transport *http.Transport
  33. Header http.Header
  34. Url string
  35. Method string
  36. RequestType string
  37. FormString string
  38. ContentType string
  39. UnmarshalType string
  40. Types map[string]string
  41. JsonByte []byte
  42. Errors []error
  43. mu sync.RWMutex
  44. }
  45. // NewHttpClient , default tls.Config{InsecureSkipVerify: true}
  46. func NewHttpClient() (client *Client) {
  47. client = &Client{
  48. HttpClient: &http.Client{},
  49. Transport: &http.Transport{},
  50. Header: make(http.Header),
  51. RequestType: TypeUrlencoded,
  52. UnmarshalType: TypeJSON,
  53. Errors: make([]error, 0),
  54. }
  55. client.Transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
  56. client.Transport.DisableKeepAlives = true
  57. return client
  58. }
  59. func (c *Client) SetTLSConfig(tlsCfg *tls.Config) (client *Client) {
  60. c.mu.Lock()
  61. c.Transport.TLSClientConfig = tlsCfg
  62. c.mu.Unlock()
  63. return c
  64. }
  65. func (c *Client) Post(url string) (client *Client) {
  66. c.mu.Lock()
  67. c.Method = POST
  68. c.Url = url
  69. c.mu.Unlock()
  70. return c
  71. }
  72. func (c *Client) Type(typeStr string) (client *Client) {
  73. if _, ok := Types[typeStr]; ok {
  74. c.mu.Lock()
  75. c.RequestType = typeStr
  76. c.mu.Unlock()
  77. } else {
  78. c.Errors = append(c.Errors, errors.New("Type func: incorrect type \""+typeStr+"\""))
  79. }
  80. return c
  81. }
  82. func (c *Client) Get(url string) (client *Client) {
  83. c.mu.Lock()
  84. c.Method = GET
  85. c.Url = url
  86. c.mu.Unlock()
  87. return c
  88. }
  89. func (c *Client) SendStruct(v interface{}) (client *Client) {
  90. bs, err := json.Marshal(v)
  91. if err != nil {
  92. c.Errors = append(c.Errors, err)
  93. return c
  94. }
  95. c.mu.Lock()
  96. c.JsonByte = bs
  97. c.mu.Unlock()
  98. return c
  99. }
  100. func (c *Client) SendString(v string) (client *Client) {
  101. c.mu.Lock()
  102. c.FormString = v
  103. c.mu.Unlock()
  104. return c
  105. }
  106. func (c *Client) EndStruct(v interface{}) (res *http.Response, errs []error) {
  107. res, bs, errs := c.EndBytes()
  108. if errs != nil && len(errs) > 0 {
  109. c.Errors = append(c.Errors, errs...)
  110. return nil, c.Errors
  111. }
  112. c.mu.RLock()
  113. defer c.mu.RUnlock()
  114. switch c.UnmarshalType {
  115. case TypeJSON:
  116. err := json.Unmarshal(bs, &v)
  117. if err != nil {
  118. c.Errors = append(c.Errors, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err))
  119. return nil, c.Errors
  120. }
  121. return res, nil
  122. case TypeXML:
  123. err := xml.Unmarshal(bs, &v)
  124. if err != nil {
  125. c.Errors = append(c.Errors, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err))
  126. return nil, c.Errors
  127. }
  128. return res, nil
  129. default:
  130. c.Errors = append(c.Errors, errors.New("UnmarshalType Type Wrong"))
  131. return nil, c.Errors
  132. }
  133. }
  134. func (c *Client) EndBytes() (res *http.Response, bs []byte, errs []error) {
  135. if len(c.Errors) > 0 {
  136. return nil, nil, c.Errors
  137. }
  138. var reader = strings.NewReader(NULL)
  139. req, err := func() (*http.Request, error) {
  140. c.mu.RLock()
  141. defer c.mu.RUnlock()
  142. switch c.Method {
  143. case GET:
  144. //todo: nothing
  145. case POST:
  146. switch c.RequestType {
  147. case TypeJSON:
  148. if c.JsonByte != nil {
  149. reader = strings.NewReader(string(c.JsonByte))
  150. }
  151. c.ContentType = Types[TypeJSON]
  152. case TypeForm, TypeFormData, TypeUrlencoded:
  153. reader = strings.NewReader(c.FormString)
  154. c.ContentType = Types[TypeForm]
  155. case TypeXML:
  156. reader = strings.NewReader(c.FormString)
  157. c.ContentType = Types[TypeXML]
  158. c.UnmarshalType = TypeXML
  159. default:
  160. return nil, errors.New("Request type Error ")
  161. }
  162. default:
  163. return nil, errors.New("Only support Get and Post ")
  164. }
  165. req, err := http.NewRequest(c.Method, c.Url, reader)
  166. if err != nil {
  167. return nil, err
  168. }
  169. req.Header = c.Header
  170. req.Header.Set("Content-Type", c.ContentType)
  171. c.HttpClient.Transport = c.Transport
  172. return req, nil
  173. }()
  174. if err != nil {
  175. c.Errors = append(c.Errors, err)
  176. return nil, nil, c.Errors
  177. }
  178. res, err = c.HttpClient.Do(req)
  179. if err != nil {
  180. c.Errors = append(c.Errors, err)
  181. return nil, nil, c.Errors
  182. }
  183. defer res.Body.Close()
  184. bs, err = ioutil.ReadAll(io.LimitReader(res.Body, int64(2<<20))) // default 2MB change the size you want
  185. if err != nil {
  186. c.Errors = append(c.Errors, err)
  187. return nil, nil, c.Errors
  188. }
  189. return res, bs, nil
  190. }