invoke.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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) DeleteJSON(methodName string) ([]byte, error) {
  32. return api.Delete(methodName, contentTypeJSONHeader)
  33. }
  34. func (api *API) PutJSON(methodName string, data []byte) ([]byte, error) {
  35. return api.Put(methodName, contentTypeJSONHeader, data)
  36. }
  37. func (api *API) GetJSON(methodName string) ([]byte, error) {
  38. return api.Get(methodName, contentTypeJSONHeader)
  39. }
  40. func (api *API) Post(methodName string, headers map[string]string, data []byte) ([]byte, error) {
  41. invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
  42. if err != nil {
  43. return nil, err
  44. }
  45. resp, err := api.client.R().
  46. SetHeaders(headers).
  47. SetBody(data).
  48. Post(invokeUrl)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if resp.IsError() {
  53. return nil, utils.ResponseStatusError(resp)
  54. }
  55. return resp.Body(), nil
  56. }
  57. func (api *API) Delete(methodName string, headers map[string]string) ([]byte, error) {
  58. invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
  59. if err != nil {
  60. return nil, err
  61. }
  62. resp, err := api.client.R().
  63. SetHeaders(headers).
  64. Delete(invokeUrl)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if resp.IsError() {
  69. return nil, utils.ResponseStatusError(resp)
  70. }
  71. return resp.Body(), nil
  72. }
  73. func (api *API) Put(methodName string, headers map[string]string, data []byte) ([]byte, error) {
  74. invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
  75. if err != nil {
  76. return nil, err
  77. }
  78. resp, err := api.client.R().
  79. SetHeaders(headers).
  80. SetBody(data).
  81. Put(invokeUrl)
  82. if err != nil {
  83. return nil, err
  84. }
  85. if resp.IsError() {
  86. return nil, utils.ResponseStatusError(resp)
  87. }
  88. return resp.Body(), nil
  89. }
  90. func (api *API) Get(methodName string, headers map[string]string) ([]byte, error) {
  91. invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
  92. if err != nil {
  93. return nil, err
  94. }
  95. resp, err := api.client.R().
  96. SetHeaders(headers).
  97. Get(invokeUrl)
  98. if err != nil {
  99. return nil, err
  100. }
  101. if resp.IsError() {
  102. return nil, utils.ResponseStatusError(resp)
  103. }
  104. return resp.Body(), nil
  105. }