github.com/Jeffail/benthos/v3@v3.65.0/lib/input/reader/http_client.go (about)

     1  package reader
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/Jeffail/benthos/v3/lib/types"
     9  	"github.com/Jeffail/benthos/v3/lib/util/http/client"
    10  )
    11  
    12  //------------------------------------------------------------------------------
    13  
    14  // HTTPClient is a reader that continuously polls an HTTP endpoint, providing an
    15  // optional payload each time.
    16  //
    17  // Deprecated: This component is no longer used by Benthos, instead look at
    18  // ./lib/input/http_client.go.
    19  type HTTPClient struct {
    20  	payload types.Message
    21  	// nolint:staticcheck // Ignore nested deprecated component
    22  	client *client.Type
    23  
    24  	dropEmptyBodies bool
    25  }
    26  
    27  // HTTPClientOptFunc changes the behaviour of an HTTPClient reader.
    28  type HTTPClientOptFunc func(*HTTPClient)
    29  
    30  // HTTPClientOptSetDropEmpty determines whether payloads received that are empty
    31  // should be dropped rather than propagated as an empty benthos message.
    32  func HTTPClientOptSetDropEmpty(dropEmpty bool) HTTPClientOptFunc {
    33  	return func(h *HTTPClient) {
    34  		h.dropEmptyBodies = dropEmpty
    35  	}
    36  }
    37  
    38  // NewHTTPClient creates a new HTTPClient reader type.
    39  //
    40  // Deprecated: This component is no longer used by Benthos, instead look at
    41  // ./lib/input/http_client.go.
    42  func NewHTTPClient(payload types.Message, httpClient *client.Type, opts ...HTTPClientOptFunc) (*HTTPClient, error) {
    43  	h := &HTTPClient{
    44  		payload:         payload,
    45  		client:          httpClient,
    46  		dropEmptyBodies: true,
    47  	}
    48  
    49  	for _, opt := range opts {
    50  		opt(h)
    51  	}
    52  
    53  	return h, nil
    54  }
    55  
    56  //------------------------------------------------------------------------------
    57  
    58  // Connect establishes a connection.
    59  func (h *HTTPClient) Connect() (err error) {
    60  	return nil
    61  }
    62  
    63  // ConnectWithContext establishes a connection.
    64  func (h *HTTPClient) ConnectWithContext(ctx context.Context) (err error) {
    65  	return nil
    66  }
    67  
    68  //------------------------------------------------------------------------------
    69  
    70  // ReadWithContext a new HTTPClient message.
    71  func (h *HTTPClient) ReadWithContext(ctx context.Context) (types.Message, AsyncAckFn, error) {
    72  	// nolint:staticcheck // Ignore nested deprecated component
    73  	res, err := h.client.Do(h.payload)
    74  	if err != nil {
    75  		if strings.Contains(err.Error(), "(Client.Timeout exceeded while awaiting headers)") {
    76  			err = types.ErrTimeout
    77  		}
    78  		return nil, nil, err
    79  	}
    80  
    81  	var msg types.Message
    82  	// nolint:staticcheck // Ignore nested deprecated component
    83  	if msg, err = h.client.ParseResponse(res); err != nil {
    84  		return nil, nil, err
    85  	}
    86  
    87  	if msg.Len() == 0 {
    88  		return nil, nil, types.ErrTimeout
    89  	}
    90  	if msg.Len() == 1 && msg.Get(0).IsEmpty() && h.dropEmptyBodies {
    91  		return nil, nil, types.ErrTimeout
    92  	}
    93  
    94  	return msg, noopAsyncAckFn, nil
    95  }
    96  
    97  // CloseAsync shuts down the HTTPClient input and stops processing requests.
    98  func (h *HTTPClient) CloseAsync() {
    99  	// nolint:staticcheck // Ignore nested deprecated component
   100  	h.client.CloseAsync()
   101  }
   102  
   103  // WaitForClose blocks until the HTTPClient input has closed down.
   104  func (h *HTTPClient) WaitForClose(timeout time.Duration) error {
   105  	return nil
   106  }
   107  
   108  //------------------------------------------------------------------------------