PhalApiClient.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package PhalApiClient
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "net"
  7. "net/http"
  8. "net/url"
  9. "time"
  10. )
  11. /**
  12. * PhalApi客户端SDK包(Go版)
  13. *
  14. *
  15. * <br>使用示例:<br>
  16. ```
  17. rs, err := PhalApiClient.NewRequest().
  18. WithHost(`http://127.0.0.1/PhalApi/Public/index.php`).
  19. WithService("Default.index").
  20. WithParams(url.Values{}).
  21. Get()
  22. if err != nil {
  23. fmt.Println(err.Error())
  24. } else {
  25. fmt.Println("code------------------------", rs.Code)
  26. fmt.Println("data------------------------", rs.Data)
  27. fmt.Println("msg------------------------", rs.Msg)
  28. }
  29. ```
  30. *
  31. * @package PhalApi\SDK
  32. * @author prettyyjnic <prettyyjnic@qq.com> 2016-03-09
  33. */
  34. type request struct {
  35. Host string
  36. Service string
  37. Params url.Values
  38. Timeout time.Duration
  39. }
  40. type result struct {
  41. Code int64 `json:"ret"`
  42. Data interface{} `json:"data"`
  43. Msg interface{} `json:"msg"`
  44. }
  45. func NewRequest() *request {
  46. ptr_request := new(request)
  47. return ptr_request
  48. }
  49. func (this *request) WithHost(host string) *request {
  50. this.Host = host
  51. return this
  52. }
  53. func (this *request) WithService(service string) *request {
  54. this.Service = service
  55. return this
  56. }
  57. func (this *request) WithParams(params url.Values) *request {
  58. this.Params = params
  59. return this
  60. }
  61. func (this *request) WithTimeout(timeout time.Duration) *request {
  62. this.Timeout = timeout
  63. return this
  64. }
  65. func (this *request) Reset() {
  66. this.Params = url.Values{}
  67. this.Host = ""
  68. this.Service = ""
  69. this.Timeout = 0
  70. }
  71. func (this *request) Get() (*result, error) {
  72. client := this.getClient()
  73. var str_url string
  74. if this.Service != "" {
  75. this.Params.Add("service", this.Service)
  76. }
  77. str_url = this.Host + "?" + this.Params.Encode()
  78. resp, err := client.Get(str_url)
  79. if err != nil {
  80. return nil, err
  81. } else {
  82. defer resp.Body.Close()
  83. return dealResult(resp)
  84. }
  85. }
  86. func (this *request) Post() (*result, error) {
  87. client := this.getClient()
  88. var str_url string
  89. if this.Service != "" {
  90. str_url = this.Host + "?service=" + this.Service
  91. } else {
  92. str_url = this.Host
  93. }
  94. resp, err := client.PostForm(str_url, this.Params)
  95. if err != nil {
  96. return nil, err
  97. } else {
  98. defer resp.Body.Close()
  99. return dealResult(resp)
  100. }
  101. }
  102. func dealResult(response *http.Response) (*result, error) {
  103. if response.Status == "200 OK" {
  104. ret := new(result)
  105. body, _ := ioutil.ReadAll(response.Body)
  106. json.Unmarshal(body, ret)
  107. return ret, nil
  108. } else {
  109. return nil, errors.New(response.Status)
  110. }
  111. }
  112. func (this *request) getClient() *http.Client {
  113. tr := &http.Transport{
  114. Dial: func(netw, addr string) (net.Conn, error) {
  115. if this.Timeout == 0 {
  116. this.Timeout = time.Second * 3
  117. }
  118. conn, err := net.DialTimeout(netw, addr, this.Timeout)
  119. if err != nil {
  120. return nil, err
  121. }
  122. conn.SetDeadline(time.Now().Add(this.Timeout))
  123. return conn, nil
  124. },
  125. ResponseHeaderTimeout: this.Timeout,
  126. }
  127. client := &http.Client{Transport: tr}
  128. return client
  129. }