tronzap-sdk 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +30 -1
- data/README.es.md +87 -5
- data/README.md +88 -5
- data/README.pt-br.md +87 -5
- data/README.ru.md +85 -5
- data/lib/tronzap/client.rb +96 -4
- data/lib/tronzap/coerce.rb +4 -0
- data/lib/tronzap/errors.rb +71 -15
- data/lib/tronzap/models/bulk_rejected_row.rb +30 -0
- data/lib/tronzap/models/enums.rb +8 -0
- data/lib/tronzap/requests/bulk_calculate.rb +40 -0
- data/lib/tronzap/requests/bulk_create.rb +52 -0
- data/lib/tronzap/requests/bulk_history.rb +28 -0
- data/lib/tronzap/requests/bulk_item.rb +29 -0
- data/lib/tronzap/requests/bulk_lookup.rb +48 -0
- data/lib/tronzap/requests/bulk_report.rb +55 -0
- data/lib/tronzap/response_decoder.rb +53 -15
- data/lib/tronzap/responses/bulk_calculation.rb +44 -0
- data/lib/tronzap/responses/bulk_history.rb +28 -0
- data/lib/tronzap/responses/bulk_order.rb +87 -0
- data/lib/tronzap/version.rb +1 -1
- data/lib/tronzap.rb +10 -0
- metadata +11 -1
|
@@ -20,8 +20,9 @@ module Tronzap
|
|
|
20
20
|
body = text(response.body)
|
|
21
21
|
document, parse_error = parse(body)
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
retry_after = retry_after(response.headers, parse_error ? nil : document)
|
|
24
|
+
check_api_error(document, status, body, retry_after) unless parse_error
|
|
25
|
+
raise http_error(status, body, retry_after) unless (200..299).cover?(status)
|
|
25
26
|
|
|
26
27
|
result = result(document, parse_error, status, body)
|
|
27
28
|
begin
|
|
@@ -55,7 +56,14 @@ module Tronzap
|
|
|
55
56
|
[nil, e]
|
|
56
57
|
end
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
# @param response [#status, #headers, #body]
|
|
60
|
+
# @return [Boolean] whether the status is 2xx and the body is +text/csv+
|
|
61
|
+
def csv?(response)
|
|
62
|
+
(200..299).cover?(response.status.to_i) &&
|
|
63
|
+
header(response.headers, "content-type").to_s.split(";").first.to_s.strip.casecmp?("text/csv")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def check_api_error(document, status, body, retry_after)
|
|
59
67
|
unless document.is_a?(Hash)
|
|
60
68
|
raise ApiError.new(UNKNOWN_API_ERROR, code: ErrorCode::AUTH_ERROR, status: status, response_body: body)
|
|
61
69
|
end
|
|
@@ -63,16 +71,35 @@ module Tronzap
|
|
|
63
71
|
code = code(document["code"])
|
|
64
72
|
return if code&.zero?
|
|
65
73
|
|
|
66
|
-
raise
|
|
74
|
+
raise api_error(
|
|
67
75
|
string(document["error"]) || UNKNOWN_API_ERROR,
|
|
68
76
|
code: code || ErrorCode::AUTH_ERROR,
|
|
69
77
|
status: status,
|
|
70
78
|
response_body: body,
|
|
71
79
|
error_key: string(document["key"]),
|
|
72
|
-
request_id: string(document["request_id"])
|
|
80
|
+
request_id: string(document["request_id"]),
|
|
81
|
+
result: document["result"],
|
|
82
|
+
retry_after: retry_after
|
|
73
83
|
)
|
|
74
84
|
end
|
|
75
85
|
|
|
86
|
+
def api_error(message, **details)
|
|
87
|
+
result = details[:result]
|
|
88
|
+
if result.is_a?(Hash)
|
|
89
|
+
case details[:code]
|
|
90
|
+
when ErrorCode::BULK_PRICE_CHANGED
|
|
91
|
+
return BulkPriceChangedError.new(message, calculation: Responses::BulkCalculation.from_api(result),
|
|
92
|
+
**details)
|
|
93
|
+
when ErrorCode::BULK_NO_VALID_ITEMS
|
|
94
|
+
return BulkNoValidItemsError.new(message, rejected: Models::BulkRejectedRow.list_from_api(result["rejected"]),
|
|
95
|
+
**details)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
ApiError.new(message, **details)
|
|
99
|
+
rescue Coerce::Error
|
|
100
|
+
ApiError.new(message, **details)
|
|
101
|
+
end
|
|
102
|
+
|
|
76
103
|
def code(value)
|
|
77
104
|
case value
|
|
78
105
|
when Integer then value
|
|
@@ -88,20 +115,21 @@ module Tronzap
|
|
|
88
115
|
end
|
|
89
116
|
end
|
|
90
117
|
|
|
91
|
-
def http_error(status, body,
|
|
118
|
+
def http_error(status, body, retry_after)
|
|
119
|
+
details = { status: status, response_body: body, retry_after: retry_after }
|
|
92
120
|
case status
|
|
93
|
-
when 429
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
when 500.. then ServerError.new("Server error (HTTP #{status})", status: status, response_body: body)
|
|
98
|
-
else HttpError.new("HTTP error #{status}", status: status, response_body: body)
|
|
121
|
+
when 429 then RateLimitError.new("Too many requests (HTTP 429)", **details)
|
|
122
|
+
when 401, 403 then UnauthorizedError.new("Unauthorized (HTTP #{status})", **details)
|
|
123
|
+
when 500.. then ServerError.new("Server error (HTTP #{status})", **details)
|
|
124
|
+
else HttpError.new("HTTP error #{status}", **details)
|
|
99
125
|
end
|
|
100
126
|
end
|
|
101
127
|
|
|
102
|
-
def retry_after(headers)
|
|
103
|
-
|
|
104
|
-
|
|
128
|
+
def retry_after(headers, document)
|
|
129
|
+
retry_after_header(header(headers, "retry-after")) || retry_after_field(document)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def retry_after_header(value)
|
|
105
133
|
return nil unless value.is_a?(String)
|
|
106
134
|
|
|
107
135
|
value = value.strip
|
|
@@ -111,5 +139,15 @@ module Tronzap
|
|
|
111
139
|
rescue ArgumentError
|
|
112
140
|
nil
|
|
113
141
|
end
|
|
142
|
+
|
|
143
|
+
def retry_after_field(document)
|
|
144
|
+
value = document["retry_after"] if document.is_a?(Hash)
|
|
145
|
+
value.to_f if value.is_a?(Integer) && !value.negative?
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def header(headers, name)
|
|
149
|
+
value = headers.to_h.find { |key, _| key.to_s.casecmp?(name) }&.last
|
|
150
|
+
value.is_a?(Array) ? value.first : value
|
|
151
|
+
end
|
|
114
152
|
end
|
|
115
153
|
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tronzap
|
|
4
|
+
module Responses
|
|
5
|
+
# The price of a bulk energy order. Nothing is charged until the order is started with {Client#bulk_create}.
|
|
6
|
+
#
|
|
7
|
+
# @!attribute [r] id
|
|
8
|
+
# @return [String] the calculation ID to pass to {Client#bulk_create}
|
|
9
|
+
# @!attribute [r] status
|
|
10
|
+
# @return [Symbol] +:draft+, or another of {Models::BULK_STATUSES} or +:unknown+
|
|
11
|
+
# @!attribute [r] resource
|
|
12
|
+
# @return [String] the resource ordered, +energy+
|
|
13
|
+
# @!attribute [r] duration
|
|
14
|
+
# @return [Integer] rental duration in hours
|
|
15
|
+
# @!attribute [r] total_amount
|
|
16
|
+
# @return [BigDecimal] the price of the accepted rows, in TRX
|
|
17
|
+
# @!attribute [r] accepted
|
|
18
|
+
# @return [Integer] how many rows will be ordered
|
|
19
|
+
# @!attribute [r] rejected_count
|
|
20
|
+
# @return [Integer] how many rows were rejected
|
|
21
|
+
# @!attribute [r] rejected
|
|
22
|
+
# @return [Array<Models::BulkRejectedRow>] the rejected rows with their row numbers and reasons
|
|
23
|
+
# @!attribute [r] expires_at
|
|
24
|
+
# @return [Models::Timestamp, nil] when the calculation expires, 10 minutes after it was made
|
|
25
|
+
BulkCalculation = Data.define(:id, :status, :resource, :duration, :total_amount, :accepted, :rejected_count,
|
|
26
|
+
:rejected, :expires_at) do
|
|
27
|
+
# @api private
|
|
28
|
+
def self.from_api(value)
|
|
29
|
+
data = Coerce.object(value, "result")
|
|
30
|
+
new(
|
|
31
|
+
id: Coerce.text(data["id"], "id"),
|
|
32
|
+
status: Coerce.enum(data["status"], Models::BULK_STATUSES, "status"),
|
|
33
|
+
resource: Coerce.text(data["resource"], "resource"),
|
|
34
|
+
duration: Coerce.integer(data["duration"], "duration"),
|
|
35
|
+
total_amount: Coerce.decimal(data["total_amount"], "total_amount"),
|
|
36
|
+
accepted: Coerce.integer(data["accepted"], "accepted"),
|
|
37
|
+
rejected_count: Coerce.integer(data["rejected_count"], "rejected_count"),
|
|
38
|
+
rejected: Models::BulkRejectedRow.list_from_api(data["rejected"]),
|
|
39
|
+
expires_at: Coerce.timestamp(data["expires_at"], "expires_at")
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tronzap
|
|
4
|
+
module Responses
|
|
5
|
+
# One page of your bulk orders, newest first.
|
|
6
|
+
#
|
|
7
|
+
# @!attribute [r] page
|
|
8
|
+
# @return [Integer] the page number, starting at 1
|
|
9
|
+
# @!attribute [r] per_page
|
|
10
|
+
# @return [Integer] the page size
|
|
11
|
+
# @!attribute [r] total
|
|
12
|
+
# @return [Integer] the number of bulk orders across all pages
|
|
13
|
+
# @!attribute [r] items
|
|
14
|
+
# @return [Array<BulkOrder>] the orders on this page
|
|
15
|
+
BulkHistory = Data.define(:page, :per_page, :total, :items) do
|
|
16
|
+
# @api private
|
|
17
|
+
def self.from_api(value)
|
|
18
|
+
data = Coerce.object(value, "result")
|
|
19
|
+
new(
|
|
20
|
+
page: Coerce.integer(data["page"], "page"),
|
|
21
|
+
per_page: Coerce.integer(data["per_page"], "per_page"),
|
|
22
|
+
total: Coerce.integer(data["total"], "total"),
|
|
23
|
+
items: Coerce.list(data["items"], "items") { BulkOrder.from_api(_1) }
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tronzap
|
|
4
|
+
module Responses
|
|
5
|
+
# A bulk energy order and its progress.
|
|
6
|
+
#
|
|
7
|
+
# @!attribute [r] id
|
|
8
|
+
# @return [String] the bulk order ID assigned by TronZap
|
|
9
|
+
# @!attribute [r] external_id
|
|
10
|
+
# @return [String, nil] the ID you passed when starting the order
|
|
11
|
+
# @!attribute [r] status
|
|
12
|
+
# @return [Symbol] the current state, one of {Models::BULK_STATUSES} or +:unknown+. +:completed+, +:partial+
|
|
13
|
+
# and +:failed+ are final.
|
|
14
|
+
# @!attribute [r] resource
|
|
15
|
+
# @return [String] the resource ordered, +energy+
|
|
16
|
+
# @!attribute [r] duration
|
|
17
|
+
# @return [Integer] rental duration in hours
|
|
18
|
+
# @!attribute [r] force
|
|
19
|
+
# @return [Boolean] whether rows are bought even if the price rose, within {max_price_increase}
|
|
20
|
+
# @!attribute [r] max_price_increase
|
|
21
|
+
# @return [Integer] the allowed price increase in percent
|
|
22
|
+
# @!attribute [r] webhook_url
|
|
23
|
+
# @return [String, nil] the URL called when the order becomes final
|
|
24
|
+
# @!attribute [r] total
|
|
25
|
+
# @return [Integer] how many rows the order has
|
|
26
|
+
# @!attribute [r] processed
|
|
27
|
+
# @return [Integer] how many rows are processed
|
|
28
|
+
# @!attribute [r] delivered
|
|
29
|
+
# @return [Integer] how many rows received energy
|
|
30
|
+
# @!attribute [r] failed
|
|
31
|
+
# @return [Integer] how many rows failed
|
|
32
|
+
# @!attribute [r] in_review
|
|
33
|
+
# @return [Integer] how many rows wait for a manual check
|
|
34
|
+
# @!attribute [r] skipped
|
|
35
|
+
# @return [Integer] how many rows were skipped without a charge
|
|
36
|
+
# @!attribute [r] skipped_by_reason
|
|
37
|
+
# @return [Hash{String => Integer}] the skipped rows by reason, for example +address_not_activated+ or
|
|
38
|
+
# +price_increased+
|
|
39
|
+
# @!attribute [r] rejected_count
|
|
40
|
+
# @return [Integer] how many rows the calculation rejected
|
|
41
|
+
# @!attribute [r] total_amount
|
|
42
|
+
# @return [BigDecimal] the calculated price of the order, in TRX
|
|
43
|
+
# @!attribute [r] charged_amount
|
|
44
|
+
# @return [BigDecimal] the amount charged so far, in TRX
|
|
45
|
+
# @!attribute [r] created_at
|
|
46
|
+
# @return [Models::Timestamp, nil] when the order was created
|
|
47
|
+
# @!attribute [r] started_at
|
|
48
|
+
# @return [Models::Timestamp, nil] when processing started
|
|
49
|
+
# @!attribute [r] finalized_at
|
|
50
|
+
# @return [Models::Timestamp, nil] when the order became final, +nil+ while it runs
|
|
51
|
+
BulkOrder = Data.define(:id, :external_id, :status, :resource, :duration, :force, :max_price_increase,
|
|
52
|
+
:webhook_url, :total, :processed, :delivered, :failed, :in_review, :skipped,
|
|
53
|
+
:skipped_by_reason, :rejected_count, :total_amount, :charged_amount, :created_at,
|
|
54
|
+
:started_at, :finalized_at) do
|
|
55
|
+
# @api private
|
|
56
|
+
def self.from_api(value)
|
|
57
|
+
data = Coerce.object(value, "bulk order")
|
|
58
|
+
new(
|
|
59
|
+
id: Coerce.text(data["id"], "id"),
|
|
60
|
+
external_id: Coerce.optional_text(data["external_id"], "external_id"),
|
|
61
|
+
status: Coerce.enum(data["status"], Models::BULK_STATUSES, "status"),
|
|
62
|
+
resource: Coerce.text(data["resource"], "resource"),
|
|
63
|
+
duration: Coerce.integer(data["duration"], "duration"),
|
|
64
|
+
force: Coerce.boolean(data["force"], "force"),
|
|
65
|
+
max_price_increase: Coerce.integer(data["max_price_increase"], "max_price_increase"),
|
|
66
|
+
webhook_url: Coerce.optional_text(data["webhook_url"], "webhook_url"),
|
|
67
|
+
**counters(data),
|
|
68
|
+
skipped_by_reason: Coerce.integer_map(data["skipped_by_reason"], "skipped_by_reason"),
|
|
69
|
+
total_amount: Coerce.decimal(data["total_amount"], "total_amount"),
|
|
70
|
+
charged_amount: Coerce.decimal(data["charged_amount"], "charged_amount"),
|
|
71
|
+
**timestamps(data)
|
|
72
|
+
)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def self.timestamps(data)
|
|
76
|
+
%w[created_at started_at finalized_at].to_h { |name| [name.to_sym, Coerce.timestamp(data[name], name)] }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def self.counters(data)
|
|
80
|
+
%w[total processed delivered failed in_review skipped rejected_count].to_h do |name|
|
|
81
|
+
[name.to_sym, Coerce.integer(data[name], name)]
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
private_class_method :counters, :timestamps
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
data/lib/tronzap/version.rb
CHANGED
data/lib/tronzap.rb
CHANGED
|
@@ -18,6 +18,7 @@ require_relative "tronzap/models/activate_address_rate"
|
|
|
18
18
|
require_relative "tronzap/models/direct_recharge_rate"
|
|
19
19
|
require_relative "tronzap/models/aml_risk_factor"
|
|
20
20
|
require_relative "tronzap/models/subscription_params"
|
|
21
|
+
require_relative "tronzap/models/bulk_rejected_row"
|
|
21
22
|
require_relative "tronzap/requests/validation"
|
|
22
23
|
require_relative "tronzap/requests/estimate_energy"
|
|
23
24
|
require_relative "tronzap/requests/calculate"
|
|
@@ -31,11 +32,20 @@ require_relative "tronzap/requests/aml_history"
|
|
|
31
32
|
require_relative "tronzap/requests/start_subscription"
|
|
32
33
|
require_relative "tronzap/requests/subscription_lookup"
|
|
33
34
|
require_relative "tronzap/requests/subscription_history"
|
|
35
|
+
require_relative "tronzap/requests/bulk_item"
|
|
36
|
+
require_relative "tronzap/requests/bulk_calculate"
|
|
37
|
+
require_relative "tronzap/requests/bulk_create"
|
|
38
|
+
require_relative "tronzap/requests/bulk_lookup"
|
|
39
|
+
require_relative "tronzap/requests/bulk_report"
|
|
40
|
+
require_relative "tronzap/requests/bulk_history"
|
|
34
41
|
require_relative "tronzap/responses/account_balance"
|
|
35
42
|
require_relative "tronzap/responses/address_info"
|
|
36
43
|
require_relative "tronzap/responses/aml_check"
|
|
37
44
|
require_relative "tronzap/responses/aml_history"
|
|
38
45
|
require_relative "tronzap/responses/aml_service"
|
|
46
|
+
require_relative "tronzap/responses/bulk_calculation"
|
|
47
|
+
require_relative "tronzap/responses/bulk_history"
|
|
48
|
+
require_relative "tronzap/responses/bulk_order"
|
|
39
49
|
require_relative "tronzap/responses/calculation"
|
|
40
50
|
require_relative "tronzap/responses/direct_recharge_info"
|
|
41
51
|
require_relative "tronzap/responses/energy_estimate"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tronzap-sdk
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- TronZap
|
|
@@ -49,6 +49,7 @@ files:
|
|
|
49
49
|
- lib/tronzap/models/address_resources.rb
|
|
50
50
|
- lib/tronzap/models/aml_risk_factor.rb
|
|
51
51
|
- lib/tronzap/models/bandwidth_rate.rb
|
|
52
|
+
- lib/tronzap/models/bulk_rejected_row.rb
|
|
52
53
|
- lib/tronzap/models/direct_recharge_rate.rb
|
|
53
54
|
- lib/tronzap/models/energy_rate.rb
|
|
54
55
|
- lib/tronzap/models/enums.rb
|
|
@@ -60,6 +61,12 @@ files:
|
|
|
60
61
|
- lib/tronzap/requests/aml_check.rb
|
|
61
62
|
- lib/tronzap/requests/aml_history.rb
|
|
62
63
|
- lib/tronzap/requests/bandwidth_transaction.rb
|
|
64
|
+
- lib/tronzap/requests/bulk_calculate.rb
|
|
65
|
+
- lib/tronzap/requests/bulk_create.rb
|
|
66
|
+
- lib/tronzap/requests/bulk_history.rb
|
|
67
|
+
- lib/tronzap/requests/bulk_item.rb
|
|
68
|
+
- lib/tronzap/requests/bulk_lookup.rb
|
|
69
|
+
- lib/tronzap/requests/bulk_report.rb
|
|
63
70
|
- lib/tronzap/requests/calculate.rb
|
|
64
71
|
- lib/tronzap/requests/check_transaction.rb
|
|
65
72
|
- lib/tronzap/requests/energy_transaction.rb
|
|
@@ -75,6 +82,9 @@ files:
|
|
|
75
82
|
- lib/tronzap/responses/aml_check.rb
|
|
76
83
|
- lib/tronzap/responses/aml_history.rb
|
|
77
84
|
- lib/tronzap/responses/aml_service.rb
|
|
85
|
+
- lib/tronzap/responses/bulk_calculation.rb
|
|
86
|
+
- lib/tronzap/responses/bulk_history.rb
|
|
87
|
+
- lib/tronzap/responses/bulk_order.rb
|
|
78
88
|
- lib/tronzap/responses/calculation.rb
|
|
79
89
|
- lib/tronzap/responses/direct_recharge_info.rb
|
|
80
90
|
- lib/tronzap/responses/energy_estimate.rb
|