Class: Igniter::LedgerClient::Transports::RemoteHTTP
- Inherits:
-
Object
- Object
- Igniter::LedgerClient::Transports::RemoteHTTP
- Defined in:
- lib/igniter/ledger_client/transports/remote_http.rb
Instance Attribute Summary collapse
-
#events_uri ⇒ Object
readonly
Returns the value of attribute events_uri.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#read_timeout ⇒ Object
readonly
Returns the value of attribute read_timeout.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #dispatch(envelope) ⇒ Object
-
#initialize(endpoint, events_url: nil, open_timeout: 1.0, read_timeout: 2.0, write_timeout: nil, headers: {}) ⇒ RemoteHTTP
constructor
A new instance of RemoteHTTP.
- #subscribe(stores:, cursor: nil, &block) ⇒ Object
Constructor Details
#initialize(endpoint, events_url: nil, open_timeout: 1.0, read_timeout: 2.0, write_timeout: nil, headers: {}) ⇒ RemoteHTTP
Returns a new instance of RemoteHTTP.
14 15 16 17 18 19 20 21 |
# File 'lib/igniter/ledger_client/transports/remote_http.rb', line 14 def initialize(endpoint, events_url: nil, open_timeout: 1.0, read_timeout: 2.0, write_timeout: nil, headers: {}) @uri = normalize_endpoint(endpoint) @events_uri = normalize_events_endpoint(events_url) @open_timeout = open_timeout @read_timeout = read_timeout @write_timeout = write_timeout @headers = headers end |
Instance Attribute Details
#events_uri ⇒ Object (readonly)
Returns the value of attribute events_uri.
12 13 14 |
# File 'lib/igniter/ledger_client/transports/remote_http.rb', line 12 def events_uri @events_uri end |
#open_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
12 13 14 |
# File 'lib/igniter/ledger_client/transports/remote_http.rb', line 12 def open_timeout @open_timeout end |
#read_timeout ⇒ Object (readonly)
Returns the value of attribute read_timeout.
12 13 14 |
# File 'lib/igniter/ledger_client/transports/remote_http.rb', line 12 def read_timeout @read_timeout end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
12 13 14 |
# File 'lib/igniter/ledger_client/transports/remote_http.rb', line 12 def uri @uri end |
Instance Method Details
#dispatch(envelope) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/igniter/ledger_client/transports/remote_http.rb', line 23 def dispatch(envelope) request = Net::HTTP::Post.new(uri) request["Content-Type"] = "application/json" @headers.each { |key, value| request[key.to_s] = value } request.body = JSON.generate(envelope) response = http.request(request) raise TransportError, "ledger HTTP #{uri} returned #{response.code}" unless response.code.to_i.between?(200, 299) JSON.parse(response.body, symbolize_names: true) rescue JSON::ParserError => e raise TransportError, "invalid ledger HTTP response: #{e.}" end |
#subscribe(stores:, cursor: nil, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/igniter/ledger_client/transports/remote_http.rb', line 37 def subscribe(stores:, cursor: nil, &block) raise ArgumentError, "subscribe requires a block" unless block http_client = nil thread = nil subscription = Subscription.new do http_client&.finish if http_client&.started? thread&.join(1) unless Thread.current.equal?(thread) rescue IOError, SystemCallError nil end thread = Thread.new do stream_uri = events_stream_uri(stores: stores, cursor: cursor) request = Net::HTTP::Get.new(stream_uri) request["Accept"] = "text/event-stream" @headers.each { |key, value| request[key.to_s] = value } http_client = http_for(stream_uri) http_client.request(request) do |response| raise TransportError, "ledger SSE #{stream_uri} returned #{response.code}" unless response.code.to_i.between?(200, 299) read_sse(response, subscription, &block) end rescue StandardError => e subscription.error = e unless subscription&.closed? ensure subscription&.close unless subscription&.closed? end subscription end |