-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathclient.go
More file actions
835 lines (692 loc) · 22.4 KB
/
Copy pathclient.go
File metadata and controls
835 lines (692 loc) · 22.4 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
package client
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/xml"
"errors"
"io"
"os"
"path/filepath"
"sync"
"time"
"github.com/fxamacker/cbor/v2"
"github.com/gofiber/fiber/v3/log"
utils "github.com/gofiber/utils/v2"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
)
var ErrFailedToAppendCert = errors.New("failed to append certificate")
// Client is used to create a Fiber client with client-level settings that
// apply to all requests made by the client.
//
// The Fiber client also provides an option to override or merge most of the
// client settings at the request level.
type Client struct {
logger log.CommonLogger
transport httpClientTransport
header *Header
params *QueryParam
cookies *Cookie
path *PathParam
jsonMarshal utils.JSONMarshal
jsonUnmarshal utils.JSONUnmarshal
xmlMarshal utils.XMLMarshal
xmlUnmarshal utils.XMLUnmarshal
cborMarshal utils.CBORMarshal
cborUnmarshal utils.CBORUnmarshal
cookieJar *CookieJar
retryConfig *RetryConfig
baseURL string
userAgent string
referer string
userRequestHooks []RequestHook
builtinRequestHooks []RequestHook
userResponseHooks []ResponseHook
builtinResponseHooks []ResponseHook
timeout time.Duration
mu sync.RWMutex
debug bool
disablePathNormalizing bool
}
// Do executes the request using the underlying fasthttp transport.
//
// It mirrors [fasthttp.Client.Do], [fasthttp.HostClient.Do], or
// [fasthttp.LBClient.Do] depending on how the Fiber client was constructed.
func (c *Client) Do(req *fasthttp.Request, resp *fasthttp.Response) error {
return c.transport.Do(req, resp)
}
// DoTimeout executes the request and waits for a response up to the provided timeout.
// It mirrors the behavior of the respective fasthttp client's DoTimeout implementation.
func (c *Client) DoTimeout(req *fasthttp.Request, resp *fasthttp.Response, timeout time.Duration) error {
return c.transport.DoTimeout(req, resp, timeout)
}
// DoDeadline executes the request and waits for a response until the provided deadline.
// It mirrors the behavior of the respective fasthttp client's DoDeadline implementation.
func (c *Client) DoDeadline(req *fasthttp.Request, resp *fasthttp.Response, deadline time.Time) error {
return c.transport.DoDeadline(req, resp, deadline)
}
// DoRedirects executes the request following redirects up to maxRedirects.
func (c *Client) DoRedirects(req *fasthttp.Request, resp *fasthttp.Response, maxRedirects int) error {
return c.transport.DoRedirects(req, resp, maxRedirects)
}
// CloseIdleConnections closes idle connections on the underlying fasthttp transport when supported.
func (c *Client) CloseIdleConnections() {
c.transport.CloseIdleConnections()
}
func (c *Client) currentTLSConfig() *tls.Config {
return c.transport.TLSConfig()
}
func (c *Client) applyTLSConfig(config *tls.Config) {
c.transport.SetTLSConfig(config)
}
func (c *Client) applyDial(dial fasthttp.DialFunc) {
c.transport.SetDial(dial)
}
// FasthttpClient returns the underlying *fasthttp.Client if the client was created with one.
func (c *Client) FasthttpClient() *fasthttp.Client {
if client, ok := c.transport.(*standardClientTransport); ok {
return client.client
}
return nil
}
// HostClient returns the underlying fasthttp.HostClient if the client was created with one.
func (c *Client) HostClient() *fasthttp.HostClient {
if client, ok := c.transport.(*hostClientTransport); ok {
return client.client
}
return nil
}
// LBClient returns the underlying fasthttp.LBClient if the client was created with one.
func (c *Client) LBClient() *fasthttp.LBClient {
if client, ok := c.transport.(*lbClientTransport); ok {
return client.client
}
return nil
}
// R creates a new Request associated with the client.
func (c *Client) R() *Request {
return AcquireRequest().SetClient(c)
}
// RequestHook returns the user-defined request hooks.
func (c *Client) RequestHook() []RequestHook {
return c.userRequestHooks
}
// AddRequestHook adds user-defined request hooks.
func (c *Client) AddRequestHook(h ...RequestHook) *Client {
c.mu.Lock()
defer c.mu.Unlock()
c.userRequestHooks = append(c.userRequestHooks, h...)
return c
}
// ResponseHook returns the user-defined response hooks.
func (c *Client) ResponseHook() []ResponseHook {
return c.userResponseHooks
}
// AddResponseHook adds user-defined response hooks.
func (c *Client) AddResponseHook(h ...ResponseHook) *Client {
c.mu.Lock()
defer c.mu.Unlock()
c.userResponseHooks = append(c.userResponseHooks, h...)
return c
}
// JSONMarshal returns the JSON marshal function used by the client.
func (c *Client) JSONMarshal() utils.JSONMarshal {
return c.jsonMarshal
}
// SetJSONMarshal sets the JSON marshal function to use.
func (c *Client) SetJSONMarshal(f utils.JSONMarshal) *Client {
c.jsonMarshal = f
return c
}
// JSONUnmarshal returns the JSON unmarshal function used by the client.
func (c *Client) JSONUnmarshal() utils.JSONUnmarshal {
return c.jsonUnmarshal
}
// SetJSONUnmarshal sets the JSON unmarshal function to use.
func (c *Client) SetJSONUnmarshal(f utils.JSONUnmarshal) *Client {
c.jsonUnmarshal = f
return c
}
// XMLMarshal returns the XML marshal function used by the client.
func (c *Client) XMLMarshal() utils.XMLMarshal {
return c.xmlMarshal
}
// SetXMLMarshal sets the XML marshal function to use.
func (c *Client) SetXMLMarshal(f utils.XMLMarshal) *Client {
c.xmlMarshal = f
return c
}
// XMLUnmarshal returns the XML unmarshal function used by the client.
func (c *Client) XMLUnmarshal() utils.XMLUnmarshal {
return c.xmlUnmarshal
}
// SetXMLUnmarshal sets the XML unmarshal function to use.
func (c *Client) SetXMLUnmarshal(f utils.XMLUnmarshal) *Client {
c.xmlUnmarshal = f
return c
}
// CBORMarshal returns the CBOR marshal function used by the client.
func (c *Client) CBORMarshal() utils.CBORMarshal {
return c.cborMarshal
}
// SetCBORMarshal sets the CBOR marshal function to use.
func (c *Client) SetCBORMarshal(f utils.CBORMarshal) *Client {
c.cborMarshal = f
return c
}
// CBORUnmarshal returns the CBOR unmarshal function used by the client.
func (c *Client) CBORUnmarshal() utils.CBORUnmarshal {
return c.cborUnmarshal
}
// SetCBORUnmarshal sets the CBOR unmarshal function to use.
func (c *Client) SetCBORUnmarshal(f utils.CBORUnmarshal) *Client {
c.cborUnmarshal = f
return c
}
// TLSConfig returns the client's TLS configuration.
// If none is set, it initializes a new one.
func (c *Client) TLSConfig() *tls.Config {
c.mu.Lock()
defer c.mu.Unlock()
if cfg := c.currentTLSConfig(); cfg != nil {
return cfg
}
cfg := &tls.Config{MinVersion: tls.VersionTLS12}
c.applyTLSConfig(cfg)
return cfg
}
// SetTLSConfig sets the TLS configuration for the client.
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.mu.Lock()
defer c.mu.Unlock()
c.applyTLSConfig(config)
return c
}
// SetCertificates adds certificates to the client's TLS configuration.
func (c *Client) SetCertificates(certs ...tls.Certificate) *Client {
config := c.TLSConfig()
config.Certificates = append(config.Certificates, certs...)
return c
}
// SetRootCertificate adds one or more root certificates to the client's TLS configuration.
func (c *Client) SetRootCertificate(path string) *Client {
cleanPath := filepath.Clean(path)
file, err := os.Open(cleanPath)
if err != nil {
c.logger.Panicf("client: %v", err)
}
defer func() {
if closeErr := file.Close(); closeErr != nil {
c.logger.Panicf("client: failed to close file: %v", closeErr)
}
}()
pem, err := io.ReadAll(file)
if err != nil {
c.logger.Panicf("client: %v", err)
}
config := c.TLSConfig()
if config.RootCAs == nil {
config.RootCAs = x509.NewCertPool()
}
if !config.RootCAs.AppendCertsFromPEM(pem) {
c.logger.Panicf("client: %v", ErrFailedToAppendCert)
}
return c
}
// SetRootCertificateFromString adds one or more root certificates from a string to the client's TLS configuration.
func (c *Client) SetRootCertificateFromString(pem string) *Client {
config := c.TLSConfig()
if config.RootCAs == nil {
config.RootCAs = x509.NewCertPool()
}
if !config.RootCAs.AppendCertsFromPEM([]byte(pem)) {
c.logger.Panicf("client: %v", ErrFailedToAppendCert)
}
return c
}
// SetProxyURL sets the proxy URL for the client. This affects all subsequent requests.
func (c *Client) SetProxyURL(proxyURL string) error {
c.mu.Lock()
defer c.mu.Unlock()
c.applyDial(fasthttpproxy.FasthttpHTTPDialer(proxyURL))
return nil
}
// RetryConfig returns the current retry configuration.
func (c *Client) RetryConfig() *RetryConfig {
return c.retryConfig
}
// SetRetryConfig sets the retry configuration for the client.
func (c *Client) SetRetryConfig(config *RetryConfig) *Client {
c.mu.Lock()
defer c.mu.Unlock()
c.retryConfig = config
return c
}
// BaseURL returns the client's base URL.
func (c *Client) BaseURL() string {
return c.baseURL
}
// SetBaseURL sets the base URL prefix for all requests made by the client.
func (c *Client) SetBaseURL(url string) *Client {
c.baseURL = url
return c
}
// Header returns all header values associated with the provided key.
func (c *Client) Header(key string) []string {
return c.header.PeekMultiple(key)
}
// AddHeader adds a single header field and its value to the client. These headers apply to all requests.
func (c *Client) AddHeader(key, val string) *Client {
c.header.Add(key, val)
return c
}
// SetHeader sets a single header field and its value in the client.
func (c *Client) SetHeader(key, val string) *Client {
c.header.Set(key, val)
return c
}
// AddHeaders adds multiple header fields and their values to the client.
func (c *Client) AddHeaders(h map[string][]string) *Client {
c.header.AddHeaders(h)
return c
}
// SetHeaders method sets multiple headers field and its values at one go in the client instance.
// These headers will be applied to all requests created from this client instance. Also it can be
// overridden at request level headers options.
func (c *Client) SetHeaders(h map[string]string) *Client {
c.header.SetHeaders(h)
return c
}
// Param returns all values of the specified query parameter.
func (c *Client) Param(key string) []string {
res := []string{}
tmp := c.params.PeekMulti(key)
for _, v := range tmp {
res = append(res, utils.UnsafeString(v))
}
return res
}
// AddParam adds a single query parameter and its value to the client.
// These params will be applied to all requests created from this client instance.
func (c *Client) AddParam(key, val string) *Client {
c.params.Add(key, val)
return c
}
// SetParam sets a single query parameter and its value in the client.
func (c *Client) SetParam(key, val string) *Client {
c.params.Set(key, val)
return c
}
// AddParams adds multiple query parameters and their values to the client.
func (c *Client) AddParams(m map[string][]string) *Client {
c.params.AddParams(m)
return c
}
// SetParams sets multiple query parameters and their values in the client.
func (c *Client) SetParams(m map[string]string) *Client {
c.params.SetParams(m)
return c
}
// SetParamsWithStruct sets multiple query parameters and their values using a struct.
func (c *Client) SetParamsWithStruct(v any) *Client {
c.params.SetParamsWithStruct(v)
return c
}
// DelParams deletes one or more query parameters and their values from the client.
func (c *Client) DelParams(key ...string) *Client {
for _, v := range key {
c.params.Del(v)
}
return c
}
// SetUserAgent sets the User-Agent header for the client.
func (c *Client) SetUserAgent(ua string) *Client {
c.userAgent = ua
return c
}
// SetReferer sets the Referer header for the client.
func (c *Client) SetReferer(r string) *Client {
c.referer = r
return c
}
// DisablePathNormalizing reports whether path normalizing is disabled for the client.
func (c *Client) DisablePathNormalizing() bool {
return c.disablePathNormalizing
}
// SetDisablePathNormalizing configures the client to disable or enable path normalizing.
func (c *Client) SetDisablePathNormalizing(disable bool) *Client {
c.disablePathNormalizing = disable
return c
}
// PathParam returns the value of the specified path parameter. Returns an empty string if it does not exist.
func (c *Client) PathParam(key string) string {
if val, ok := (*c.path)[key]; ok {
return val
}
return ""
}
// SetPathParam sets a single path parameter and its value in the client.
func (c *Client) SetPathParam(key, val string) *Client {
c.path.SetParam(key, val)
return c
}
// SetPathParams sets multiple path parameters and their values in the client.
func (c *Client) SetPathParams(m map[string]string) *Client {
c.path.SetParams(m)
return c
}
// SetPathParamsWithStruct sets multiple path parameters and their values using a struct.
func (c *Client) SetPathParamsWithStruct(v any) *Client {
c.path.SetParamsWithStruct(v)
return c
}
// DelPathParams deletes one or more path parameters and their values from the client.
func (c *Client) DelPathParams(key ...string) *Client {
c.path.DelParams(key...)
return c
}
// Cookie returns the value of the specified cookie. Returns an empty string if it does not exist.
func (c *Client) Cookie(key string) string {
if val, ok := (*c.cookies)[key]; ok {
return val
}
return ""
}
// SetCookie sets a single cookie and its value in the client.
func (c *Client) SetCookie(key, val string) *Client {
c.cookies.SetCookie(key, val)
return c
}
// SetCookies sets multiple cookies and their values in the client.
func (c *Client) SetCookies(m map[string]string) *Client {
c.cookies.SetCookies(m)
return c
}
// SetCookiesWithStruct sets multiple cookies and their values using a struct.
func (c *Client) SetCookiesWithStruct(v any) *Client {
c.cookies.SetCookiesWithStruct(v)
return c
}
// DelCookies deletes one or more cookies and their values from the client.
func (c *Client) DelCookies(key ...string) *Client {
c.cookies.DelCookies(key...)
return c
}
// SetTimeout sets the timeout value for the client. This applies to all requests unless overridden at the request level.
func (c *Client) SetTimeout(t time.Duration) *Client {
c.timeout = t
return c
}
// Debug enables debug-level logging output.
func (c *Client) Debug() *Client {
c.debug = true
return c
}
// DisableDebug disables debug-level logging output.
func (c *Client) DisableDebug() *Client {
c.debug = false
return c
}
// SetCookieJar sets the cookie jar for the client.
func (c *Client) SetCookieJar(cookieJar *CookieJar) *Client {
c.cookieJar = cookieJar
return c
}
// Get sends a GET request to the specified URL, similar to axios.
func (c *Client) Get(url string, cfg ...Config) (*Response, error) {
req := AcquireRequest().SetClient(c)
setConfigToRequest(req, cfg...)
return req.Get(url)
}
// Post sends a POST request to the specified URL, similar to axios.
func (c *Client) Post(url string, cfg ...Config) (*Response, error) {
req := AcquireRequest().SetClient(c)
setConfigToRequest(req, cfg...)
return req.Post(url)
}
// Head sends a HEAD request to the specified URL, similar to axios.
func (c *Client) Head(url string, cfg ...Config) (*Response, error) {
req := AcquireRequest().SetClient(c)
setConfigToRequest(req, cfg...)
return req.Head(url)
}
// Put sends a PUT request to the specified URL, similar to axios.
func (c *Client) Put(url string, cfg ...Config) (*Response, error) {
req := AcquireRequest().SetClient(c)
setConfigToRequest(req, cfg...)
return req.Put(url)
}
// Delete sends a DELETE request to the specified URL, similar to axios.
func (c *Client) Delete(url string, cfg ...Config) (*Response, error) {
req := AcquireRequest().SetClient(c)
setConfigToRequest(req, cfg...)
return req.Delete(url)
}
// Options sends an OPTIONS request to the specified URL, similar to axios.
func (c *Client) Options(url string, cfg ...Config) (*Response, error) {
req := AcquireRequest().SetClient(c)
setConfigToRequest(req, cfg...)
return req.Options(url)
}
// Patch sends a PATCH request to the specified URL, similar to axios.
func (c *Client) Patch(url string, cfg ...Config) (*Response, error) {
req := AcquireRequest().SetClient(c)
setConfigToRequest(req, cfg...)
return req.Patch(url)
}
// Custom sends a request with a custom method to the specified URL, similar to axios.
func (c *Client) Custom(url, method string, cfg ...Config) (*Response, error) {
req := AcquireRequest().SetClient(c)
setConfigToRequest(req, cfg...)
return req.Custom(url, method)
}
// SetDial sets the custom dial function for the client.
func (c *Client) SetDial(dial fasthttp.DialFunc) *Client {
c.mu.Lock()
defer c.mu.Unlock()
c.applyDial(dial)
return c
}
// SetLogger sets the logger instance used by the client.
func (c *Client) SetLogger(logger log.CommonLogger) *Client {
c.mu.Lock()
defer c.mu.Unlock()
c.logger = logger
return c
}
// Logger returns the logger instance used by the client.
func (c *Client) Logger() log.CommonLogger {
return c.logger
}
// Reset resets the client to its default state, clearing most configurations.
func (c *Client) Reset() {
c.transport = newStandardClientTransport(&fasthttp.Client{})
c.baseURL = ""
c.timeout = 0
c.userAgent = ""
c.referer = ""
c.retryConfig = nil
c.debug = false
c.disablePathNormalizing = false
if c.cookieJar != nil {
c.cookieJar.Release()
c.cookieJar = nil
}
c.path.Reset()
c.cookies.Reset()
c.header.Reset()
c.params.Reset()
}
// Config is used to easily set request parameters. Note that when setting a request body,
// JSON is used as the default serialization mechanism. The priority is:
// Body > FormData > File.
type Config struct {
Ctx context.Context //nolint:containedctx // It's needed to be stored in the config.
Body any
Header map[string]string
Param map[string]string
Cookie map[string]string
PathParam map[string]string
FormData map[string]string
UserAgent string
Referer string
File []*File
Timeout time.Duration
MaxRedirects int
DisablePathNormalizing bool
}
// setConfigToRequest sets the parameters passed via Config to the Request.
func setConfigToRequest(req *Request, config ...Config) {
if len(config) == 0 {
return
}
cfg := config[0]
if cfg.Ctx != nil {
req.SetContext(cfg.Ctx)
}
if cfg.UserAgent != "" {
req.SetUserAgent(cfg.UserAgent)
}
if cfg.Referer != "" {
req.SetReferer(cfg.Referer)
}
if cfg.Header != nil {
req.SetHeaders(cfg.Header)
}
if cfg.Param != nil {
req.SetParams(cfg.Param)
}
if cfg.Cookie != nil {
req.SetCookies(cfg.Cookie)
}
if cfg.PathParam != nil {
req.SetPathParams(cfg.PathParam)
}
if cfg.Timeout != 0 {
req.SetTimeout(cfg.Timeout)
}
if cfg.MaxRedirects != 0 {
req.SetMaxRedirects(cfg.MaxRedirects)
}
if cfg.DisablePathNormalizing {
req.SetDisablePathNormalizing(true)
}
if cfg.Body != nil {
req.SetJSON(cfg.Body)
return
}
if cfg.FormData != nil {
req.SetFormDataWithMap(cfg.FormData)
return
}
if len(cfg.File) != 0 {
req.AddFiles(cfg.File...)
return
}
}
var (
defaultClient *Client
replaceMu = sync.Mutex{}
defaultUserAgent = "fiber"
)
func init() {
defaultClient = New()
}
// New creates and returns a new Client object.
func New() *Client {
// Follow-up performance optimizations:
// Try to use a pool to reduce the memory allocation cost for the Fiber client and the fasthttp client.
// If possible, also consider pooling other structs (e.g., request headers, cookies, query parameters, path parameters).
return NewWithClient(&fasthttp.Client{})
}
// NewWithClient creates and returns a new Client object from an existing fasthttp.Client.
func NewWithClient(c *fasthttp.Client) *Client {
if c == nil {
panic("fasthttp.Client must not be nil")
}
return newClient(newStandardClientTransport(c))
}
// NewWithHostClient creates and returns a new Client object from an existing fasthttp.HostClient.
func NewWithHostClient(c *fasthttp.HostClient) *Client {
if c == nil {
panic("fasthttp.HostClient must not be nil")
}
return newClient(newHostClientTransport(c))
}
// NewWithLBClient creates and returns a new Client object from an existing fasthttp.LBClient.
func NewWithLBClient(c *fasthttp.LBClient) *Client {
if c == nil {
panic("fasthttp.LBClient must not be nil")
}
return newClient(newLBClientTransport(c))
}
func newClient(transport httpClientTransport) *Client {
return &Client{
transport: transport,
header: &Header{
RequestHeader: &fasthttp.RequestHeader{},
},
params: &QueryParam{
Args: fasthttp.AcquireArgs(),
},
cookies: &Cookie{},
path: &PathParam{},
userRequestHooks: []RequestHook{},
builtinRequestHooks: []RequestHook{parserRequestURL, parserRequestHeader, parserRequestBody},
userResponseHooks: []ResponseHook{},
builtinResponseHooks: []ResponseHook{parserResponseCookie, logger},
jsonMarshal: json.Marshal,
jsonUnmarshal: json.Unmarshal,
xmlMarshal: xml.Marshal,
cborMarshal: cbor.Marshal,
cborUnmarshal: cbor.Unmarshal,
xmlUnmarshal: xml.Unmarshal,
logger: log.DefaultLogger[*log.Logger](),
}
}
// C returns the default client.
func C() *Client {
return defaultClient
}
// Replace replaces the defaultClient with a new one, returning a function to restore the old client.
func Replace(c *Client) func() {
replaceMu.Lock()
defer replaceMu.Unlock()
oldClient := defaultClient
defaultClient = c
return func() {
replaceMu.Lock()
defer replaceMu.Unlock()
defaultClient = oldClient
}
}
// Get sends a GET request using the default client.
func Get(url string, cfg ...Config) (*Response, error) {
return C().Get(url, cfg...)
}
// Post sends a POST request using the default client.
func Post(url string, cfg ...Config) (*Response, error) {
return C().Post(url, cfg...)
}
// Head sends a HEAD request using the default client.
func Head(url string, cfg ...Config) (*Response, error) {
return C().Head(url, cfg...)
}
// Put sends a PUT request using the default client.
func Put(url string, cfg ...Config) (*Response, error) {
return C().Put(url, cfg...)
}
// Delete sends a DELETE request using the default client.
func Delete(url string, cfg ...Config) (*Response, error) {
return C().Delete(url, cfg...)
}
// Options sends an OPTIONS request using the default client.
func Options(url string, cfg ...Config) (*Response, error) {
return C().Options(url, cfg...)
}
// Patch sends a PATCH request using the default client.
func Patch(url string, cfg ...Config) (*Response, error) {
return C().Patch(url, cfg...)
}