@@ -99,6 +99,11 @@ type Request struct {
99
99
ManualFramingMode bool
100
100
101
101
sent bool // a request may only be sent once
102
+
103
+ abi struct {
104
+ req * fastly.HTTPRequest
105
+ body * fastly.HTTPBody
106
+ }
102
107
}
103
108
104
109
// NewRequest constructs an outgoing request with the given HTTP method, URI,
@@ -276,7 +281,15 @@ func (req *Request) AddCookie(c *Cookie) {
276
281
// that have been preconfigured in your service, regardless of their URL. Once
277
282
// sent, a request cannot be sent again.
278
283
func (req * Request ) Send (ctx context.Context , backend string ) (* Response , error ) {
279
- abiPending , abiReqBody , err := req .sendAsyncStreaming (backend )
284
+
285
+ if req .abi .req == nil && req .abi .body == nil {
286
+ // abi request not yet constructed
287
+ if err := req .constructABIRequest (); err != nil {
288
+ return nil , err
289
+ }
290
+ }
291
+
292
+ abiPending , abiReqBody , err := req .sendABIRequestAsyncStreaming (backend )
280
293
if err != nil {
281
294
return nil , err
282
295
}
@@ -341,56 +354,66 @@ func (req *Request) Send(ctx context.Context, backend string) (*Response, error)
341
354
return resp , nil
342
355
}
343
356
344
- func (req * Request ) sendAsyncStreaming ( backend string ) ( * fastly. PendingRequest , * fastly. HTTPBody , error ) {
345
- if req .sent {
346
- return nil , nil , fmt .Errorf ("request already sent " )
357
+ func (req * Request ) constructABIRequest () error {
358
+ if req .abi . req != nil || req . abi . body != nil {
359
+ return fmt .Errorf ("request already constructed " )
347
360
}
348
361
349
362
abiReq , err := fastly .NewHTTPRequest ()
350
363
if err != nil {
351
- return nil , nil , fmt .Errorf ("construct request: %w" , err )
364
+ return fmt .Errorf ("construct request: %w" , err )
352
365
}
353
366
354
367
if err := abiReq .SetMethod (req .Method ); err != nil {
355
- return nil , nil , fmt .Errorf ("set method: %w" , err )
368
+ return fmt .Errorf ("set method: %w" , err )
356
369
}
357
370
358
371
if err := abiReq .SetURI (req .URL .String ()); err != nil {
359
- return nil , nil , fmt .Errorf ("set URL: %w" , err )
372
+ return fmt .Errorf ("set URL: %w" , err )
360
373
}
361
374
362
375
if err := abiReq .SetAutoDecompressResponse (fastly .AutoDecompressResponseOptions (req .DecompressResponseOptions )); err != nil {
363
- return nil , nil , fmt .Errorf ("set auto decompress response: %w" , err )
376
+ return fmt .Errorf ("set auto decompress response: %w" , err )
364
377
}
365
378
366
379
if err := abiReq .SetFramingHeadersMode (req .ManualFramingMode ); err != nil {
367
- return nil , nil , fmt .Errorf ("set framing headers mode: %w" , err )
380
+ return fmt .Errorf ("set framing headers mode: %w" , err )
368
381
}
369
382
370
383
if err := abiReq .SetCacheOverride (fastly .CacheOverrideOptions (req .CacheOptions )); err != nil {
371
- return nil , nil , fmt .Errorf ("set cache options: %w" , err )
384
+ return fmt .Errorf ("set cache options: %w" , err )
372
385
}
373
386
374
387
for _ , key := range req .Header .Keys () {
375
388
vals := req .Header .Values (key )
376
389
if err := abiReq .SetHeaderValues (key , vals ); err != nil {
377
- return nil , nil , fmt .Errorf ("set headers: %w" , err )
390
+ return fmt .Errorf ("set headers: %w" , err )
378
391
}
379
392
}
380
393
381
394
abiReqBody , err := abiBodyFrom (req .Body )
382
395
if err != nil {
383
- return nil , nil , fmt .Errorf ("get body: %w" , err )
396
+ return fmt .Errorf ("get body: %w" , err )
384
397
}
385
398
386
- abiPending , err := abiReq .SendAsyncStreaming (abiReqBody , backend )
399
+ req .abi .req = abiReq
400
+ req .abi .body = abiReqBody
401
+
402
+ return nil
403
+ }
404
+
405
+ func (req * Request ) sendABIRequestAsyncStreaming (backend string ) (* fastly.PendingRequest , * fastly.HTTPBody , error ) {
406
+ if req .sent {
407
+ return nil , nil , fmt .Errorf ("request already sent" )
408
+ }
409
+
410
+ abiPending , err := req .abi .req .SendAsyncStreaming (req .abi .body , backend )
387
411
if err != nil {
388
412
return nil , nil , fmt .Errorf ("begin send: %w" , err )
389
413
}
390
414
391
415
req .sent = true
392
-
393
- return abiPending , abiReqBody , nil
416
+ return abiPending , req .abi .body , nil
394
417
}
395
418
396
419
// CacheOptions control caching behavior for outgoing requests.
0 commit comments