state.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. )
  14. type API struct {
  15. port int
  16. client *resty.Client
  17. }
  18. func NewAPI(port int, timeout time.Duration) *API {
  19. return &API{
  20. port: port,
  21. client: resty.New().SetTimeout(timeout),
  22. }
  23. }
  24. func DestroyAPI(api *API) {
  25. if api == nil {
  26. return
  27. }
  28. api.port = 0
  29. api.client = nil
  30. }
  31. func (api *API) SaveState(storeName string, request []SaveStateRequest) error {
  32. saveUrl := fmt.Sprintf(saveUrlFormat, api.port, storeName)
  33. resp, err := api.client.R().
  34. SetHeader("Content-Type", "application/json").
  35. SetBody(request).
  36. Post(saveUrl)
  37. if err != nil {
  38. return err
  39. }
  40. if resp.IsError() {
  41. return fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
  42. }
  43. return nil
  44. }
  45. func (api *API) GetState(storeName string, key string, queryParams map[string]string, result interface{}) (string, error) {
  46. getUrl := fmt.Sprintf(getUrlFormat, api.port, storeName, key)
  47. resp, err := api.client.R().
  48. SetQueryParams(queryParams).
  49. SetResult(result).
  50. Get(getUrl)
  51. if err != nil {
  52. return "", err
  53. }
  54. if resp.IsError() {
  55. return "", fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
  56. }
  57. return resp.Header().Get("Etag"), nil
  58. }
  59. func (api *API) GetStateBulk(storeName string, queryParams map[string]string, request GetStateBulkRequest) ([]GetStateBulkItem, error) {
  60. getBulkUrl := fmt.Sprintf(getBulkUrlFormat, api.port, storeName)
  61. resp, err := api.client.R().
  62. SetQueryParams(queryParams).
  63. SetBody(request).
  64. Post(getBulkUrl)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if resp.IsError() {
  69. return nil, fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
  70. }
  71. items := make([]GetStateBulkItem, 0)
  72. if resp.Body() != nil && len(resp.Body()) != 0 {
  73. err = json.Unmarshal(resp.Body(), &items)
  74. if err != nil {
  75. return nil, err
  76. }
  77. }
  78. return items, nil
  79. }
  80. func (api *API) DeleteState(storeName string, key string, queryParams map[string]string) error {
  81. deleteUrl := fmt.Sprintf(deleteUrlFormat, api.port, storeName, key)
  82. resp, err := api.client.R().
  83. SetQueryParams(queryParams).
  84. Delete(deleteUrl)
  85. if err != nil {
  86. return err
  87. }
  88. if resp.IsError() {
  89. return fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
  90. }
  91. return nil
  92. }
  93. func (api *API) Transaction() {
  94. }