Browse Source

修改调用方法

master v0.3.0
guzeng 3 years ago
parent
commit
1838e087aa
5 changed files with 158 additions and 0 deletions
  1. +50
    -0
      client.info.go
  2. +16
    -0
      client.info_test.go
  3. +50
    -0
      client.is_open.go
  4. +17
    -0
      client.is_open_test.go
  5. +25
    -0
      conn.go

+ 50
- 0
client.info.go View File

@ -0,0 +1,50 @@
package supplierrpc
import (
"encoding/json"
"errors"
)
func Get(site_id, dbname, id string, url ...string) (map[string]string, error) {
if dbname == "" || id == "" {
return nil, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return nil, err
}
defer conn.Close()
arg := GetParam{site_id, dbname, id}
req, err := SetReqData(arg)
if err != nil {
return nil, err
}
res := &Response{}
err = conn.Get(req, res)
if err != nil {
return nil, err
}
res_data_de, err := GetResData(res)
if err != nil {
return nil, err
}
if res_data_de == "" {
return nil, nil
}
var res_arr map[string]string
err = json.Unmarshal([]byte(res_data_de), &res_arr)
if err != nil {
return nil, err
}
return res_arr, nil
}

+ 16
- 0
client.info_test.go View File

@ -0,0 +1,16 @@
package supplierrpc
import (
"testing"
)
func Test_Get(t *testing.T) {
siteid := "1056475"
dbname := "shop_v2"
key := "2"
res, err := Get(siteid, dbname, key)
t.Log(res)
t.Log(err)
}

+ 50
- 0
client.is_open.go View File

@ -0,0 +1,50 @@
package supplierrpc
import (
"encoding/json"
"errors"
)
func IsOpen(site_id, dbname, id string, url ...string) (*BoolRet, error) {
if dbname == "" || id == "" {
return nil, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return nil, err
}
defer conn.Close()
arg := GetParam{site_id, dbname, id}
req, err := SetReqData(arg)
if err != nil {
return nil, err
}
res := &Response{}
err = conn.IsOpen(req, res)
if err != nil {
return nil, err
}
res_data_de, err := GetResData(res)
if err != nil {
return nil, err
}
if res_data_de == "" {
return nil, nil
}
var res_arr BoolRet
err = json.Unmarshal([]byte(res_data_de), &res_arr)
if err != nil {
return nil, err
}
return &res_arr, nil
}

+ 17
- 0
client.is_open_test.go View File

@ -0,0 +1,17 @@
package supplierrpc
import (
"testing"
)
func Test_IsOpen(t *testing.T) {
siteid := "1056475"
dbname := "shop_v2"
key := "1"
res, err := IsOpen(siteid, dbname, key)
t.Log(res)
t.Log(res.Value)
t.Log(err)
}

+ 25
- 0
conn.go View File

@ -0,0 +1,25 @@
package supplierrpc
import (
"git.tetele.net/tgo/conf"
)
func rpc_server_conn(url ...string) (*SupplierServiceClient, error) {
var rpc_url string
if len(url) > 0 && url[0] != "" {
rpc_url = url[0]
} else if conf.SUPPLIER_RPC_URL != "" {
rpc_url = conf.SUPPLIER_RPC_URL
} else {
rpc_url = "127.0.0.1:" + conf.SUPPLIER_RPC_PORT
}
conn, _, err := DialSupplierService("tcp", rpc_url)
if err != nil {
return nil, err
}
return conn, nil
}

Loading…
Cancel
Save