Class: ERPC::RestTransport

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credential:, endpoint:, headers:, timeout:, adapter:) ⇒ RestTransport

Returns a new instance of RestTransport.



176
177
178
179
180
181
182
# File 'lib/erpc_sdk/transport.rb', line 176

def initialize(credential:, endpoint:, headers:, timeout:, adapter:)
  @credential = credential
  @endpoint = endpoint
  @headers = headers
  @timeout = timeout
  @adapter = adapter
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



174
175
176
# File 'lib/erpc_sdk/transport.rb', line 174

def endpoint
  @endpoint
end

Instance Method Details

#get(path, query = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/erpc_sdk/transport.rb', line 184

def get(path, query = nil)
  response = @adapter.request(
    method: :get,
    url: url(path, query),
    headers: @headers.merge("authorization" => "Bearer #{@credential}", "accept" => "application/json"),
    timeout: @timeout
  )
  raise HttpError, response.status unless response.status.between?(200, 299)

  JSON.parse(response.body)
rescue JSON::ParserError
  raise InvalidResponseError, "ERPC returned malformed JSON"
end

#stream(path, query = nil) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/erpc_sdk/transport.rb', line 198

def stream(path, query = nil)
  @adapter.stream(
    url: url(path, query),
    headers: @headers.merge("authorization" => "Bearer #{@credential}", "accept" => "text/event-stream"),
    timeout: @timeout
  )
end