123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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
- }
|