state.go 3.0 KB

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