From e49473afa5e9c1586c06b971d19fbc51560deef6 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期五, 31 五月 2019 13:13:26 +0800
Subject: [PATCH] add rply mode
---
reqrepClient.go | 79 ++++++++++++++++++++++++++++-----------
1 files changed, 56 insertions(+), 23 deletions(-)
diff --git a/reqrepClient.go b/reqrepClient.go
index 46abcdc..ebdc397 100644
--- a/reqrepClient.go
+++ b/reqrepClient.go
@@ -1,36 +1,69 @@
package dbapi
import (
- "dbapi/deliver"
- "dbapi/protomsg"
+ "basic.com/valib/deliver.git"
"encoding/json"
- "fmt"
+ "errors"
)
-type ReqrepApi struct {
+type ReqRepClient struct {}
+var ReqRep_URL = "tcp://192.168.1.11:8002"
+
+//鍒濆鍖杛ply鐨勫湴鍧�
+func InitRplyUrl(url string) {
+ ReqRep_URL = url
}
-const (
- ReqRep_URL = "tcp://127.0.0.1:8002"
-)
-
-func (api ReqrepApi)SendRequestAndGetReply(reqStr string) string {
- sender := deliver.NewConsumer(deliver.Mode(deliver.ReqRep), ReqRep_URL)
- var sendMsg = protomsg.SdkMessage{}
- sendMsg.Cid = "kkkk"
- byteArr, _ := json.Marshal(sendMsg)
- err := sender.Send(byteArr)
- if err !=nil {
- fmt.Println(err)
+func (api ReqRepClient) DoGetRequest(url string, params map[string]string, headers map[string]string) ([]byte, error) {
+ var reqBody RplyParamBody
+ reqBody.Action = url
+ var destMap map[string]interface{}
+ for idx,val := range params {
+ destMap[idx] = interface{}(val)
}
- fmt.Println("sendedMsg")
+ reqBody.Params = destMap
+ return doRply(reqBody)
+}
- //recvMsg, err := sender.Recv()
- //if err ==nil {
- // fmt.Println(string(recvMsg))
- //}
+func (api ReqRepClient) DoPostRequest(url string, contentType string, body map[string]interface{}, params map[string]string, headers map[string]string) ([]byte, error) {
+ var reqBody RplyParamBody
+ reqBody.Action = url
+ reqBody.Params = body
+ return doRply(reqBody)
+}
- fmt.Println("SendRequest")
- return ""
+func (api ReqRepClient) DoPutRequest(url string, contentType string, body map[string]interface{}, headers map[string]string) ([]byte, error) {
+ var reqBody RplyParamBody
+ reqBody.Action = url
+ reqBody.Params = body
+ return doRply(reqBody)
+}
+
+func (api ReqRepClient) DoDeleteRequest(url string, contentType string, body map[string]interface{}, headers map[string]string) ([]byte, error) {
+ var reqBody RplyParamBody
+ reqBody.Action = url
+ reqBody.Params = body
+ return doRply(reqBody)
+}
+
+type RplyParamBody struct {
+ Action string `json:"action"` //鏍规嵁action鍒ゆ柇鍋氫綍绉嶅搷搴�
+ Params map[string]interface{} `json:"params"`//鍙傛暟map
+}
+
+func doRply(body RplyParamBody)([]byte,error){
+ sender := deliver.NewClient(deliver.Mode(deliver.ReqRep), ReqRep_URL)
+ bytes,err := json.Marshal(body)
+ if err !=nil {
+ return nil,err
+ }
+ err = sender.Send(bytes)
+ if err !=nil {
+ resMsg, _ := sender.Recv()
+ if resMsg !=nil {
+ return resMsg,nil
+ }
+ }
+ return nil,errors.New("nng no resp")
}
\ No newline at end of file
--
Gitblit v1.8.0