@@ -20,31 +20,56 @@ import (
20
20
"github.com/pkg/errors"
21
21
)
22
22
23
+ var (
24
+ DefaultDialTimeout = time .Second * 10
25
+ DefaultRequestTimeout = time .Second * 20
26
+ )
27
+
23
28
// HTTPClient represents a client for a paymail/bsvalias service that uses HTTP for requests.
24
29
type HTTPClient struct {
25
30
Handle string
26
31
Site Site
27
32
Alias string
28
33
Hostname string
34
+
35
+ DialTimeout , RequestTimeout time.Duration
29
36
}
30
37
31
38
// HTTPFactory is a factory for creating HTTP clients.
32
- type HTTPFactory struct {}
39
+ type HTTPFactory struct {
40
+ DialTimeout , RequestTimeout time.Duration
41
+ }
33
42
34
43
// NewHTTPFactory creates a new HTTP factory.
35
44
func NewHTTPFactory () * HTTPFactory {
36
- return & HTTPFactory {}
45
+ return & HTTPFactory {
46
+ DialTimeout : DefaultDialTimeout ,
47
+ RequestTimeout : DefaultRequestTimeout ,
48
+ }
49
+ }
50
+
51
+ func (f * HTTPFactory ) SetTimeouts (dialTimeout , requestTimeout time.Duration ) {
52
+ f .DialTimeout = dialTimeout
53
+ f .RequestTimeout = requestTimeout
37
54
}
38
55
39
56
// NewClient creates a new client.
40
57
func (f * HTTPFactory ) NewClient (ctx context.Context , handle string ) (Client , error ) {
41
- return NewHTTPClient (ctx , handle )
58
+ c , err := NewHTTPClient (ctx , handle )
59
+ if err != nil {
60
+ return nil , err
61
+ }
62
+
63
+ c .SetTimeouts (f .DialTimeout , f .RequestTimeout )
64
+ return c , nil
42
65
}
43
66
44
67
// NewHTTPClient creates a new HTTPClient.
45
68
func NewHTTPClient (ctx context.Context , handle string ) (* HTTPClient , error ) {
46
69
result := HTTPClient {
47
- Handle : handle ,
70
+ DialTimeout : DefaultDialTimeout ,
71
+ RequestTimeout : DefaultRequestTimeout ,
72
+ Handle : handle ,
48
73
}
49
74
50
75
fields := strings .Split (handle , "@" )
@@ -64,6 +89,11 @@ func NewHTTPClient(ctx context.Context, handle string) (*HTTPClient, error) {
64
89
return & result , nil
65
90
}
66
91
92
+ func (c * HTTPClient ) SetTimeouts (dialTimeout , requestTimeout time.Duration ) {
93
+ c .DialTimeout = dialTimeout
94
+ c .RequestTimeout = requestTimeout
95
+ }
96
+
67
97
func (c * HTTPClient ) IsCapable (url string ) (bool , error ) {
68
98
if _ , err := c .Site .Capabilities .GetURL (url ); err != nil {
69
99
if errors .Cause (err ) == ErrNotCapable {
@@ -87,7 +117,7 @@ func (c *HTTPClient) GetPublicKey(ctx context.Context) (*bitcoin.PublicKey, erro
87
117
url = strings .ReplaceAll (url , "{domain.tld}" , c .Hostname )
88
118
89
119
var response PublicKeyResponse
90
- if err := get (ctx , url , & response ); err != nil {
120
+ if err := get (ctx , c . DialTimeout , c . RequestTimeout , url , & response ); err != nil {
91
121
return nil , errors .Wrap (err , "http get" )
92
122
}
93
123
@@ -137,7 +167,7 @@ func (c *HTTPClient) GetPaymentDestination(ctx context.Context, senderName, send
137
167
url = strings .ReplaceAll (url , "{domain.tld}" , c .Hostname )
138
168
139
169
var response PaymentDestinationResponse
140
- if err := post (ctx , url , request , & response ); err != nil {
170
+ if err := post (ctx , c . DialTimeout , c . RequestTimeout , url , request , & response ); err != nil {
141
171
return nil , errors .Wrap (err , "http post" )
142
172
}
143
173
@@ -189,7 +219,7 @@ func (c *HTTPClient) GetPaymentRequest(ctx context.Context, senderName, senderHa
189
219
url = strings .ReplaceAll (url , "{domain.tld}" , c .Hostname )
190
220
191
221
var response PaymentRequestResponse
192
- if err := post (ctx , url , request , & response ); err != nil {
222
+ if err := post (ctx , c . DialTimeout , c . RequestTimeout , url , request , & response ); err != nil {
193
223
return nil , errors .Wrap (err , "http post" )
194
224
}
195
225
@@ -243,7 +273,7 @@ func (c *HTTPClient) GetP2PPaymentDestination(ctx context.Context,
243
273
url = strings .ReplaceAll (url , "{domain.tld}" , c .Hostname )
244
274
245
275
var response P2PPaymentDestinationResponse
246
- if err := post (ctx , url , request , & response ); err != nil {
276
+ if err := post (ctx , c . DialTimeout , c . RequestTimeout , url , request , & response ); err != nil {
247
277
return nil , errors .Wrap (err , "http post" )
248
278
}
249
279
@@ -299,7 +329,7 @@ func (c *HTTPClient) PostP2PTransaction(ctx context.Context, senderHandle, note,
299
329
url = strings .ReplaceAll (url , "{domain.tld}" , c .Hostname )
300
330
301
331
var response P2PTransactionResponse
302
- if err := post (ctx , url , request , & response ); err != nil {
332
+ if err := post (ctx , c . DialTimeout , c . RequestTimeout , url , request , & response ); err != nil {
303
333
return "" , errors .Wrap (err , "http post" )
304
334
}
305
335
@@ -321,7 +351,7 @@ func (c *HTTPClient) ListTokenizedInstruments(ctx context.Context) ([]Instrument
321
351
url = strings .ReplaceAll (url , "{domain.tld}" , c .Hostname )
322
352
323
353
var response InstrumentAliasListResponse
324
- if err := get (ctx , url , & response ); err != nil {
354
+ if err := get (ctx , c . DialTimeout , c . RequestTimeout , url , & response ); err != nil {
325
355
return nil , errors .Wrap (err , "http get" )
326
356
}
327
357
@@ -339,7 +369,7 @@ func (c *HTTPClient) GetPublicProfile(ctx context.Context) (*PublicProfile, erro
339
369
url = strings .ReplaceAll (url , "{domain.tld}" , c .Hostname )
340
370
341
371
response := & PublicProfile {}
342
- if err := get (ctx , url , response ); err != nil {
372
+ if err := get (ctx , c . DialTimeout , c . RequestTimeout , url , response ); err != nil {
343
373
return nil , errors .Wrap (err , "http get" )
344
374
}
345
375
@@ -358,7 +388,7 @@ func (c *HTTPClient) PostNegotiationTx(ctx context.Context,
358
388
url = strings .ReplaceAll (url , "{alias}" , c .Alias )
359
389
url = strings .ReplaceAll (url , "{domain.tld}" , c .Hostname )
360
390
361
- status , body , err := postRaw (ctx , url , tx )
391
+ status , body , err := postRaw (ctx , c . DialTimeout , c . RequestTimeout , url , tx )
362
392
if err != nil {
363
393
return nil , errors .Wrap (err , "http post" )
364
394
}
@@ -402,23 +432,25 @@ func (c *HTTPClient) PostMerkleProofs(ctx context.Context, merkleProofs MerklePr
402
432
url = strings .ReplaceAll (url , "{alias}" , c .Alias )
403
433
url = strings .ReplaceAll (url , "{domain.tld}" , c .Hostname )
404
434
405
- if err := post (ctx , url , merkleProofs , nil ); err != nil {
435
+ if err := post (ctx , c . DialTimeout , c . RequestTimeout , url , merkleProofs , nil ); err != nil {
406
436
return errors .Wrap (err , "http post" )
407
437
}
408
438
409
439
return nil
410
440
}
411
441
412
- func postRaw (ctx context.Context , url string , request interface {}) (int , io.ReadCloser , error ) {
442
+ func postRaw (ctx context.Context , dialTimeout , requestTimeout time.Duration , url string ,
443
+ request interface {}) (int , io.ReadCloser , error ) {
444
+
413
445
var transport = & http.Transport {
414
446
Dial : (& net.Dialer {
415
- Timeout : 5 * time . Second ,
447
+ Timeout : dialTimeout ,
416
448
}).Dial ,
417
- TLSHandshakeTimeout : 5 * time . Second ,
449
+ TLSHandshakeTimeout : dialTimeout ,
418
450
}
419
451
420
452
var client = & http.Client {
421
- Timeout : time . Second * 10 ,
453
+ Timeout : requestTimeout ,
422
454
Transport : transport ,
423
455
}
424
456
@@ -436,16 +468,18 @@ func postRaw(ctx context.Context, url string, request interface{}) (int, io.Read
436
468
}
437
469
438
470
// post sends a request to the HTTP server using the POST method.
439
- func post (ctx context.Context , url string , request , response interface {}) error {
471
+ func post (ctx context.Context , dialTimeout , requestTimeout time.Duration , url string ,
472
+ request , response interface {}) error {
473
+
440
474
var transport = & http.Transport {
441
475
Dial : (& net.Dialer {
442
- Timeout : 5 * time . Second ,
476
+ Timeout : dialTimeout ,
443
477
}).Dial ,
444
- TLSHandshakeTimeout : 5 * time . Second ,
478
+ TLSHandshakeTimeout : dialTimeout ,
445
479
}
446
480
447
481
var client = & http.Client {
448
- Timeout : time . Second * 10 ,
482
+ Timeout : requestTimeout ,
449
483
Transport : transport ,
450
484
}
451
485
@@ -486,16 +520,18 @@ func post(ctx context.Context, url string, request, response interface{}) error
486
520
}
487
521
488
522
// get sends a request to the HTTP server using the GET method.
489
- func get (ctx context.Context , url string , response interface {}) error {
523
+ func get (ctx context.Context , dialTimeout , requestTimeout time.Duration , url string ,
524
+ response interface {}) error {
525
+
490
526
var transport = & http.Transport {
491
527
Dial : (& net.Dialer {
492
- Timeout : 5 * time . Second ,
528
+ Timeout : dialTimeout ,
493
529
}).Dial ,
494
- TLSHandshakeTimeout : 5 * time . Second ,
530
+ TLSHandshakeTimeout : dialTimeout ,
495
531
}
496
532
497
533
var client = & http.Client {
498
- Timeout : time . Second * 10 ,
534
+ Timeout : requestTimeout ,
499
535
Transport : transport ,
500
536
}
501
537
0 commit comments