class Riffer::HTTP::Client
Constants
- DEFAULT_OPEN_TIMEOUT
-
@rbs @connection: Faraday::Connection @rbs @extra_headers: Hash[String, String]
- DEFAULT_READ_TIMEOUT
Public Class Methods
Source
# File lib/riffer/http/client.rb, line 18 def initialize(headers: {}, connection: nil) @extra_headers = headers @connection = connection || Faraday.new( request: { open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT }, ) end
headers are sent on every request and win over the subclass’s auth and version headers, as do headers set on the connection.
Public Instance Methods
Source
# File lib/riffer/http/client.rb, line 28 def post(path, body) response = request(path, body) raise_api_error!(response.status, response.body) unless response.success? JSON.parse(response.body, symbolize_names: true) end
Raises Riffer::APIError on a non-success status.
Source
# File lib/riffer/http/client.rb, line 37 def post_stream(path, body, &) streamed = false yielded = false held = +"" # A proc, not a lambda: httpx and typhoeus pass no env. Without one the # status is unknown until the response returns, so every chunk is held. on_data = proc do |chunk, bytes_received, env| streamed = true # Faraday restarts bytes_received on each attempt. httpx passes the chunk # size instead, so attempts are only told apart when there is an env. if env && bytes_received == chunk.bytesize if yielded raise Riffer::Error, "the stream restarted after data was delivered; " \ "retry middleware must not retry streaming requests mid-body" end held.clear end if env&.success? unless chunk.empty? yielded = true yield(chunk) end else held << chunk end end response = request(path, body, on_data: on_data) unyielded = streamed ? held : response.body raise_api_error!(response.status, unyielded) unless response.success? yield(unyielded) unless unyielded.empty? end
Raises Riffer::APIError on a non-success status; the error body is never yielded.