-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.go
219 lines (184 loc) · 5 KB
/
request.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package fauna
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
type apiRequest struct {
Context context.Context
Headers map[string]string
}
func (apiReq *apiRequest) post(cli *Client, url *url.URL, bytesOut []byte) (attempts int, httpRes *http.Response, err error) {
var httpReq *http.Request
if httpReq, err = http.NewRequestWithContext(
apiReq.Context,
http.MethodPost,
url.String(),
bytes.NewReader(bytesOut),
); err != nil {
err = fmt.Errorf("failed to init request: %w", err)
return
}
httpReq.Header.Set(headerAuthorization, `Bearer `+cli.secret)
if lastTxnTs := cli.lastTxnTime.string(); lastTxnTs != "" {
httpReq.Header.Set(HeaderLastTxnTs, lastTxnTs)
}
for k, v := range apiReq.Headers {
httpReq.Header.Set(k, v)
}
if attempts, httpRes, err = cli.doWithRetry(httpReq); err != nil {
err = ErrNetwork(fmt.Errorf("network error: %w", err))
}
cli.logger.LogResponse(cli.ctx, bytesOut, httpRes)
return
}
type queryRequest struct {
apiRequest
Query any
Arguments map[string]any
}
type queryResponse struct {
Header http.Header
Data json.RawMessage `json:"data"`
Error *ErrFauna `json:"error,omitempty"`
Logging []string `json:"logging,omitempty"`
SchemaVersion int64 `json:"schema_version"`
StaticType string `json:"static_type"`
Stats *Stats `json:"stats,omitempty"`
Summary string `json:"summary"`
TxnTime int64 `json:"txn_ts"`
Tags string `json:"query_tags"`
}
func parseQueryResponse(httpRes *http.Response) (qRes *queryResponse, err error) {
var bytesIn []byte
if bytesIn, err = io.ReadAll(httpRes.Body); err != nil {
err = fmt.Errorf("failed to read response body: %w", err)
return
}
if err = json.Unmarshal(bytesIn, &qRes); err != nil {
err = fmt.Errorf("failed to unmarshal response: %w", err)
}
return
}
func (r *queryResponse) queryTags() map[string]string {
ret := map[string]string{}
if r.Tags != "" {
for _, tag := range strings.Split(r.Tags, `,`) {
tokens := strings.Split(tag, `=`)
ret[tokens[0]] = tokens[1]
}
}
return ret
}
func (qReq *queryRequest) do(cli *Client) (qSus *QuerySuccess, err error) {
var bytesOut []byte
if bytesOut, err = marshal(qReq); err != nil {
err = fmt.Errorf("marshal request failed: %w", err)
return
}
var queryURL *url.URL
if queryURL, err = cli.parseQueryURL(); err != nil {
return
}
var (
attempts int
httpRes *http.Response
)
if attempts, httpRes, err = qReq.post(cli, queryURL, bytesOut); err != nil {
return
}
var qRes *queryResponse
if qRes, err = parseQueryResponse(httpRes); err != nil {
return
}
cli.logger.LogResponse(cli.ctx, bytesOut, httpRes)
cli.lastTxnTime.sync(qRes.TxnTime)
qRes.Header = httpRes.Header
if err = getErrFauna(httpRes.StatusCode, qRes, attempts); err != nil {
return
}
var data any
if data, err = decode(qRes.Data); err != nil {
err = fmt.Errorf("failed to decode data: %w", err)
return
}
qSus = &QuerySuccess{
QueryInfo: newQueryInfo(qRes),
Data: data,
StaticType: qRes.StaticType,
}
qSus.Stats.Attempts = attempts
return
}
type streamRequest struct {
apiRequest
Stream EventSource
StartTS int64
Cursor string
}
func (streamReq *streamRequest) do(cli *Client) (bytes io.ReadCloser, err error) {
var bytesOut []byte
if bytesOut, err = marshal(streamReq); err != nil {
err = fmt.Errorf("marshal request failed: %w", err)
return
}
var streamURL *url.URL
if streamURL, err = cli.parseStreamURL(); err != nil {
return
}
var (
attempts int
httpRes *http.Response
)
if attempts, httpRes, err = streamReq.post(cli, streamURL, bytesOut); err != nil {
return
}
cli.logger.LogResponse(cli.ctx, bytesOut, httpRes)
if httpRes.StatusCode != http.StatusOK {
var qRes *queryResponse
if qRes, err = parseQueryResponse(httpRes); err == nil {
if err = getErrFauna(httpRes.StatusCode, qRes, attempts); err == nil {
err = fmt.Errorf("unknown error for http status: %d", httpRes.StatusCode)
}
}
return
}
bytes = httpRes.Body
return
}
type feedRequest struct {
apiRequest
Source EventSource
Cursor string
StartTS int64
PageSize int
}
func (feedReq *feedRequest) do(cli *Client) (io.ReadCloser, error) {
bytesOut, marshalErr := marshal(feedReq)
if marshalErr != nil {
return nil, fmt.Errorf("marshal request failed: %w", marshalErr)
}
changeFeedURL, parseURLErr := cli.parseFeedURL()
if parseURLErr != nil {
return nil, fmt.Errorf("parse url failed: %w", parseURLErr)
}
attempts, httpRes, postErr := feedReq.post(cli, changeFeedURL, bytesOut)
if postErr != nil {
return nil, fmt.Errorf("post request failed: %w", postErr)
}
if httpRes.StatusCode != http.StatusOK {
qRes, err := parseQueryResponse(httpRes)
if err == nil {
if err = getErrFauna(httpRes.StatusCode, qRes, attempts); err == nil {
err = fmt.Errorf("unknown error for http status: %d", httpRes.StatusCode)
}
}
return nil, err
}
return httpRes.Body, nil
}