-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
83 lines (70 loc) · 1.5 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"fmt"
"time"
dapr "github.com/dapr/go-sdk/client"
)
type ValueType struct {
Value interface{} `json:"value"`
Time int64 `json:"time"`
}
func main() {
client, err := dapr.NewClient()
if nil != err {
panic(err)
}
// create entity.
createUrl := "plugins/pluginA/entities?id=test1&owner=abc&source=abc&type=device"
result, err := client.InvokeMethodWithContent(context.Background(),
"core",
createUrl,
"POST",
&dapr.DataContent{
ContentType: "application/json",
})
if nil != err {
panic(err)
}
fmt.Println(string(result))
// get entity.
getUrl := "plugins/pluginA/entities/test1?owner=abc&source=abc&type=device"
result, err = client.InvokeMethodWithContent(context.Background(),
"core",
getUrl,
"GET",
&dapr.DataContent{
ContentType: "application/json",
})
if nil != err {
panic(err)
}
fmt.Println(string(result))
// pub entity.
data := make(map[string]interface{})
data["entity_id"] = "test1"
data["owner"] = "abc"
dataItem := make(map[string]interface{})
dataItem["core"] = ValueType{Value: 189, Time: time.Now().UnixNano() / 1e6}
data["data"] = dataItem
err = client.PublishEvent(context.Background(),
"client-pubsub",
"core-pub",
data,
)
if nil != err {
panic(err)
}
// get entity.
result, err = client.InvokeMethodWithContent(context.Background(),
"core",
getUrl,
"GET",
&dapr.DataContent{
ContentType: "application/json",
})
if nil != err {
panic(err)
}
fmt.Println(string(result))
}