Class: Sequence::Session

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

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Session

Returns a new instance of Session.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sequence/session.rb', line 12

def initialize(opts)
  @opts = opts
  @ledger = @opts[:ledger_name] || raise(
    ArgumentError,
    'missing ledger_name',
  )
  @credential = @opts[:credential] || raise(
    ArgumentError,
    'missing credential',
  )
  @team_name = @opts[:team_name] || raise(
    ArgumentError,
    'missing team_name',
  )
  @ledger_api = HttpWrapper.new('https://' + @opts[:addr], @credential, @opts)
end

Instance Method Details

#dupObject



29
30
31
# File 'lib/sequence/session.rb', line 29

def dup
  Sequence::Session.new(@opts)
end

#request(path, body = {}) ⇒ Object



33
34
35
# File 'lib/sequence/session.rb', line 33

def request(path, body = {})
  request_full_resp(nil, path, body)[:parsed_body]
end

#request_full_resp(id, path, body = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sequence/session.rb', line 37

def request_full_resp(id, path, body = {})
  id ||= SecureRandom.hex(10)
  @ledger_api.post(id, ledger_url(path), body) do |response|
    # require that the response contains the Chain-Request-ID
    # http header. Since the Sequence API will always set this
    # header, its absence indicates that the request stopped at
    # some intermediary like a proxy on the local network or
    # a Sequence load balancer. This error will be retried by
    # HttpWrapper.post.
    req_id = response['Chain-Request-ID']
    unless req_id.is_a?(String) && !req_id.empty?
      raise InvalidRequestIDError, response
    end
  end
end