This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathclient.go
146 lines (119 loc) · 3.25 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package t38c
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/tidwall/gjson"
"github.com/xjem/t38c/transport"
)
// Client allows you to interact with the Tile38 server.
type Client struct {
debug bool
exec Executor
Search *Search
Keys *Keys
Webhooks *Hooks
Channels *Channels
Scripting *Scripting
Geofence *Geofence
Server *Server
}
type clientParams struct {
debug bool
password *string
poolSize int
}
// ClientOption ...
type ClientOption func(*clientParams)
// Debug option.
var Debug = ClientOption(func(c *clientParams) {
c.debug = true
})
// WithPassword option.
func WithPassword(password string) ClientOption {
return func(c *clientParams) {
c.password = &password
}
}
// SetPoolSize option.
func SetPoolSize(size int) ClientOption {
return func(c *clientParams) {
c.poolSize = size
}
}
// New creates a new Tile38 client.
// By default uses redis pool with 5 connections.
// In debug mode will also print commands which will be sent to the server.
func New(addr string, opts ...ClientOption) (*Client, error) {
params := &clientParams{poolSize: 5}
for _, opt := range opts {
opt(params)
}
radixPool, err := transport.NewRadixPool(addr, params.poolSize, params.password)
if err != nil {
return nil, err
}
return NewWithExecutor(radixPool, params.debug)
}
// NewWithExecutor creates a new Tile38 client with provided executor.
// See Executor interface for more information.
func NewWithExecutor(exec Executor, debug bool) (*Client, error) {
client := &Client{
exec: exec,
debug: debug,
}
if err := client.Ping(); err != nil {
return nil, err
}
// Health check can be used this way to test the readiness of tile38
// if healthError := client.HealthZ(); healthError != nil {
// return nil, healthError
// }
client.Webhooks = &Hooks{client}
client.Geofence = &Geofence{client}
client.Keys = &Keys{client}
client.Search = &Search{client}
client.Scripting = &Scripting{client}
client.Channels = &Channels{client}
client.Server = &Server{client}
return client, nil
}
func (client *Client) jExecute(resp interface{}, command string, args ...string) error {
b, err := client.Execute(command, args...)
if err != nil {
return err
}
if resp != nil {
return json.Unmarshal(b, &resp)
}
return nil
}
// Execute Tile38 command.
func (client *Client) Execute(command string, args ...string) ([]byte, error) {
resp, err := client.exec.Execute(command, args...)
if client.debug {
log.Printf("[%s]: %s", newCmd(command, args...).String(), resp)
}
if err != nil {
return nil, err
}
if !gjson.GetBytes(resp, "ok").Bool() {
return nil, fmt.Errorf(gjson.GetBytes(resp, "err").String())
}
return resp, nil
}
// ExecuteStream used for Tile38 commands with streaming response.
func (client *Client) ExecuteStream(ctx context.Context, handler func([]byte) error, command string, args ...string) error {
if client.debug {
log.Printf("[%s]", newCmd(command, args...).String())
}
return client.exec.ExecuteStream(ctx, handler, command, args...)
}
// Close closes all connections in the pool and rejects future execution calls.
// Blocks until all streams are closed.
//
// NOTE: custom Executor implementation may change behavior.
func (client *Client) Close() error {
return client.exec.Close()
}