-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_test.go
66 lines (52 loc) · 1.44 KB
/
doc_test.go
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
package jrpc2client_test
import (
"fmt"
"time"
"github.com/riftbit/jrpc2client"
)
// This function is named ExampleClient_Call(), this way godoc knows to associate
// it with the Client type and method Call.
func ExampleClient_Call() {
type TestReply struct {
LogID string `json:"log_id"`
UserAgent string `json:"user_agent"`
}
type TestArgs struct {
ID string
}
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:8080")
dstP := &TestReply{}
client.SetClientTimeout(10 * time.Millisecond)
// final url will be http://127.0.0.1:8080/api
err := client.Call("/api", "demo.TestClientTimeout", TestArgs{ID: "123"}, dstP)
if err != nil {
panic(err)
}
fmt.Println(dstP.LogID)
}
// This function is named ExampleClient_CallForMap(), this way godoc knows to associate
// it with the Client type and method CallForMap.
func ExampleClient_CallForMap() {
type TestArgs struct {
ID string
}
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:8080")
client.SetClientTimeout(10 * time.Millisecond)
// final url will be http://127.0.0.1:8080/api
dstM, err := client.CallForMap("/api", "demo.TestClientTimeout", TestArgs{ID: "321"})
if err != nil {
panic(err)
}
val, ok := dstM["log_id"]
if ok != true {
panic("key log_id not exists")
}
fmt.Println(val)
}
func ExampleNewClient() {
client := jrpc2client.NewClient()
//print empty line because BaseURL not setted
println(client.BaseURL)
}