-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexecute.go
116 lines (94 loc) · 3.11 KB
/
execute.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
/*
Copyright © 2021 Robin Moffat & Contributors
Copyright © 2021 Thomas Meitz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Parts of this apiclient are borrowed from Zalando Skipper
https://github.com/zalando/skipper/blob/master/net/httpclient.go
Zalando licence: MIT
https://github.com/zalando/skipper/blob/master/LICENSE
*/
package ksqldb
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/thmeitz/ksqldb-go/internal"
"github.com/thmeitz/ksqldb-go/parser"
)
type SessionVariablesMap map[string]interface{}
type ExecOptions struct {
KSql string `json:"ksql"`
StreamsProperties PropertyMap `json:"streamsProperties,omitempty"`
SessionVariables SessionVariablesMap `json:"sessionVariables,omitempty"`
CommandSequenceNumber int64 `json:"commandSequenceNumber,omitempty"`
}
func (o *ExecOptions) SanitizeQuery() {
o.KSql = internal.SanitizeQuery(o.KSql)
}
func (o *ExecOptions) EmptyQuery() bool {
return len(o.KSql) < 1
}
// Execute will execute a ksqlDB statement.
// All statements, except those starting with SELECT,
// can be run on this endpoint.
// To run SELECT statements use use Push or Pull functions.
//
// To use this function pass in the @ExecOptions.
//
// Ref: https://docs.ksqldb.io/en/latest/developer-guide/ksqldb-rest-api/ksql-endpoint/
func (api *KsqldbClient) Execute(ctx context.Context, options ExecOptions) (response *KsqlResponseSlice, err error) {
response = new(KsqlResponseSlice)
if options.EmptyQuery() {
return nil, fmt.Errorf("empty ksql query")
}
// remove \t \n from query
options.SanitizeQuery()
if api.ParseSQLEnabled() {
ksqlerr := parser.ParseSql(options.KSql)
if ksqlerr != nil {
return nil, ksqlerr
}
}
jsonData, err := json.Marshal(options)
if err != nil {
return nil, fmt.Errorf("can't marshal input data")
}
// make the request
req, err := newKsqlRequest(ctx, api.http, bytes.NewReader(jsonData))
// api.logger.Debugf("sending ksqlDB request:%v", q)
if err != nil {
return nil, fmt.Errorf("can't create new request: %w", err)
}
res, err := api.http.Do(req)
if err != nil {
return nil, fmt.Errorf("can't do request: %w", err)
}
defer func() {
berr := res.Body.Close()
if err == nil {
err = berr
}
}()
body, err := api.readBody(res.Body)
if err != nil {
return nil, fmt.Errorf("can't read response body: %w", err)
}
// this is only one side of the coin
if res.StatusCode != http.StatusOK {
return nil, handleRequestError(res.StatusCode, body)
}
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("could not parse the response: %w\n%v", err, string(body))
}
return
}