Class: Octoryn::NetHTTPTransport
- Inherits:
-
Object
- Object
- Octoryn::NetHTTPTransport
- Defined in:
- lib/octoryn/transport.rb
Overview
Standard-library HTTP transport with incremental response-body streaming.
Instance Method Summary collapse
-
#initialize(base_url:, api_key:, open_timeout: 30, read_timeout: 600) ⇒ NetHTTPTransport
constructor
A new instance of NetHTTPTransport.
- #post(path, payload, stream: false, &block) ⇒ Object
Constructor Details
#initialize(base_url:, api_key:, open_timeout: 30, read_timeout: 600) ⇒ NetHTTPTransport
Returns a new instance of NetHTTPTransport.
12 13 14 15 16 17 |
# File 'lib/octoryn/transport.rb', line 12 def initialize(base_url:, api_key:, open_timeout: 30, read_timeout: 600) @base_url = base_url.end_with?('/') ? base_url : "#{base_url}/" @api_key = api_key @open_timeout = open_timeout @read_timeout = read_timeout end |
Instance Method Details
#post(path, payload, stream: false, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/octoryn/transport.rb', line 19 def post(path, payload, stream: false, &block) uri = URI.join(@base_url, path) request = Net::HTTP::Post.new(uri) request['Authorization'] = "Bearer #{@api_key}" request['Content-Type'] = 'application/json' request['Accept'] = stream ? 'text/event-stream' : 'application/json' request['User-Agent'] = 'octoryn-ruby/0.1.1' request['X-Octoryn-Sdk'] = 'ruby/0.1.1' request.body = JSON.generate(payload) Net::HTTP.start( uri.hostname, uri.port, use_ssl: uri.scheme == 'https', open_timeout: @open_timeout, read_timeout: @read_timeout ) do |http| if stream stream_response(http, request, &block) else response = http.request(request) Response.new(response.code.to_i, headers(response), response.body) end end end |