Class: Apirelio::HttpBatchTransport
- Inherits:
-
Object
- Object
- Apirelio::HttpBatchTransport
- Defined in:
- lib/apirelio/http_batch_transport.rb
Instance Method Summary collapse
-
#initialize(endpoint:, api_key:, timeout: 2.0, max_retries: 2) ⇒ HttpBatchTransport
constructor
A new instance of HttpBatchTransport.
- #send(events) ⇒ Object
Constructor Details
#initialize(endpoint:, api_key:, timeout: 2.0, max_retries: 2) ⇒ HttpBatchTransport
Returns a new instance of HttpBatchTransport.
18 19 20 21 22 23 |
# File 'lib/apirelio/http_batch_transport.rb', line 18 def initialize(endpoint:, api_key:, timeout: 2.0, max_retries: 2) @uri = URI.join("#{endpoint.to_s.sub(%r{/+\z}, "")}/", "ingest/v1/events/batch") @api_key = api_key @timeout = [timeout.to_f, 0.1].max @max_retries = [[max_retries.to_i, 0].max, 10].min end |
Instance Method Details
#send(events) ⇒ Object
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 |
# File 'lib/apirelio/http_batch_transport.rb', line 25 def send(events) return if events.empty? request = Net::HTTP::Post.new(@uri) request["Accept"] = "application/json" request["Authorization"] = "Bearer #{@api_key}" request["Content-Type"] = "application/json" request["User-Agent"] = "apirelio-ruby/#{VERSION}" request.body = JSON.generate(events: events) attempts = 0 begin response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.scheme == "https", open_timeout: @timeout, read_timeout: @timeout) do |http| http.request(request) end status = response.code.to_i return if status.between?(200, 299) error = IngestionError.new("Apirelio ingestion returned HTTP #{status}.", status) raise error if status < 500 && status != 429 raise error rescue StandardError => error if error.is_a?(IngestionError) && error.status < 500 && error.status != 429 raise error end attempts += 1 raise error if attempts > @max_retries sleep(0.1 * (2**(attempts - 1))) retry end end |