소스 검색

修改报错

yjp 3 년 전
부모
커밋
439f59a305
3개의 변경된 파일87개의 추가작업 그리고 7개의 파일을 삭제
  1. 78 0
      invoke/invoke.go
  2. 3 2
      pubsub/pubsub.go
  3. 6 5
      state/state.go

+ 78 - 0
invoke/invoke.go

@@ -0,0 +1,78 @@
+package invoke
+
+import (
+	"errors"
+	"github.com/go-resty/resty/v2"
+	"net/url"
+	"time"
+)
+
+type API struct {
+	client  *resty.Client
+	baseUrl string
+}
+
+func NewAPI(baseUrl string, timeout time.Duration) *API {
+	return &API{
+		client:  resty.New().SetTimeout(timeout),
+		baseUrl: baseUrl,
+	}
+}
+
+func DestroyAPI(api *API) {
+	if api == nil {
+		return
+	}
+
+	api.baseUrl = ""
+	api.client = nil
+}
+
+func (api *API) PostJSON() {
+
+}
+
+func (api *API) GetJSON() {
+
+}
+
+func (api *API) PutJSON() {
+
+}
+
+func (api *API) DeleteJSON() {
+
+}
+
+func (api *API) Post(methodName string, headers map[string]string, data []byte) ([]byte, error) {
+	invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
+	if err != nil {
+		return nil, err
+	}
+
+	resp, err := api.client.R().
+		SetHeaders(headers).
+		SetBody(data).
+		Post(invokeUrl)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.IsError() {
+		return nil, errors.New(string(resp.Body()))
+	}
+
+	return resp.Body(), nil
+}
+
+func (api *API) Get() {
+
+}
+
+func (api *API) Put() {
+
+}
+
+func (api *API) Delete() {
+
+}

+ 3 - 2
pubsub/pubsub.go

@@ -1,6 +1,7 @@
 package pubsub
 
 import (
+	"errors"
 	"fmt"
 	"github.com/go-resty/resty/v2"
 	"time"
@@ -11,8 +12,8 @@ const (
 )
 
 type API struct {
-	port   int
 	client *resty.Client
+	port   int
 }
 
 func NewAPI(port int, timeout time.Duration) *API {
@@ -53,7 +54,7 @@ func (api *API) publish(pubsubName string, topic string, contentType string, dat
 	}
 
 	if resp.IsError() {
-		return fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
+		return errors.New(string(resp.Body()))
 	}
 
 	return nil

+ 6 - 5
state/state.go

@@ -2,6 +2,7 @@ package state
 
 import (
 	"encoding/json"
+	"errors"
 	"fmt"
 	resty "github.com/go-resty/resty/v2"
 	"strconv"
@@ -50,7 +51,7 @@ func (api *API) SaveState(storeName string, request []SaveStateRequest) error {
 	}
 
 	if resp.IsError() {
-		return fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
+		return errors.New(string(resp.Body()))
 	}
 
 	return nil
@@ -67,7 +68,7 @@ func (api *API) GetState(storeName string, key string, queryParams map[string]st
 	}
 
 	if resp.IsError() {
-		return "", "", fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
+		return "", "", errors.New(string(resp.Body()))
 	}
 
 	var body string
@@ -96,7 +97,7 @@ func (api *API) GetStateBulk(storeName string, queryParams map[string]string, re
 	}
 
 	if resp.IsError() {
-		return nil, fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
+		return nil, errors.New(string(resp.Body()))
 	}
 
 	items := make([]GetStateBulkItem, 0)
@@ -121,7 +122,7 @@ func (api *API) DeleteState(storeName string, key string, queryParams map[string
 	}
 
 	if resp.IsError() {
-		return fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
+		return errors.New(string(resp.Body()))
 	}
 
 	return nil
@@ -139,7 +140,7 @@ func (api *API) Transaction(storeName string, request TransactionRequest) error
 	}
 
 	if resp.IsError() {
-		return fmt.Errorf("Status %d: %s\n", resp.StatusCode(), resp.Body())
+		return errors.New(string(resp.Body()))
 	}
 
 	return nil