http_client.go 4.6 KB

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