Class: LogBrew::HttpTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/logbrew.rb

Constant Summary collapse

DEFAULT_ENDPOINT =
"https://api.logbrew.co/v1/events"
DEFAULT_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint: DEFAULT_ENDPOINT, headers: {}, timeout: DEFAULT_TIMEOUT, http_client: nil) ⇒ HttpTransport

Returns a new instance of HttpTransport.



120
121
122
123
124
125
# File 'lib/logbrew.rb', line 120

def initialize(endpoint: DEFAULT_ENDPOINT, headers: {}, timeout: DEFAULT_TIMEOUT, http_client: nil)
  @endpoint = validate_endpoint(endpoint)
  @headers = copy_headers(headers)
  @timeout = validate_timeout(timeout)
  @http_client = http_client
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



118
119
120
# File 'lib/logbrew.rb', line 118

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



118
119
120
# File 'lib/logbrew.rb', line 118

def headers
  @headers
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



118
119
120
# File 'lib/logbrew.rb', line 118

def http_client
  @http_client
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



118
119
120
# File 'lib/logbrew.rb', line 118

def timeout
  @timeout
end

Instance Method Details

#send(api_key, body) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/logbrew.rb', line 127

def send(api_key, body)
  Validation.require_non_empty("api_key", api_key)
  raise SdkError.new("validation_error", "body must be non-empty") if body.nil?

  request = Net::HTTP::Post.new(request_path)
  request["authorization"] = "Bearer #{api_key}"
  request["content-type"] = "application/json"
  @headers.each { |name, value| request[name] = value }
  request.body = body

  response = HttpClientTracing.suppress do
    @http_client ? @http_client.request(request) : request_with_default_client(request)
  end
  TransportResponse.new(response.code.to_i, 1)
rescue TransportError
  raise
rescue IOError, SystemCallError, SocketError, Timeout::Error, EOFError, Net::OpenTimeout, Net::ReadTimeout => error
  raise TransportError.network("http transport failed: #{error.message}")
end