forked from plgd-dev/go-coap
/
client.go
124 lines (105 loc) · 2.98 KB
/
client.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
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
117
118
119
120
121
122
123
124
package client
import (
"context"
"io"
"net"
"github.com/matrix-org/go-coap/v2/message"
"github.com/matrix-org/go-coap/v2/mux"
"github.com/matrix-org/go-coap/v2/udp/message/pool"
)
type Client struct {
cc *ClientConn
}
func NewClient(cc *ClientConn) *Client {
return &Client{
cc: cc,
}
}
func (c *Client) Ping(ctx context.Context) error {
return c.cc.Ping(ctx)
}
func (c *Client) Delete(ctx context.Context, path string, opts ...message.Option) (*message.Message, error) {
resp, err := c.cc.Delete(ctx, path, opts...)
if err != nil {
return nil, err
}
defer pool.ReleaseMessage(resp)
return pool.ConvertTo(resp)
}
func (c *Client) Put(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*message.Message, error) {
resp, err := c.cc.Put(ctx, path, contentFormat, payload, opts...)
if err != nil {
return nil, err
}
defer pool.ReleaseMessage(resp)
return pool.ConvertTo(resp)
}
func (c *Client) Post(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*message.Message, error) {
resp, err := c.cc.Post(ctx, path, contentFormat, payload, opts...)
if err != nil {
return nil, err
}
defer pool.ReleaseMessage(resp)
return pool.ConvertTo(resp)
}
func (c *Client) Get(ctx context.Context, path string, opts ...message.Option) (*message.Message, error) {
resp, err := c.cc.Get(ctx, path, opts...)
if err != nil {
return nil, err
}
defer pool.ReleaseMessage(resp)
return pool.ConvertTo(resp)
}
func (c *Client) Close() error {
return c.cc.Close()
}
func (c *Client) RemoteAddr() net.Addr {
return c.cc.RemoteAddr()
}
func (c *Client) Context() context.Context {
return c.cc.Context()
}
func (c *Client) SetContextValue(key interface{}, val interface{}) {
c.cc.Session().SetContextValue(key, val)
}
func (c *Client) WriteMessage(req *message.Message) error {
r, err := pool.ConvertFrom(req)
if err != nil {
return err
}
defer pool.ReleaseMessage(r)
return c.cc.WriteMessage(r)
}
func (c *Client) Do(req *message.Message) (*message.Message, error) {
r, err := pool.ConvertFrom(req)
if err != nil {
return nil, err
}
defer pool.ReleaseMessage(r)
resp, err := c.cc.Do(r)
if err != nil {
return nil, err
}
defer pool.ReleaseMessage(resp)
return pool.ConvertTo(resp)
}
func createClientConnObserveHandler(observeFunc func(notification *message.Message)) func(n *pool.Message) {
return func(n *pool.Message) {
muxn, err := pool.ConvertTo(n)
if err != nil {
return
}
observeFunc(muxn)
}
}
func (c *Client) Observe(ctx context.Context, path string, observeFunc func(notification *message.Message), opts ...message.Option) (mux.Observation, error) {
return c.cc.Observe(ctx, path, createClientConnObserveHandler(observeFunc), opts...)
}
// Sequence acquires sequence number.
func (c *Client) Sequence() uint64 {
return c.cc.Sequence()
}
// ClientConn get's underlaying client connection.
func (c *Client) ClientConn() interface{} {
return c.cc
}