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.



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

def initialize(opts)
  @opts = opts
  @ledger = @opts[:ledger_name] || raise(
    ArgumentError,
    'missing ledger_name',
  )
  @credential = @opts[:credential] || raise(
    ArgumentError,
    'missing credential',
  )

  @lock = Mutex.new # protects the following instance variables
  @team_name, @addr, ttl_seconds = hello.call
  @api = api(@addr)
  @deadline = now + ttl_seconds
end

Instance Method Details

#dupObject



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

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

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



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

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

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



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
# File 'lib/sequence/session.rb', line 38

def request_full_resp(id, path, body = {})
  id ||= SecureRandom.hex(10)
  deadline = nil
  api = nil

  @lock.synchronize do
    deadline = @deadline
    api = @api
    path = "/#{@team_name}/#{@ledger}/#{path}".gsub('//', '/')
  end

  if now >= deadline
    refresh
  end

  api.post(id, 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