invoke.go 3.1 KB

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