state.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package state
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.sxidc.com/service-supports/dapr_api/utils"
  6. resty "github.com/go-resty/resty/v2"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. const (
  12. saveUrlFormat = "http://localhost:%d/v1.0/state/%s"
  13. getUrlFormat = "http://localhost:%d/v1.0/state/%s/%s"
  14. getBulkUrlFormat = "http://localhost:%d/v1.0/state/%s/bulk"
  15. deleteUrlFormat = "http://localhost:%d/v1.0/state/%s/%s"
  16. transactionUrlFormat = "http://localhost:%d/v1.0/state/%s/transaction"
  17. )
  18. type API struct {
  19. client *resty.Client
  20. port int
  21. }
  22. func NewAPI(port int, timeout time.Duration) *API {
  23. return &API{
  24. client: resty.New().SetTimeout(timeout),
  25. port: port,
  26. }
  27. }
  28. func DestroyAPI(api *API) {
  29. if api == nil {
  30. return
  31. }
  32. api.port = 0
  33. api.client = nil
  34. }
  35. func (api *API) SaveState(storeName string, request []SaveStateRequest) error {
  36. saveUrl := fmt.Sprintf(saveUrlFormat, api.port, storeName)
  37. resp, err := api.client.R().
  38. SetHeader("Content-Type", "application/json").
  39. SetBody(request).
  40. Post(saveUrl)
  41. if err != nil {
  42. return err
  43. }
  44. if resp.IsError() {
  45. return utils.ResponseStatusError(saveUrl, resp)
  46. }
  47. return nil
  48. }
  49. func (api *API) GetState(storeName string, key string, queryParams map[string]string) (string, string, error) {
  50. getUrl := fmt.Sprintf(getUrlFormat, api.port, storeName, key)
  51. resp, err := api.client.R().
  52. SetQueryParams(queryParams).
  53. Get(getUrl)
  54. if err != nil {
  55. return "", "", err
  56. }
  57. if resp.IsError() {
  58. return "", "", utils.ResponseStatusError(getUrl, resp)
  59. }
  60. var body string
  61. if resp.Body() != nil && len(resp.Body()) != 0 {
  62. body = string(resp.Body())
  63. body, err = strconv.Unquote(body)
  64. if err != nil {
  65. return "", "", err
  66. }
  67. body = strings.Trim(body, "\"")
  68. }
  69. return body, resp.Header().Get("Etag"), nil
  70. }
  71. func (api *API) GetStateBulk(storeName string, queryParams map[string]string, request GetStateBulkRequest) ([]GetStateBulkItem, error) {
  72. getBulkUrl := fmt.Sprintf(getBulkUrlFormat, api.port, storeName)
  73. resp, err := api.client.R().
  74. SetQueryParams(queryParams).
  75. SetBody(request).
  76. Post(getBulkUrl)
  77. if err != nil {
  78. return nil, err
  79. }
  80. if resp.IsError() {
  81. return nil, utils.ResponseStatusError(getBulkUrl, resp)
  82. }
  83. items := make([]GetStateBulkItem, 0)
  84. if resp.Body() != nil && len(resp.Body()) != 0 {
  85. err = json.Unmarshal(resp.Body(), &items)
  86. if err != nil {
  87. return nil, err
  88. }
  89. }
  90. return items, nil
  91. }
  92. func (api *API) DeleteState(storeName string, key string, queryParams map[string]string) error {
  93. deleteUrl := fmt.Sprintf(deleteUrlFormat, api.port, storeName, key)
  94. resp, err := api.client.R().
  95. SetQueryParams(queryParams).
  96. Delete(deleteUrl)
  97. if err != nil {
  98. return err
  99. }
  100. if resp.IsError() {
  101. return utils.ResponseStatusError(deleteUrl, resp)
  102. }
  103. return nil
  104. }
  105. func (api *API) Transaction(storeName string, request TransactionRequest) error {
  106. transactionUrl := fmt.Sprintf(transactionUrlFormat, api.port, storeName)
  107. resp, err := api.client.R().
  108. SetHeader("Content-Type", "application/json").
  109. SetBody(request).
  110. Post(transactionUrl)
  111. if err != nil {
  112. return err
  113. }
  114. if resp.IsError() {
  115. return utils.ResponseStatusError(transactionUrl, resp)
  116. }
  117. return nil
  118. }