@@ -2,10 +2,46 @@ module Travis::API::V3
2
2
class BillingClient
3
3
class ConfigurationError < StandardError ; end
4
4
5
+ ALLOWANCE_TIMEOUT = 1 # second
6
+
5
7
def initialize ( user_id )
6
8
@user_id = user_id
7
9
end
8
10
11
+ def allowance ( owner_type , owner_id )
12
+ response = connection ( timeout : ALLOWANCE_TIMEOUT ) . get ( "/usage/#{ owner_type . downcase } s/#{ owner_id } /allowance" )
13
+ return BillingClient . default_allowance_response unless response . status == 200
14
+
15
+ Travis ::API ::V3 ::Models ::Allowance . new ( 2 , owner_id , response . body )
16
+ end
17
+
18
+ def authorize_build ( repo , sender_id , jobs )
19
+ response = connection . post ( "/#{ repo . owner . class . name . downcase . pluralize } /#{ repo . owner . id } /authorize_build" , { repository : { private : repo . private? } , sender_id : sender_id , jobs : jobs } )
20
+ handle_errors_and_respond ( response )
21
+ end
22
+
23
+ def self . default_allowance_response ( id = 0 )
24
+ Travis ::API ::V3 ::Models ::Allowance . new ( 1 , id , {
25
+ "public_repos" => true ,
26
+ "private_repos" => false ,
27
+ "concurrency_limit" => 1 ,
28
+ "user_usage" => false ,
29
+ "pending_user_licenses" => false
30
+ } . freeze )
31
+ end
32
+
33
+ def self . minimal_allowance_response ( id = 0 )
34
+ Travis ::API ::V3 ::Models ::Allowance . new ( 2 , id , { } )
35
+ end
36
+
37
+ def executions ( owner_type , owner_id , page , per_page , from , to )
38
+ response = connection . get ( "/usage/#{ owner_type . downcase } s/#{ owner_id } /executions?page=#{ page } &per_page=#{ per_page } &from=#{ from } &to=#{ to } " )
39
+ executions = response . body . map do |execution_data |
40
+ Travis ::API ::V3 ::Models ::Execution . new ( execution_data )
41
+ end
42
+ executions
43
+ end
44
+
9
45
def all
10
46
data = connection . get ( '/subscriptions' ) . body
11
47
subscriptions = data . fetch ( 'subscriptions' ) . map do |subscription_data |
@@ -16,17 +52,38 @@ def all
16
52
Travis ::API ::V3 ::Models ::SubscriptionsCollection . new ( subscriptions , permissions )
17
53
end
18
54
55
+ def all_v2
56
+ data = connection . get ( '/v2/subscriptions' ) . body
57
+ subscriptions = data . fetch ( 'plans' ) . map do |subscription_data |
58
+ Travis ::API ::V3 ::Models ::V2Subscription . new ( subscription_data )
59
+ end
60
+ permissions = data . fetch ( 'permissions' )
61
+
62
+ Travis ::API ::V3 ::Models ::SubscriptionsCollection . new ( subscriptions , permissions )
63
+ end
64
+
19
65
def get_subscription ( id )
20
66
response = connection . get ( "/subscriptions/#{ id } " )
21
67
handle_subscription_response ( response )
22
68
end
23
69
70
+ def get_v2_subscription ( id )
71
+ response = connection . get ( "/v2/subscriptions/#{ id } " )
72
+ handle_v2_subscription_response ( response )
73
+ end
74
+
24
75
def get_invoices_for_subscription ( id )
25
76
connection . get ( "/subscriptions/#{ id } /invoices" ) . body . map do |invoice_data |
26
77
Travis ::API ::V3 ::Models ::Invoice . new ( invoice_data )
27
78
end
28
79
end
29
80
81
+ def get_invoices_for_v2_subscription ( id )
82
+ connection . get ( "/v2/subscriptions/#{ id } /invoices" ) . body . map do |invoice_data |
83
+ Travis ::API ::V3 ::Models ::Invoice . new ( invoice_data )
84
+ end
85
+ end
86
+
30
87
def trials
31
88
connection . get ( '/trials' ) . body . map do | trial_data |
32
89
Travis ::API ::V3 ::Models ::Trial . new ( trial_data )
@@ -43,11 +100,21 @@ def update_address(subscription_id, address_data)
43
100
handle_subscription_response ( response )
44
101
end
45
102
103
+ def update_v2_address ( subscription_id , address_data )
104
+ response = connection . patch ( "/v2/subscriptions/#{ subscription_id } /address" , address_data )
105
+ handle_v2_subscription_response ( response )
106
+ end
107
+
46
108
def update_creditcard ( subscription_id , creditcard_token )
47
109
response = connection . patch ( "/subscriptions/#{ subscription_id } /creditcard" , token : creditcard_token )
48
110
handle_subscription_response ( response )
49
111
end
50
112
113
+ def update_v2_creditcard ( subscription_id , creditcard_token )
114
+ response = connection . patch ( "/v2/subscriptions/#{ subscription_id } /creditcard" , token : creditcard_token )
115
+ handle_v2_subscription_response ( response )
116
+ end
117
+
51
118
def update_plan ( subscription_id , plan_data )
52
119
response = connection . patch ( "/subscriptions/#{ subscription_id } /plan" , plan_data )
53
120
handle_subscription_response ( response )
@@ -58,6 +125,44 @@ def create_subscription(subscription_data)
58
125
handle_subscription_response ( response )
59
126
end
60
127
128
+ def create_v2_subscription ( subscription_data )
129
+ response = connection . post ( '/v2/subscriptions' , subscription_data )
130
+ handle_v2_subscription_response ( response )
131
+ end
132
+
133
+ def changetofree_v2_subscription ( subscription_id , data )
134
+ response = connection . patch ( "/v2/subscriptions/#{ subscription_id } /changetofree" , data )
135
+ handle_v2_subscription_response ( response )
136
+ end
137
+
138
+ def update_v2_subscription ( subscription_id , plan_data )
139
+ response = connection . patch ( "/v2/subscriptions/#{ subscription_id } /plan" , plan_data )
140
+ handle_v2_subscription_response ( response )
141
+ end
142
+
143
+ def purchase_addon ( subscription_id , addon_config_id )
144
+ response = connection . patch ( "/v2/subscriptions/#{ subscription_id } /addon" , { addon : addon_config_id } )
145
+ handle_v2_subscription_response ( response )
146
+ end
147
+
148
+ def v2_subscription_user_usages ( subscription_id )
149
+ connection . get ( "/v2/subscriptions/#{ subscription_id } /user_usage" ) . body . map do |usage_data |
150
+ Travis ::API ::V3 ::Models ::V2AddonUsage . new ( usage_data )
151
+ end
152
+ end
153
+
154
+ def v2_plans_for_organization ( organization_id )
155
+ connection . get ( "/v2/plans_for/organization/#{ organization_id } " ) . body . map do |plan_data |
156
+ Travis ::API ::V3 ::Models ::V2PlanConfig . new ( plan_data )
157
+ end
158
+ end
159
+
160
+ def v2_plans_for_user
161
+ connection . get ( '/v2/plans_for/user' ) . body . map do |plan_data |
162
+ Travis ::API ::V3 ::Models ::V2PlanConfig . new ( plan_data )
163
+ end
164
+ end
165
+
61
166
def cancel_subscription ( id , reason_data )
62
167
response = connection . post ( "/subscriptions/#{ id } /cancel" , reason_data )
63
168
handle_subscription_response ( response )
@@ -70,7 +175,7 @@ def plans_for_organization(organization_id)
70
175
end
71
176
72
177
def plans_for_user
73
- connection . get ( " /plans_for/user" ) . body . map do |plan_data |
178
+ connection . get ( ' /plans_for/user' ) . body . map do |plan_data |
74
179
Travis ::API ::V3 ::Models ::Plan . new ( plan_data )
75
180
end
76
181
end
@@ -85,6 +190,11 @@ def pay(id)
85
190
handle_subscription_response ( response )
86
191
end
87
192
193
+ def pay_v2 ( id )
194
+ response = connection . post ( "/v2/subscriptions/#{ id } /pay" )
195
+ handle_v2_subscription_response ( response )
196
+ end
197
+
88
198
def get_coupon ( code )
89
199
response = connection . get ( "/coupons/#{ code } " )
90
200
handle_coupon_response ( response )
@@ -101,6 +211,10 @@ def handle_subscription_response(response)
101
211
handle_errors_and_respond ( response ) { |r | Travis ::API ::V3 ::Models ::Subscription . new ( r ) }
102
212
end
103
213
214
+ def handle_v2_subscription_response ( response )
215
+ handle_errors_and_respond ( response ) { |r | Travis ::API ::V3 ::Models ::V2Subscription . new ( r ) }
216
+ end
217
+
104
218
def handle_coupon_response ( response )
105
219
handle_errors_and_respond ( response ) { |r | Travis ::API ::V3 ::Models ::Coupon . new ( r ) }
106
220
end
@@ -113,24 +227,28 @@ def handle_errors_and_respond(response)
113
227
true
114
228
when 204
115
229
true
116
- when 404
117
- raise Travis ::API ::V3 ::NotFound , response . body [ 'error' ]
118
230
when 400
119
231
raise Travis ::API ::V3 ::ClientError , response . body [ 'error' ]
232
+ when 403
233
+ raise Travis ::API ::V3 ::InsufficientAccess , response . body [ 'rejection_code' ]
234
+ when 404
235
+ raise Travis ::API ::V3 ::NotFound , response . body [ 'error' ]
120
236
when 422
121
237
raise Travis ::API ::V3 ::UnprocessableEntity , response . body [ 'error' ]
122
238
else
123
239
raise Travis ::API ::V3 ::ServerError , 'Billing system failed'
124
240
end
125
241
end
126
242
127
- def connection
243
+ def connection ( timeout : 10 )
128
244
@connection ||= Faraday . new ( url : billing_url , ssl : { ca_path : '/usr/lib/ssl/certs' } ) do |conn |
129
245
conn . basic_auth '_' , billing_auth_key
130
246
conn . headers [ 'X-Travis-User-Id' ] = @user_id . to_s
131
247
conn . headers [ 'Content-Type' ] = 'application/json'
132
248
conn . request :json
133
249
conn . response :json
250
+ conn . options [ :open_timeout ] = timeout
251
+ conn . options [ :timeout ] = timeout
134
252
conn . use OpenCensus ::Trace ::Integrations ::FaradayMiddleware if Travis ::Api ::App ::Middleware ::OpenCensus . enabled?
135
253
conn . adapter :net_http
136
254
end
0 commit comments