/
jrpcClient.go
280 lines (252 loc) · 9.11 KB
/
jrpcClient.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2022 Fortio Authors.
//
// 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.
// Opiniated JSON RPC / REST style library. Facilitates web JSON calls,
// using generics to serialize/deserialize any type.
package jrpc // import "fortio.org/fortio/jrpc"
// This package is a true self contained library, that doesn't rely on our logger nor other packages
// in fortio/ outside of version/ (which now also doesn't rely on logger or any other package).
// Naming is hard, we have Call, Send, Get, Fetch and FetchBytes pretty much all meaning retrieving data
// from a URL with the variants depending on whether we have something to serialize and if it's bytes
// or struct based in and out. Additionally *URL() variants are for when no additional headers or options
// are needed and the url is just a plain string. If golang supported multiple signatures it would be a single
// method name instead of 8.
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptrace"
"time"
"fortio.org/fortio/version"
)
// Client side and common code.
const (
UserAgentHeader = "User-Agent"
)
// Default timeout for Call.
var timeout = 60 * time.Second
// UserAgent is the User-Agent header used by client calls (also used in fhttp/).
var UserAgent = "fortio.org/fortio-" + version.Short()
// SetCallTimeout changes the timeout for further Call calls, returns
// the previous value (default in 60s). Value is used when a timeout
// isn't passed in the options. Note this is not thread safe,
// use Destination.Timeout for changing values outside of main/single
// thread.
func SetCallTimeout(t time.Duration) time.Duration {
previous := timeout
timeout = t
return previous
}
// FetchError is a custom error type that preserves http result code if obtained.
type FetchError struct {
Message string
// HTTP code if present, -1 for other errors.
Code int
// Original (wrapped) error if any
Err error
// Original reply payload if any
Bytes []byte
}
// Destination is the URL and optional additional headers.
type Destination struct {
URL string
// Default is nil, which means no additional headers.
Headers *http.Header
// Default is 0 which means use global timeout.
Timeout time.Duration
// Default is "" which will use POST if there is a payload and GET otherwise.
Method string
// Context or will be context.Background() if not set.
//nolint:containedctx // backward compatibility and keeping the many APIs simple as it's optional
// https://go.dev/blog/context-and-structs
Context context.Context
// ClientTrace to use if set.
ClientTrace *httptrace.ClientTrace
}
func (d *Destination) GetContext() context.Context {
if d.Context != nil {
return d.Context
}
return context.Background()
}
func (fe *FetchError) Error() string {
return fmt.Sprintf("%s, code %d: %v (raw reply: %s)", fe.Message, fe.Code, fe.Err, DebugSummary(fe.Bytes, 256))
}
func (fe *FetchError) Unwrap() error {
return fe.Err
}
// Call calls the url endpoint, POSTing a serialized as json optional payload
// (pass nil for a GET http request) and returns the result, deserializing
// json into type Q. T can be inferred so we declare Response Q first.
func Call[Q any, T any](url *Destination, payload *T) (*Q, error) {
var bytes []byte
var err error
if payload != nil {
bytes, err = json.Marshal(payload)
if err != nil {
return nil, err
}
}
return Fetch[Q](url, bytes)
}
// CallURL is Call without any options/non default headers, timeout etc and just the URL.
func CallURL[Q any, T any](url string, payload *T) (*Q, error) {
return Call[Q](NewDestination(url), payload)
}
// Get fetches and deserializes the JSON returned by the Destination into a Q struct.
// Used when there is no json payload to send. Note that Get can be a different http
// method than GET, for instance if url.Method is set to "POST".
func Get[Q any](url *Destination) (*Q, error) {
return Fetch[Q](url, []byte{})
}
// GetArray fetches and deserializes the JSON returned by the Destination into a slice of
// Q struct (ie the response is a json array).
func GetArray[Q any](url *Destination) ([]Q, error) {
slicePtr, err := Fetch[[]Q](url, []byte{})
if slicePtr == nil {
return nil, err
}
return *slicePtr, err
}
// GetURL is Get without additional options (default timeout and headers).
func GetURL[Q any](url string) (*Q, error) {
return Get[Q](NewDestination(url))
}
// Serialize serializes the object as json.
func Serialize(obj interface{}) ([]byte, error) {
return json.Marshal(obj)
}
// Deserialize deserializes json as a new object of desired type.
func Deserialize[Q any](bytes []byte) (*Q, error) {
var result Q
if len(bytes) == 0 {
// Allow empty body to be deserialized as empty object.
return &result, nil
}
err := json.Unmarshal(bytes, &result)
return &result, err // Will return zero object, not nil upon error
}
// Fetch is for cases where the payload is already serialized (or empty
// but call Get() when empty for clarity).
// Note that if you're looking for the []byte version instead of this
// generics version, it's now called FetchBytes().
func Fetch[Q any](url *Destination, bytes []byte) (*Q, error) {
code, bytes, err := Send(url, bytes) // returns -1 on other errors
if err != nil {
return nil, err
}
// 200, 201, 202 are ok
ok := (code >= http.StatusOK && code <= http.StatusAccepted)
result, err := Deserialize[Q](bytes)
if err != nil {
if ok {
return nil, err
}
return nil, &FetchError{"non ok http result and deserialization error", code, err, bytes}
}
if !ok {
// can still be "ok" for some callers, they can use the result object as it deserialized as expected.
return result, &FetchError{"non ok http result", code, nil, bytes}
}
return result, nil
}
// SetHeaderIfMissing utility function to not overwrite nor append to existing headers.
func SetHeaderIfMissing(headers http.Header, name, value string) {
if headers.Get(name) != "" {
return
}
headers.Set(name, value)
}
// Send fetches the result from url and sends optional payload as a POST, GET if missing.
// Returns the http code (if no other error before then, -1 if there are errors),
// the bytes from the reply and error if any.
func Send(dest *Destination, jsonPayload []byte) (int, []byte, error) {
curTimeout := dest.Timeout
if curTimeout == 0 {
curTimeout = timeout
}
ctx, cancel := context.WithTimeout(dest.GetContext(), curTimeout)
defer cancel()
var req *http.Request
var err error
var res []byte
method := dest.Method
if len(jsonPayload) > 0 {
if method == "" {
method = http.MethodPost
}
req, err = http.NewRequestWithContext(ctx, method, dest.URL, bytes.NewReader(jsonPayload))
} else {
if method == "" {
method = http.MethodGet
}
req, err = http.NewRequestWithContext(ctx, method, dest.URL, nil)
}
if dest.ClientTrace != nil {
req = req.WithContext(httptrace.WithClientTrace(req.Context(), dest.ClientTrace))
}
if err != nil {
return -1, res, err
}
if dest.Headers != nil {
req.Header = dest.Headers.Clone()
}
if len(jsonPayload) > 0 {
SetHeaderIfMissing(req.Header, "Content-Type", "application/json; charset=utf-8")
}
SetHeaderIfMissing(req.Header, "Accept", "application/json")
SetHeaderIfMissing(req.Header, UserAgentHeader, UserAgent)
var resp *http.Response
resp, err = http.DefaultClient.Do(req)
if err != nil {
return -1, res, err
}
res, err = io.ReadAll(resp.Body)
resp.Body.Close()
return resp.StatusCode, res, err
}
// NewDestination returns a Destination object set for the given url
// (and default/nil replacement headers and default global timeout).
func NewDestination(url string) *Destination {
return &Destination{URL: url}
}
// FetchURL is Send without a payload and no additional options (default timeout and headers).
// Technically this should be called FetchBytesURL().
func FetchURL(url string) (int, []byte, error) {
return Send(NewDestination(url), []byte{})
}
// Fetch is Send without a payload (so will be a GET request).
// Used to be called Fetch() but we needed that shorter name to
// simplify the former CallWithPayload function name.
func FetchBytes(url *Destination) (int, []byte, error) {
return Send(url, []byte{})
}
// EscapeBytes returns printable string. Same as %q format without the
// surrounding/extra "".
func EscapeBytes(buf []byte) string {
e := fmt.Sprintf("%q", buf)
return e[1 : len(e)-1]
}
// DebugSummary returns a string with the size and escaped first max/2 and
// last max/2 bytes of a buffer (or the whole escaped buffer if small enough).
func DebugSummary(buf []byte, max int) string {
l := len(buf)
if l <= max+3 { // no point in shortening to add ... if we could return those 3
return EscapeBytes(buf)
}
max /= 2
return fmt.Sprintf("%d: %s...%s", l, EscapeBytes(buf[:max]), EscapeBytes(buf[l-max:]))
}