http_client.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package gopay
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "encoding/xml"
  6. "errors"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. "sync"
  12. )
  13. const (
  14. POST = "POST"
  15. GET = "GET"
  16. TypeJSON = "json"
  17. TypeXML = "xml"
  18. TypeUrlencoded = "urlencoded"
  19. TypeForm = "form"
  20. TypeFormData = "form-data"
  21. TypeHTML = "html"
  22. TypeText = "text"
  23. TypeMultipart = "multipart"
  24. )
  25. var Types = map[string]string{
  26. TypeJSON: "application/json",
  27. TypeXML: "application/xml",
  28. TypeForm: "application/x-www-form-urlencoded",
  29. TypeFormData: "application/x-www-form-urlencoded",
  30. TypeUrlencoded: "application/x-www-form-urlencoded",
  31. TypeHTML: "text/html",
  32. TypeText: "text/plain",
  33. TypeMultipart: "multipart/form-data",
  34. }
  35. type Client struct {
  36. HttpClient *http.Client
  37. Transport *http.Transport
  38. Url string
  39. Method string
  40. ForceType string
  41. FormString string
  42. ContentType string
  43. UnmarshalType string
  44. Types map[string]string
  45. JsonByte []byte
  46. Errors []error
  47. mu sync.RWMutex
  48. }
  49. // NewHttpClient , default tls.Config{InsecureSkipVerify: true}
  50. func NewHttpClient() (client *Client) {
  51. c := new(Client)
  52. c.HttpClient = new(http.Client)
  53. c.Transport = &http.Transport{}
  54. c.Transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
  55. c.ForceType = TypeUrlencoded
  56. c.UnmarshalType = TypeJSON
  57. c.Errors = make([]error, 0)
  58. return c
  59. }
  60. func (c *Client) SetTLSConfig(tlsCfg *tls.Config) (client *Client) {
  61. c.mu.Lock()
  62. c.Transport.TLSClientConfig = tlsCfg
  63. c.mu.Unlock()
  64. return c
  65. }
  66. func (c *Client) Post(url string) (client *Client) {
  67. c.mu.Lock()
  68. c.Method = POST
  69. c.Url = url
  70. c.mu.Unlock()
  71. return c
  72. }
  73. func (c *Client) Type(typeStr string) (client *Client) {
  74. if _, ok := Types[typeStr]; ok {
  75. c.ForceType = typeStr
  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.JsonByte = bs
  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. switch c.UnmarshalType {
  110. case TypeJSON:
  111. err := json.Unmarshal(bs, &v)
  112. if err != nil {
  113. c.Errors = append(c.Errors, fmt.Errorf("json.Unmarshal:%s", err.Error()))
  114. return nil, c.Errors
  115. }
  116. return res, nil
  117. case TypeXML:
  118. err := xml.Unmarshal(bs, &v)
  119. if err != nil {
  120. c.Errors = append(c.Errors, fmt.Errorf("xml.Unmarshal:%s", err.Error()))
  121. return nil, c.Errors
  122. }
  123. return res, nil
  124. default:
  125. c.Errors = append(c.Errors, errors.New("UnmarshalType Type Wrong"))
  126. return nil, c.Errors
  127. }
  128. }
  129. func (c *Client) EndBytes() (res *http.Response, bs []byte, errs []error) {
  130. if len(c.Errors) > 0 {
  131. return nil, nil, c.Errors
  132. }
  133. var reader *strings.Reader
  134. switch c.Method {
  135. case GET:
  136. reader = strings.NewReader(null)
  137. case POST:
  138. switch c.ForceType {
  139. case TypeJSON:
  140. if c.JsonByte != nil {
  141. reader = strings.NewReader(string(c.JsonByte))
  142. }
  143. c.ContentType = Types[TypeJSON]
  144. case TypeForm, TypeFormData, TypeUrlencoded:
  145. reader = strings.NewReader(c.FormString)
  146. c.ContentType = Types[TypeForm]
  147. case TypeXML:
  148. reader = strings.NewReader(c.FormString)
  149. c.ContentType = Types[TypeXML]
  150. c.UnmarshalType = TypeXML
  151. default:
  152. c.Errors = append(c.Errors, errors.New("Request type Error "))
  153. }
  154. default:
  155. reader = strings.NewReader(null)
  156. }
  157. req, err := http.NewRequest(c.Method, c.Url, reader)
  158. if err != nil {
  159. c.Errors = append(c.Errors, err)
  160. return nil, nil, c.Errors
  161. }
  162. req.Header.Set("Content-Type", c.ContentType)
  163. c.HttpClient.Transport = c.Transport
  164. res, err = c.HttpClient.Do(req)
  165. if err != nil {
  166. c.Errors = append(c.Errors, err)
  167. return nil, nil, c.Errors
  168. }
  169. defer res.Body.Close()
  170. bs, err = ioutil.ReadAll(res.Body)
  171. if err != nil {
  172. c.Errors = append(c.Errors, err)
  173. return nil, nil, c.Errors
  174. }
  175. return res, bs, nil
  176. }