package state import ( "encoding/json" "fmt" "git.sxidc.com/service-supports/dapr_api/utils" resty "github.com/go-resty/resty/v2" "strconv" "strings" "time" ) const ( saveUrlFormat = "http://localhost:%d/v1.0/state/%s" getUrlFormat = "http://localhost:%d/v1.0/state/%s/%s" getBulkUrlFormat = "http://localhost:%d/v1.0/state/%s/bulk" deleteUrlFormat = "http://localhost:%d/v1.0/state/%s/%s" transactionUrlFormat = "http://localhost:%d/v1.0/state/%s/transaction" ) type API struct { client *resty.Client port int } func NewAPI(port int, timeout time.Duration) *API { return &API{ client: resty.New().SetTimeout(timeout), port: port, } } func DestroyAPI(api *API) { if api == nil { return } api.port = 0 api.client = nil } func (api *API) SaveState(storeName string, request []SaveStateRequest) error { saveUrl := fmt.Sprintf(saveUrlFormat, api.port, storeName) resp, err := api.client.R(). SetHeader("Content-Type", "application/json"). SetBody(request). Post(saveUrl) if err != nil { return err } if resp.IsError() { return utils.ResponseStatusError(saveUrl, resp) } return nil } func (api *API) GetState(storeName string, key string, queryParams map[string]string) (string, string, error) { getUrl := fmt.Sprintf(getUrlFormat, api.port, storeName, key) resp, err := api.client.R(). SetQueryParams(queryParams). Get(getUrl) if err != nil { return "", "", err } if resp.IsError() { return "", "", utils.ResponseStatusError(getUrl, resp) } var body string if resp.Body() != nil && len(resp.Body()) != 0 { body = string(resp.Body()) body, err = strconv.Unquote(body) if err != nil { return "", "", err } body = strings.Trim(body, "\"") } return body, resp.Header().Get("Etag"), nil } func (api *API) GetStateBulk(storeName string, queryParams map[string]string, request GetStateBulkRequest) ([]GetStateBulkItem, error) { getBulkUrl := fmt.Sprintf(getBulkUrlFormat, api.port, storeName) resp, err := api.client.R(). SetQueryParams(queryParams). SetBody(request). Post(getBulkUrl) if err != nil { return nil, err } if resp.IsError() { return nil, utils.ResponseStatusError(getBulkUrl, resp) } items := make([]GetStateBulkItem, 0) if resp.Body() != nil && len(resp.Body()) != 0 { err = json.Unmarshal(resp.Body(), &items) if err != nil { return nil, err } } return items, nil } func (api *API) DeleteState(storeName string, key string, queryParams map[string]string) error { deleteUrl := fmt.Sprintf(deleteUrlFormat, api.port, storeName, key) resp, err := api.client.R(). SetQueryParams(queryParams). Delete(deleteUrl) if err != nil { return err } if resp.IsError() { return utils.ResponseStatusError(deleteUrl, resp) } return nil } func (api *API) Transaction(storeName string, request TransactionRequest) error { transactionUrl := fmt.Sprintf(transactionUrlFormat, api.port, storeName) resp, err := api.client.R(). SetHeader("Content-Type", "application/json"). SetBody(request). Post(transactionUrl) if err != nil { return err } if resp.IsError() { return utils.ResponseStatusError(transactionUrl, resp) } return nil }