state.go 3.2 KB

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