invoke.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package invoke
  2. import (
  3. "git.sxidc.com/service-supports/dapr_api/utils"
  4. "github.com/go-resty/resty/v2"
  5. "net/url"
  6. "time"
  7. )
  8. type API struct {
  9. client *resty.Client
  10. baseUrl string
  11. }
  12. func NewAPI(baseUrl string, timeout time.Duration) *API {
  13. return &API{
  14. client: resty.New().SetTimeout(timeout),
  15. baseUrl: baseUrl,
  16. }
  17. }
  18. func DestroyAPI(api *API) {
  19. if api == nil {
  20. return
  21. }
  22. api.baseUrl = ""
  23. api.client = nil
  24. }
  25. func (api *API) PostJSON(methodName string, data []byte, headers map[string]string) ([]byte, error) {
  26. if headers == nil {
  27. headers = make(map[string]string)
  28. }
  29. headers["Content-Type"] = "application/json"
  30. return api.Post(methodName, headers, data)
  31. }
  32. func (api *API) PutJSON(methodName string, data []byte, headers map[string]string) ([]byte, error) {
  33. if headers == nil {
  34. headers = make(map[string]string)
  35. }
  36. headers["Content-Type"] = "application/json"
  37. return api.Put(methodName, headers, data)
  38. }
  39. func (api *API) PostWithoutHeaders(methodName string, data []byte) ([]byte, error) {
  40. return api.Post(methodName, nil, data)
  41. }
  42. func (api *API) PutWithoutHeaders(methodName string, data []byte) ([]byte, error) {
  43. return api.Put(methodName, nil, data)
  44. }
  45. func (api *API) DeleteWithoutHeaders(methodName string, queryParams map[string]string) ([]byte, error) {
  46. return api.Delete(methodName, queryParams, nil)
  47. }
  48. func (api *API) GetWithoutHeaders(methodName string, queryParams map[string]string) ([]byte, error) {
  49. return api.Get(methodName, queryParams, nil)
  50. }
  51. func (api *API) Post(methodName string, headers map[string]string, data []byte) ([]byte, error) {
  52. invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
  53. if err != nil {
  54. return nil, err
  55. }
  56. resp, err := api.client.R().
  57. SetHeaders(headers).
  58. SetBody(data).
  59. Post(invokeUrl)
  60. if err != nil {
  61. return nil, err
  62. }
  63. if resp.IsError() {
  64. return nil, utils.ResponseStatusError(invokeUrl, resp)
  65. }
  66. return resp.Body(), nil
  67. }
  68. func (api *API) Delete(methodName string, queryParams map[string]string, headers map[string]string) ([]byte, error) {
  69. invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
  70. if err != nil {
  71. return nil, err
  72. }
  73. resp, err := api.client.R().
  74. SetQueryParams(queryParams).
  75. SetHeaders(headers).
  76. Delete(invokeUrl)
  77. if err != nil {
  78. return nil, err
  79. }
  80. if resp.IsError() {
  81. return nil, utils.ResponseStatusError(invokeUrl, resp)
  82. }
  83. return resp.Body(), nil
  84. }
  85. func (api *API) Put(methodName string, headers map[string]string, data []byte) ([]byte, error) {
  86. invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
  87. if err != nil {
  88. return nil, err
  89. }
  90. resp, err := api.client.R().
  91. SetHeaders(headers).
  92. SetBody(data).
  93. Put(invokeUrl)
  94. if err != nil {
  95. return nil, err
  96. }
  97. if resp.IsError() {
  98. return nil, utils.ResponseStatusError(invokeUrl, resp)
  99. }
  100. return resp.Body(), nil
  101. }
  102. func (api *API) Get(methodName string, queryParams map[string]string, headers map[string]string) ([]byte, error) {
  103. invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
  104. if err != nil {
  105. return nil, err
  106. }
  107. resp, err := api.client.R().
  108. SetQueryParams(queryParams).
  109. SetHeaders(headers).
  110. Get(invokeUrl)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if resp.IsError() {
  115. return nil, utils.ResponseStatusError(invokeUrl, resp)
  116. }
  117. return resp.Body(), nil
  118. }