forked from shadowspore/t38c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
133 lines (108 loc) · 3.09 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
package t38c
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/qinvent/t38c/transport"
"github.com/tidwall/gjson"
)
// 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
}
// Config is a t38c client config.
type Config struct {
// Tile38 server address.
//
// Example: localhost:9851
Address string
// Enables debug logging.
// Executed queries will be printed to stdout.
Debug bool
// Allows to perform password authorization.
Password *string
// ConnectionPoolSize sets number of connections in the pool.
// Defaults to 4.
ConnectionPoolSize int
}
// New creates a new Tile38 client.
func New(cfg Config) (*Client, error) {
radixPool, err := transport.NewRadix(cfg.Address, cfg.ConnectionPoolSize, cfg.Password)
if err != nil {
return nil, err
}
return NewWithExecutor(radixPool, cfg.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,
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
if err := client.Ping(ctx); 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(ctx context.Context, resp interface{}, command string, args ...string) error {
b, err := client.Execute(ctx, command, args...)
if err != nil {
return err
}
if resp != nil {
return json.Unmarshal(b, &resp)
}
return nil
}
// Execute Tile38 command.
func (client *Client) Execute(ctx context.Context, command string, args ...string) ([]byte, error) {
resp, err := client.exec.Execute(ctx, 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()
}