http_client.go 4.5 KB

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