فهرست منبع

完成invoke接口编写

yjp 3 سال پیش
والد
کامیت
3dc182988e
1فایلهای تغییر یافته به همراه64 افزوده شده و 11 حذف شده
  1. 64 11
      invoke/invoke.go

+ 64 - 11
invoke/invoke.go

@@ -7,6 +7,10 @@ import (
 	"time"
 )
 
+var (
+	contentTypeJSONHeader = map[string]string{"ContentType": "application/json"}
+)
+
 type API struct {
 	client  *resty.Client
 	baseUrl string
@@ -28,20 +32,20 @@ func DestroyAPI(api *API) {
 	api.client = nil
 }
 
-func (api *API) PostJSON() {
-
+func (api *API) PostJSON(methodName string, data []byte) ([]byte, error) {
+	return api.Post(methodName, contentTypeJSONHeader, data)
 }
 
-func (api *API) GetJSON() {
-
+func (api *API) DeleteJSON(methodName string) ([]byte, error) {
+	return api.Delete(methodName, contentTypeJSONHeader)
 }
 
-func (api *API) PutJSON() {
-
+func (api *API) PutJSON(methodName string, data []byte) ([]byte, error) {
+	return api.Put(methodName, contentTypeJSONHeader, data)
 }
 
-func (api *API) DeleteJSON() {
-
+func (api *API) GetJSON(methodName string) ([]byte, error) {
+	return api.Get(methodName, contentTypeJSONHeader)
 }
 
 func (api *API) Post(methodName string, headers map[string]string, data []byte) ([]byte, error) {
@@ -65,14 +69,63 @@ func (api *API) Post(methodName string, headers map[string]string, data []byte)
 	return resp.Body(), nil
 }
 
-func (api *API) Get() {
+func (api *API) Delete(methodName string, headers map[string]string) ([]byte, error) {
+	invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
+	if err != nil {
+		return nil, err
+	}
+
+	resp, err := api.client.R().
+		SetHeaders(headers).
+		Delete(invokeUrl)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.IsError() {
+		return nil, utils.ResponseStatusError(resp)
+	}
 
+	return resp.Body(), nil
 }
 
-func (api *API) Put() {
+func (api *API) Put(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).
+		Put(invokeUrl)
+	if err != nil {
+		return nil, err
+	}
 
+	if resp.IsError() {
+		return nil, utils.ResponseStatusError(resp)
+	}
+
+	return resp.Body(), nil
 }
 
-func (api *API) Delete() {
+func (api *API) Get(methodName string, headers map[string]string) ([]byte, error) {
+	invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
+	if err != nil {
+		return nil, err
+	}
+
+	resp, err := api.client.R().
+		SetHeaders(headers).
+		Get(invokeUrl)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.IsError() {
+		return nil, utils.ResponseStatusError(resp)
+	}
 
+	return resp.Body(), nil
 }