@@ -99,6 +99,11 @@ type Request struct {
9999 ManualFramingMode bool
100100
101101 sent bool // a request may only be sent once
102+
103+ abi struct {
104+ req * fastly.HTTPRequest
105+ body * fastly.HTTPBody
106+ }
102107}
103108
104109// NewRequest constructs an outgoing request with the given HTTP method, URI,
@@ -276,7 +281,15 @@ func (req *Request) AddCookie(c *Cookie) {
276281// that have been preconfigured in your service, regardless of their URL. Once
277282// sent, a request cannot be sent again.
278283func (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 )
280293 if err != nil {
281294 return nil , err
282295 }
@@ -341,56 +354,66 @@ func (req *Request) Send(ctx context.Context, backend string) (*Response, error)
341354 return resp , nil
342355}
343356
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 " )
347360 }
348361
349362 abiReq , err := fastly .NewHTTPRequest ()
350363 if err != nil {
351- return nil , nil , fmt .Errorf ("construct request: %w" , err )
364+ return fmt .Errorf ("construct request: %w" , err )
352365 }
353366
354367 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 )
356369 }
357370
358371 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 )
360373 }
361374
362375 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 )
364377 }
365378
366379 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 )
368381 }
369382
370383 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 )
372385 }
373386
374387 for _ , key := range req .Header .Keys () {
375388 vals := req .Header .Values (key )
376389 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 )
378391 }
379392 }
380393
381394 abiReqBody , err := abiBodyFrom (req .Body )
382395 if err != nil {
383- return nil , nil , fmt .Errorf ("get body: %w" , err )
396+ return fmt .Errorf ("get body: %w" , err )
384397 }
385398
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 )
387411 if err != nil {
388412 return nil , nil , fmt .Errorf ("begin send: %w" , err )
389413 }
390414
391415 req .sent = true
392-
393- return abiPending , abiReqBody , nil
416+ return abiPending , req .abi .body , nil
394417}
395418
396419// CacheOptions control caching behavior for outgoing requests.
0 commit comments