sequence-sdk 1.5.2 → 2.pre.rc.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './validations'
4
-
5
3
  module Sequence
6
4
  # Base class for ledger client components.
7
5
  # @private
8
6
  class ClientModule
9
- include Sequence::Validations
10
-
11
7
  attr_reader :client
12
8
 
13
9
  def initialize(client)
@@ -45,12 +45,8 @@ module Sequence
45
45
  :seq_code,
46
46
  :temporary,
47
47
  )
48
- # Use {#seq_code} instead.
49
- # @deprecated
50
- attr_accessor :code
51
48
 
52
49
  def initialize(body, response)
53
- self.code = body['code']
54
50
  self.chain_message = body['message']
55
51
  self.detail = body['detail']
56
52
  self.retriable = body['retriable']
data/lib/sequence/feed.rb CHANGED
@@ -82,49 +82,46 @@ module Sequence
82
82
  end
83
83
 
84
84
  class ClientModule < Sequence::ClientModule
85
- # @param [Hash] opts Parameters for creating a Feed.
86
- # @option opts [String] id A unique id for the feed.
87
- # @option opts [String] type The type of the feed: "action" or
88
- # "transaction".
89
- # @option opts [String] filter A valid filter string. The feed will be
90
- # composed of items that match the filter.
91
- # @option opts [Array<String|Integer>] filter_params A list of values that
92
- # will be interpolated into the filter expression.
85
+ # Create a feed.
86
+ # @param type [String] The type of the feed: "action" or "transaction".
87
+ # @param id [String] A unique id for the feed.
88
+ # @param filter [String] A valid filter string. The feed will be composed
89
+ # of items that match the filter.
90
+ # @param filter_params [Array<String|Integer>] A list of values that will
91
+ # be interpolated into the filter expression.
93
92
  # @return [Feed] Newly created feed.
94
- def create(opts = {})
95
- validate_inclusion_of!(
96
- opts,
97
- :id,
98
- :type,
99
- :filter,
100
- :filter_params,
101
- )
102
- validate_required!(opts, :type)
103
- if opts[:type] != 'action' && opts[:type] != 'transaction'
93
+ def create(type:, id: nil, filter: nil, filter_params: nil)
94
+ if type != 'action' && type != 'transaction'
104
95
  raise ArgumentError, ':type must equal action or transaction'
105
96
  end
106
- Feed.new(client.session.request('create-feed', opts), client.session)
97
+ Feed.new(
98
+ client.session.request(
99
+ 'create-feed',
100
+ id: id,
101
+ type: type,
102
+ filter: filter,
103
+ filter_params: filter_params,
104
+ ),
105
+ client.session,
106
+ )
107
107
  end
108
108
 
109
- # Get single feed given an id.
110
- # @param [Hash] opts Parameters to get single feed.
111
- # @option opts [String] id The unique ID of a feed.
109
+ # Get feed by id.
110
+ # @param id [String] The unique ID of a feed.
112
111
  # @return [Feed] Requested feed object.
113
- def get(opts = {})
114
- validate_required!(opts, :id)
115
- Feed.new(client.session.request('get-feed', opts), client.session)
112
+ def get(id:)
113
+ Feed.new(client.session.request('get-feed', id: id), client.session)
116
114
  end
117
115
 
118
- # @param [Hash] opts
119
- # @option opts [String] id The unique ID of a feed.
116
+ # Delete feed by id.
117
+ # @option id [String] The unique ID of a feed.
120
118
  # @return [void]
121
- def delete(opts = {})
122
- validate_required!(opts, :id)
123
- client.session.request('delete-feed', opts)
119
+ def delete(id:)
120
+ client.session.request('delete-feed', id: id)
124
121
  nil
125
122
  end
126
123
 
127
- # Executes a query, returning an enumerable over individual feeds.
124
+ # Execute a query, returning an enumerable over individual feeds.
128
125
  # @return [Query]
129
126
  def list
130
127
  Query.new(client)
@@ -11,7 +11,7 @@ module Sequence
11
11
  # A type or class of value that can be tracked on a ledger.
12
12
  class Flavor < ResponseObject
13
13
  # @!attribute [r] id
14
- # Unique, auto-generated identifier.
14
+ # Unique identifier of the flavor.
15
15
  # @return [String]
16
16
  attrib :id
17
17
 
@@ -21,13 +21,6 @@ module Sequence
21
21
  # @return [Array<String>]
22
22
  attrib(:key_ids)
23
23
 
24
- # @!attribute [r] keys
25
- # Deprecated. Use {#key_ids} instead.
26
- # The set of keys used to sign transactions that issue tokens of the
27
- # flavor.
28
- # @return [Array<Key>]
29
- attrib(:keys) { |raw| raw.map { |k| Key.new(k) } }
30
-
31
24
  # @!attribute [r] quorum
32
25
  # The number of keys required to sign transactions that issue tokens of
33
26
  # the flavor.
@@ -39,74 +32,55 @@ module Sequence
39
32
  # @return [Hash]
40
33
  attrib :tags
41
34
 
42
- # @deprecated Use {#key_ids} instead.
43
35
  class Key < ResponseObject
44
36
  attrib :id
45
37
  end
46
38
 
47
39
  class ClientModule < Sequence::ClientModule
48
- # Creates a new flavor in the ledger.
49
- # @param [Hash] opts
50
- # Options hash
51
- # @option opts [String] id
52
- # Unique, user-specified identifier.
53
- # @option opts [Array<String>] key_ids
40
+ # Create a new flavor in the ledger.
41
+ # @param key_ids [Array<String>]
54
42
  # The set of key IDs used for signing transactions that issue tokens of
55
43
  # the flavor.
56
- # @option opts [Array<Hash>, Array<Sequence::Key>] keys
57
- # Deprecated. Use :key_ids instead.
58
- # The set of keys used for signing transactions that issue tokens of the
59
- # flavor. A key can be either a key object, or a hash containing an
60
- # `id` field.
61
- # @option opts [Integer] quorum
44
+ # @param id [String]
45
+ # Unique identifier. Auto-generated if not specified.
46
+ # @param quorum [Integer]
62
47
  # The number of keys required to sign transactions that issue tokens of
63
48
  # the flavor. Defaults to the number of keys provided.
64
- # @option opts [Hash] tags
49
+ # @param tags [Hash]
65
50
  # User-specified key-value data describing the flavor.
66
51
  # @return [Flavor]
67
- def create(opts = {})
68
- validate_inclusion_of!(opts, :id, :key_ids, :keys, :quorum, :tags)
69
- if (opts[:key_ids].nil? || opts[:key_ids].empty?) &&
70
- (opts[:keys].nil? || opts[:keys].empty?)
71
- raise(
72
- ArgumentError,
73
- ':key_ids or :keys (but not both) must be provided',
74
- )
75
- end
76
- Flavor.new(client.session.request('create-flavor', opts))
52
+ def create(key_ids:, id: nil, quorum: nil, tags: nil)
53
+ raise ArgumentError, ':key_ids cannot be empty' if key_ids == []
54
+ Flavor.new(
55
+ client.session.request(
56
+ 'create-flavor',
57
+ id: id,
58
+ key_ids: key_ids,
59
+ quorum: quorum,
60
+ tags: tags,
61
+ ),
62
+ )
77
63
  end
78
64
 
79
- # Updates a flavor's tags.
80
- # @param [Hash] opts
81
- # Options hash
82
- # @option opts [String] id
65
+ # Update a flavor's tags.
66
+ # @param id [String]
83
67
  # The ID of the flavor.
84
- # @option opts [Hash] tags
68
+ # @param tags [Hash]
85
69
  # A new set of tags, which will replace the existing tags.
86
70
  # @return [void]
87
- def update_tags(opts = {})
88
- validate_inclusion_of!(opts, :id, :tags)
89
- if opts[:id].nil? || opts[:id].empty?
90
- raise ArgumentError, ':id must be provided'
91
- end
92
- client.session.request('update-flavor-tags', opts)
71
+ def update_tags(id:, tags: nil)
72
+ raise ArgumentError, ':id cannot be blank' if id == ''
73
+ client.session.request('update-flavor-tags', id: id, tags: tags)
93
74
  end
94
75
 
95
- # Executes a query, returning an enumerable over individual flavors.
96
- # @param [Hash] opts
97
- # Options hash
98
- # @option opts [String] filter
76
+ # Execute a query, returning an enumerable over individual flavors.
77
+ # @param filter [String]
99
78
  # A filter expression.
100
- # @option opts [Array<String|Integer>] filter_params
79
+ # @param filter_params [Array<String|Integer>]
101
80
  # A list of values that will be interpolated into the filter expression.
102
81
  # @return [Query]
103
- def list(opts = {})
104
- validate_inclusion_of!(
105
- opts,
106
- :filter,
107
- :filter_params,
108
- )
109
- Query.new(client, opts)
82
+ def list(filter: nil, filter_params: nil)
83
+ Query.new(client, filter: filter, filter_params: filter_params)
110
84
  end
111
85
  end
112
86
 
@@ -26,13 +26,10 @@ module Sequence
26
26
  Errno::ECONNREFUSED,
27
27
  ].freeze
28
28
 
29
- attr_accessor :dis_macaroon
30
-
31
- def initialize(base_url, macaroon, opts = {})
29
+ def initialize(base_url, credential, opts = {})
32
30
  @mutex = Mutex.new
33
31
  @base_url = URI(base_url)
34
- @macaroon = macaroon
35
- @dis_macaroon = nil
32
+ @credential = credential
36
33
  @opts = opts
37
34
  @connection = setup_connection
38
35
  end
@@ -57,10 +54,7 @@ module Sequence
57
54
  req['Idempotency-Key'] = idempotency_key
58
55
  req['Name-Set'] = 'snake'
59
56
  req['User-Agent'] = 'chain-sdk-ruby/' + Sequence::VERSION
60
- if !@macaroon.nil? && !@dis_macaroon.nil?
61
- req['Macaroon'] = @macaroon
62
- req['Discharge-Macaroon'] = @dis_macaroon
63
- end
57
+ req['Credential'] = @credential
64
58
  if !@opts[:user].nil? && !@opts[:pass].nil?
65
59
  req.basic_auth(@opts[:user], @opts[:pass])
66
60
  end
@@ -132,10 +126,13 @@ module Sequence
132
126
 
133
127
  # TLS configuration
134
128
  connection.use_ssl = true
135
- connection.verify_mode = @opts[:verify_mode] || OpenSSL::SSL::VERIFY_PEER
136
- [:ca_file, :cert, :key].each do |k|
137
- next unless @opts.key?(:ssl_params) && @opts[:ssl_params].key?(k)
138
- connection.send("#{k}=", @opts[:ssl_params][k])
129
+ connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
130
+ if ENV['SEQTLSVERIFYNONE']
131
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
132
+ end
133
+ if ENV['SEQTLSCA']
134
+ puts "setting ca_file #{ENV['SEQTLSCA']}"
135
+ connection.ca_file = ENV['SEQTLSCA']
139
136
  end
140
137
 
141
138
  # Timeout configuration
data/lib/sequence/key.rb CHANGED
@@ -13,46 +13,16 @@ module Sequence
13
13
  # @return [String]
14
14
  attrib :id
15
15
 
16
- # @!attribute [r] alias
17
- # Deprecated. Use {#id} instead.
18
- # Unique, user-specified identifier of the key.
19
- # @return [String]
20
- attrib :alias
21
-
22
16
  class ClientModule < Sequence::ClientModule
23
- # Creates a key.
24
- # @param [Hash] opts
25
- # Options hash
26
- # @option opts [String] id
27
- # Unique, user-specified identifier of the key.
28
- # @option opts [String] alias
29
- # Deprecated. Use :id instead.
30
- # Unique, user-specified identifier of the key.
17
+ # Create a key.
18
+ # @param id [String]
19
+ # Unique identifier. Auto-generated if not specified.
31
20
  # @return [Key]
32
- def create(opts = {})
33
- validate_inclusion_of!(opts, :alias, :id)
34
- Key.new(client.session.request('create-key', opts))
35
- end
36
-
37
- # @deprecated Use list instead.
38
- # Executes a query, returning an enumerable over individual keys.
39
- # @param [Hash] opts
40
- # Options hash
41
- # @option opts [Array<String>] aliases
42
- # Deprecated. Use :ids instead.
43
- # A list of aliases of keys to retrieve.
44
- # @option opts [Array<String>] ids
45
- # A list of ids of keys to retrieve.
46
- # @option opts [Integer>] page_size
47
- # Deprecated. Use list.page(size: size) instead.
48
- # The number of items to return in the result set.
49
- # @return [Query]
50
- def query(opts = {})
51
- validate_inclusion_of!(opts, :aliases, :ids, :page_size, :after)
52
- Query.new(client, opts)
21
+ def create(id: nil)
22
+ Key.new(client.session.request('create-key', id: id))
53
23
  end
54
24
 
55
- # Lists all keys.
25
+ # List all keys.
56
26
  # Executes a query, returning an enumerable over individual keys.
57
27
  # @return [Query]
58
28
  def list
data/lib/sequence/page.rb CHANGED
@@ -12,12 +12,6 @@ module Sequence
12
12
  # @return [Array]
13
13
  attrib :items
14
14
 
15
- # @!attribute [r] next
16
- # Deprecated. Use {#cursor} instead.
17
- # Query object to request next page of items.
18
- # @return [Hash]
19
- attrib :next
20
-
21
15
  # @!attribute [r] cursor
22
16
  # String encoding the query object to request the next page of items.
23
17
  # @return [String]
@@ -5,8 +5,6 @@ require_relative './page'
5
5
  module Sequence
6
6
  class Query
7
7
  include ::Enumerable
8
- include Sequence::Validations
9
-
10
8
  # @private
11
9
  # @return [Client]
12
10
  attr_reader :client
@@ -44,24 +42,17 @@ module Sequence
44
42
  raise NotImplementedError
45
43
  end
46
44
 
47
- # Returns all objects, blocking until complete.
48
- # Deprecated: use
49
- # {https://ruby-doc.org/core-2.5.0/Enumerable.html Enumerable}
50
- # methods such as to_a instead.
51
- # @deprecated
52
- def all
53
- to_a
54
- end
45
+ alias all to_a
55
46
 
56
47
  # @private
57
48
  def pages
58
49
  PageQuery.new(client, query, method(:fetch), method(:translate))
59
50
  end
60
51
 
61
- def page(opts = {})
62
- validate_inclusion_of!(opts, :size, :cursor)
63
- unless opts[:size].nil? || opts[:size].zero?
64
- opts[:page_size] = opts.delete(:size)
52
+ def page(size: nil, cursor: nil)
53
+ opts = { size: size, cursor: cursor }
54
+ unless size.nil? || size.zero?
55
+ opts[:page_size] = size
65
56
  end
66
57
  @query = @query.merge(opts)
67
58
  pages.page
@@ -83,8 +74,7 @@ module Sequence
83
74
 
84
75
  loop do
85
76
  page = Page.new(@fetch.call(@query), @translate)
86
- @query = page.next
87
-
77
+ @query = { cursor: page.cursor }
88
78
  yield page
89
79
 
90
80
  break if page.last_page
@@ -99,10 +89,7 @@ module Sequence
99
89
  Page.new(@fetch.call(@query), @translate)
100
90
  end
101
91
 
102
- # @deprecated
103
- def all
104
- to_a
105
- end
92
+ alias all to_a
106
93
  end
107
94
  end
108
95
  end
@@ -15,35 +15,15 @@ module Sequence
15
15
  ArgumentError,
16
16
  'missing ledger_name',
17
17
  )
18
- @macaroon = @opts[:credential] || raise(
18
+ @credential = @opts[:credential] || raise(
19
19
  ArgumentError,
20
20
  'missing credential',
21
21
  )
22
-
23
- # Start at 0 to trigger an immediate refresh
24
- @refresh_at = 0
25
-
26
- # Expect this to get set in #refresh!
27
- @team_name = nil
28
-
29
- # This can be used to avoid making an http request to get a
30
- # new discharge macaroon.
31
- @refresh_method = @opts[:refresh_method]
32
- if @refresh_method
33
- unless @refresh_method.respond_to?(:call)
34
- raise ArgumentError, 'refresh_method is not a lambda'
35
- end
36
- if @refresh_method.arity != 1
37
- raise(
38
- ArgumentError,
39
- 'refresh_method must take 1 argument. (the macaroon)',
40
- )
41
- end
42
- end
43
-
44
- addr = ENV['SEQADDR'] || 'api.seq.com'
45
- @session_api = HttpWrapper.new('https://session-' + addr, nil)
46
- @ledger_api = HttpWrapper.new('https://' + addr, @macaroon, @opts)
22
+ @team_name = @opts[:team_name] || raise(
23
+ ArgumentError,
24
+ 'missing team_name',
25
+ )
26
+ @ledger_api = HttpWrapper.new('https://' + @opts[:addr], @credential, @opts)
47
27
  end
48
28
 
49
29
  def dup
@@ -55,7 +35,6 @@ module Sequence
55
35
  end
56
36
 
57
37
  def request_full_resp(id, path, body = {})
58
- refresh!(id)
59
38
  id ||= SecureRandom.hex(10)
60
39
  @ledger_api.post(id, ledger_url(path), body) do |response|
61
40
  # require that the response contains the Chain-Request-ID
@@ -77,23 +56,5 @@ module Sequence
77
56
  path = path[1..-1] if path.start_with?('/')
78
57
  "/#{@team_name}/#{@ledger}/#{path}"
79
58
  end
80
-
81
- def refresh!(id)
82
- return if @refresh_at > Time.now.to_i
83
-
84
- if @refresh_method
85
- result = @refresh_method.call(@macaroon)
86
- else
87
- result = @session_api.post(
88
- id,
89
- '/sessions/validate',
90
- macaroon: @macaroon,
91
- )[:parsed_body]
92
- end
93
-
94
- @team_name = result['team_name']
95
- @refresh_at = Integer(result['refresh_at'])
96
- @ledger_api.dis_macaroon = result['refresh_token']
97
- end
98
59
  end
99
60
  end