forked from KeisukeYamashita/go-jsonrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
116 lines (88 loc) · 2.65 KB
/
examples_test.go
File metadata and controls
116 lines (88 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package jsonrpc
import (
"fmt"
)
func Example() {
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Country string `json:"country"`
}
// create client
rpcClient := NewRPCClient("http://my-rpc-service")
// execute rpc to service
response, _ := rpcClient.Call("getPersonByID", 12345)
// parse result into struct
var person Person
response.GetObject(&person)
// change result and send back using rpc
person.Age = 35
rpcClient.Call("setPersonByID", 12345, person)
}
func ExampleRPCClient_Call() {
rpcClient := NewRPCClient("http://my-rpc-service")
// result processing omitted, see: RPCResponse methods
rpcClient.Call("getTimestamp")
rpcClient.Call("getPerson", 1234)
rpcClient.Call("addNumbers", 5, 2, 3)
rpcClient.Call("strangeFunction", 1, true, "alex", 3.4)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Country string `json:"country"`
}
person := Person{
Name: "alex",
Age: 33,
Country: "germany",
}
rpcClient.Call("setPersonByID", 123, person)
}
func ExampleRPCClient_CallNamed() {
rpcClient := NewRPCClient("http://my-rpc-service")
// result processing omitted, see: RPCResponse methods
rpcClient.CallNamed("createPerson", map[string]interface{}{
"name": "Bartholomew Allen",
"nicknames": []string{"Barry", "Flash"},
"male": true,
"age": 28,
"address": map[string]interface{}{"street": "Main Street", "city": "Central City"},
})
}
func ExampleRPCResponse() {
rpcClient := NewRPCClient("http://my-rpc-service")
response, _ := rpcClient.Call("addNumbers", 1, 2, 3)
sum, _ := response.GetInt()
fmt.Println(sum)
response, _ = rpcClient.Call("isValidEmail", "my@ema.il")
valid, _ := response.GetBool()
fmt.Println(valid)
response, _ = rpcClient.Call("getJoke")
joke, _ := response.GetString()
fmt.Println(joke)
response, _ = rpcClient.Call("getPi", 10)
piRounded, _ := response.GetFloat64()
fmt.Println(piRounded)
var rndNumbers []int
response, _ = rpcClient.Call("getRndIntNumbers", 10)
response.GetObject(&rndNumbers)
fmt.Println(rndNumbers[0])
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Country string `json:"country"`
}
var p Person
response, _ = rpcClient.Call("getPersonByID", 1234)
response.GetObject(&p)
fmt.Println(p.Name)
}
func ExampleRPCClient_Batch() {
rpcClient := NewRPCClient(httpServer.URL)
req1 := rpcClient.NewRPCRequestObject("addNumbers", 1, 2, 3)
req2 := rpcClient.NewRPCRequestObject("getTimestamp")
responses, _ := rpcClient.Batch(req1, req2)
response, _ := responses.GetResponseOf(req2)
timestamp, _ := response.GetInt()
fmt.Println(timestamp)
}