client.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package client
  2. import (
  3. "git.sxidc.com/go-tools/utils/http_client"
  4. "time"
  5. )
  6. const (
  7. defaultTimeoutSec = 30
  8. )
  9. var instance *Client
  10. func InitInstance(timeout time.Duration) {
  11. if instance == nil {
  12. if timeout == 0 {
  13. timeout = defaultTimeoutSec * time.Second
  14. }
  15. instance = New(timeout)
  16. }
  17. }
  18. func GetInstance() *Client {
  19. if instance == nil {
  20. panic("还没有调用InitInstance")
  21. }
  22. return instance
  23. }
  24. type Client struct {
  25. client *http_client.Client
  26. timeout time.Duration
  27. }
  28. func New(timeout time.Duration) *Client {
  29. client := http_client.New()
  30. return &Client{
  31. client: client,
  32. timeout: timeout,
  33. }
  34. }
  35. func (c *Client) post(token string, url string, request interface{}, result interface{}) error {
  36. req := c.client.NewRequest(http_client.WithNewRequestTimeout(c.timeout))
  37. resp, err := req.Post(url, request,
  38. http_client.WithRequestHeaders(map[string]string{
  39. "Content-Type": "application/json",
  40. "Authorization": token,
  41. }))
  42. if err != nil {
  43. return err
  44. }
  45. err = resp.Json(result)
  46. if err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func (c *Client) delete(token string, url string, queryMap map[string]string, result interface{}) error {
  52. req := c.client.NewRequest(http_client.WithNewRequestTimeout(c.timeout))
  53. resp, err := req.Delete(url,
  54. http_client.WithRequestHeaders(map[string]string{
  55. "Content-Type": "application/json",
  56. "Authorization": token,
  57. }),
  58. http_client.WithRequestQueryParams(queryMap))
  59. if err != nil {
  60. return err
  61. }
  62. err = resp.Json(result)
  63. if err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. func (c *Client) put(token string, url string, request interface{}, result interface{}) error {
  69. req := c.client.NewRequest(http_client.WithNewRequestTimeout(c.timeout))
  70. resp, err := req.Put(url, request,
  71. http_client.WithRequestHeaders(map[string]string{
  72. "Content-Type": "application/json",
  73. "Authorization": token,
  74. }))
  75. if err != nil {
  76. return err
  77. }
  78. err = resp.Json(result)
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. func (c *Client) get(token string, url string, queryMap map[string]string, result interface{}) error {
  85. req := c.client.NewRequest(http_client.WithNewRequestTimeout(c.timeout))
  86. resp, err := req.Get(url,
  87. http_client.WithRequestHeaders(map[string]string{
  88. "Content-Type": "application/json",
  89. "Authorization": token,
  90. }),
  91. http_client.WithRequestQueryParams(queryMap))
  92. if err != nil {
  93. return err
  94. }
  95. err = resp.Json(result)
  96. if err != nil {
  97. return err
  98. }
  99. return nil
  100. }