-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathclient.go
More file actions
424 lines (359 loc) · 11 KB
/
client.go
File metadata and controls
424 lines (359 loc) · 11 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"bufio"
"mime/multipart"
"os"
"path/filepath"
"github.com/xdevplatform/xurl/auth"
"github.com/xdevplatform/xurl/config"
xurlErrors "github.com/xdevplatform/xurl/errors"
"github.com/xdevplatform/xurl/version"
)
// RequestOptions contains common options for API requests
type RequestOptions struct {
Method string
Endpoint string
Headers []string
Data string
AuthType string
Username string
Verbose bool
Trace bool
}
// MultipartOptions contains options specific to multipart requests
type MultipartOptions struct {
RequestOptions
FormFields map[string]string
FileField string
FilePath string
FileName string
FileData []byte
}
// Client is an interface for API clients
type Client interface {
BuildRequest(requestOptions RequestOptions) (*http.Request, error)
BuildMultipartRequest(options MultipartOptions) (*http.Request, error)
SendRequest(options RequestOptions) (json.RawMessage, error)
StreamRequest(options RequestOptions) error
SendMultipartRequest(options MultipartOptions) (json.RawMessage, error)
}
// ApiClient handles API requests
type ApiClient struct {
url string
client *http.Client
auth *auth.Auth
}
// NewApiClient creates a new ApiClient
func NewApiClient(config *config.Config, auth *auth.Auth) *ApiClient {
return &ApiClient{
url: config.APIBaseURL,
client: &http.Client{Timeout: 30 * time.Second},
auth: auth,
}
}
// BuildRequest builds an HTTP request
func (c *ApiClient) BuildRequest(requestOptions RequestOptions) (*http.Request, error) {
httpMethod := strings.ToUpper(requestOptions.Method)
var body io.Reader
contentType := ""
if requestOptions.Data != "" && (httpMethod == "POST" || httpMethod == "PUT" || httpMethod == "PATCH") {
body = bytes.NewBufferString(requestOptions.Data)
var js json.RawMessage
if json.Unmarshal([]byte(requestOptions.Data), &js) == nil {
contentType = "application/json"
} else {
contentType = "application/x-www-form-urlencoded"
}
}
return c.buildBaseRequest(
requestOptions.Method,
requestOptions.Endpoint,
body,
contentType,
requestOptions.Headers,
requestOptions.AuthType,
requestOptions.Username,
requestOptions.Trace,
)
}
// BuildMultipartRequest builds an HTTP request with multipart form data
func (c *ApiClient) BuildMultipartRequest(options MultipartOptions) (*http.Request, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// Handle file from path
if options.FileField != "" && options.FilePath != "" {
file, err := os.Open(options.FilePath)
if err != nil {
return nil, xurlErrors.NewIOError(fmt.Errorf("error opening file: %v", err))
}
defer file.Close()
part, err := writer.CreateFormFile(options.FileField, filepath.Base(options.FilePath))
if err != nil {
return nil, xurlErrors.NewIOError(fmt.Errorf("error creating form file: %v", err))
}
if _, err := io.Copy(part, file); err != nil {
return nil, xurlErrors.NewIOError(fmt.Errorf("error copying file content: %v", err))
}
} else if options.FileField != "" && len(options.FileData) > 0 { // Handle file from buffer
part, err := writer.CreateFormFile(options.FileField, options.FileName)
if err != nil {
return nil, xurlErrors.NewIOError(fmt.Errorf("error creating form file: %v", err))
}
if _, err := part.Write(options.FileData); err != nil {
return nil, xurlErrors.NewIOError(fmt.Errorf("error writing file data: %v", err))
}
}
for key, value := range options.FormFields {
if err := writer.WriteField(key, value); err != nil {
return nil, xurlErrors.NewIOError(fmt.Errorf("error writing form field: %v", err))
}
}
if err := writer.Close(); err != nil {
return nil, xurlErrors.NewIOError(fmt.Errorf("error closing multipart writer: %v", err))
}
// Use the common base request builder with the multipart content type
return c.buildBaseRequest(
options.Method,
options.Endpoint,
body,
writer.FormDataContentType(),
options.Headers,
options.AuthType,
options.Username,
options.Trace,
)
}
// SendRequest sends an HTTP request
func (c *ApiClient) SendRequest(options RequestOptions) (json.RawMessage, error) {
req, err := c.BuildRequest(options)
if err != nil {
return nil, xurlErrors.NewHTTPError(err)
}
c.logRequest(req, options.Verbose)
resp, err := c.client.Do(req)
if err != nil {
return nil, xurlErrors.NewHTTPError(err)
}
defer resp.Body.Close()
return c.processResponse(resp, options.Verbose)
}
// SendMultipartRequest sends an HTTP request with multipart form data
func (c *ApiClient) SendMultipartRequest(options MultipartOptions) (json.RawMessage, error) {
req, err := c.BuildMultipartRequest(options)
if err != nil {
return nil, err
}
c.logRequest(req, options.Verbose)
resp, err := c.client.Do(req)
if err != nil {
return nil, xurlErrors.NewHTTPError(err)
}
defer resp.Body.Close()
return c.processResponse(resp, options.Verbose)
}
// StreamRequest sends an HTTP request and streams the response
func (c *ApiClient) StreamRequest(options RequestOptions) error {
req, err := c.BuildRequest(options)
if err != nil {
return xurlErrors.NewHTTPError(err)
}
if options.Verbose {
fmt.Printf("\033[1;34m> %s\033[0m %s\n", req.Method, req.URL)
for key, values := range req.Header {
for _, value := range values {
fmt.Printf("\033[1;36m> %s\033[0m: %s\n", key, value)
}
}
fmt.Println()
}
client := &http.Client{
Timeout: 0,
}
fmt.Printf("\033[1;32mConnecting to streaming endpoint: %s\033[0m\n", options.Endpoint)
resp, err := client.Do(req)
if err != nil {
return xurlErrors.NewHTTPError(err)
}
defer resp.Body.Close()
if options.Verbose {
fmt.Printf("\033[1;31m< %s\033[0m\n", resp.Status)
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("\033[1;32m< %s\033[0m: %s\n", key, value)
}
}
fmt.Println()
}
if resp.StatusCode >= 400 {
body, err := io.ReadAll(resp.Body)
if err != nil {
return xurlErrors.NewIOError(err)
}
var js json.RawMessage
if err := json.Unmarshal(body, &js); err != nil {
return xurlErrors.NewJSONError(err)
}
return xurlErrors.NewAPIError(js)
}
scanner := bufio.NewScanner(resp.Body)
const maxScanTokenSize = 1024 * 1024
buf := make([]byte, maxScanTokenSize)
scanner.Buffer(buf, maxScanTokenSize)
fmt.Println("\033[1;32m--- Streaming response started ---\033[0m")
fmt.Println("\033[1;32m--- Press Ctrl+C to stop ---\033[0m")
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
// We can't pretty-print streaming responses
fmt.Println(line)
}
if err := scanner.Err(); err != nil {
if err == bufio.ErrTooLong {
return xurlErrors.NewIOError(fmt.Errorf("line too long: increase buffer size"))
}
return xurlErrors.NewIOError(err)
}
fmt.Println("\033[1;32m--- End of stream ---\033[0m")
return nil
}
// buildBaseRequest creates the base HTTP request with common headers and settings
func (c *ApiClient) buildBaseRequest(method, endpoint string, body io.Reader, contentType string, headers []string, authType, username string, trace bool) (*http.Request, error) {
httpMethod := strings.ToUpper(method)
// Build the full URL
url := endpoint
if !strings.HasPrefix(strings.ToLower(endpoint), "http") {
url = c.url
if !strings.HasSuffix(url, "/") {
url += "/"
}
if strings.HasPrefix(endpoint, "/") {
url += endpoint[1:]
} else {
url += endpoint
}
}
// Create the request
req, err := http.NewRequest(httpMethod, url, body)
if err != nil {
return nil, xurlErrors.NewHTTPError(err)
}
// Add headers
for _, header := range headers {
parts := strings.SplitN(header, ":", 2)
if len(parts) == 2 {
req.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
}
}
// Set content type if provided
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
// Add authorization header if not already set
if req.Header.Get("Authorization") == "" {
authHeader, err := c.getAuthHeader(httpMethod, url, authType, username)
if err == nil {
req.Header.Add("Authorization", authHeader)
}
}
// Add common headers
req.Header.Add("User-Agent", "xurl/"+version.Version)
if trace {
req.Header.Add("X-B3-Flags", "1")
}
return req, nil
}
// GetAuthHeader gets the authorization header for a request
func (c *ApiClient) getAuthHeader(method, url string, authType string, username string) (string, error) {
if c.auth == nil {
return "", xurlErrors.NewAuthError("AuthNotSet", errors.New("auth not set"))
}
if authType != "" {
switch strings.ToLower(authType) {
case "oauth1":
return c.auth.GetOAuth1Header(method, url, nil)
case "oauth2":
return c.auth.GetOAuth2Header(username)
case "app":
return c.auth.GetBearerTokenHeader()
default:
return "", xurlErrors.NewAuthError("InvalidAuthType", fmt.Errorf("invalid auth type: %s", authType))
}
}
// If no auth type is specified, try to use the first OAuth2 token
// Use ForApp variants so the active app name (set via --app) is respected.
token := c.auth.TokenStore.GetFirstOAuth2TokenForApp(c.auth.AppName())
if token != nil {
accessToken, err := c.auth.GetOAuth2Header(username)
if err == nil {
return accessToken, nil
}
}
// If no OAuth2 token is available, try to use the first OAuth1 token
token = c.auth.TokenStore.GetOAuth1TokensForApp(c.auth.AppName())
if token != nil {
authHeader, err := c.auth.GetOAuth1Header(method, url, nil)
if err == nil {
return authHeader, nil
}
}
// If no OAuth1 token is available, try to use the bearer token
bearerToken, err := c.auth.GetBearerTokenHeader()
if err == nil {
return bearerToken, nil
}
// If no authentication method is available, return an error
return "", xurlErrors.NewAuthError("NoAuthMethod", errors.New("no authentication method available"))
}
// logRequest logs request details if verbose mode is enabled
func (c *ApiClient) logRequest(req *http.Request, verbose bool) {
if verbose {
fmt.Printf("\033[1;34m> %s\033[0m %s\n", req.Method, req.URL)
for key, values := range req.Header {
for _, value := range values {
fmt.Printf("\033[1;36m> %s\033[0m: %s\n", key, value)
}
}
fmt.Println()
}
}
// processResponse handles common response processing logic
func (c *ApiClient) processResponse(resp *http.Response, verbose bool) (json.RawMessage, error) {
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, xurlErrors.NewIOError(err)
}
if verbose {
fmt.Printf("\033[1;31m< %s\033[0m\n", resp.Status)
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("\033[1;32m< %s\033[0m: %s\n", key, value)
}
}
fmt.Println()
}
var js json.RawMessage
if len(responseBody) > 0 {
if err := json.Unmarshal(responseBody, &js); err != nil {
if resp.StatusCode >= 400 {
return nil, xurlErrors.NewHTTPError(fmt.Errorf("HTTP error: %s", resp.Status))
}
js = json.RawMessage("{}")
}
} else {
js = json.RawMessage("{}")
}
if resp.StatusCode >= 400 {
return nil, xurlErrors.NewAPIError(js)
}
return js, nil
}