Repository navigation
Expand file tree
/
Copy pathCreditsGrants.java
More file actions
406 lines (382 loc) · 17 KB
/
Copy pathCreditsGrants.java
File metadata and controls
406 lines (382 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// this file is @generated
package com.meteroid.api;
import com.meteroid.ApiResponse;
import com.meteroid.RequestOptions;
import com.meteroid.internal.MeteroidHttpClient;
import com.meteroid.internal.Utils;
import com.meteroid.models.CreateCreditGrantRequest;
import com.meteroid.models.CreditGrant;
import com.meteroid.models.CreditGrantListResponse;
import com.meteroid.models.CreditGrantStatus;
import com.meteroid.models.Currency;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import java.util.List;
import java.util.Objects;
/**
* The {@code credits.grants} operations, blocking. {@link #withRawResponse()} has the same methods
* returning the status and headers along with the body.
*/
public final class CreditsGrants {
private final MeteroidHttpClient client;
private final WithRawResponse withRawResponse;
/**
* The operations, sending through {@code client}.
*
* @param client the HTTP client of the SDK
*/
public CreditsGrants(MeteroidHttpClient client) {
this.client = client;
this.withRawResponse = new WithRawResponse();
}
/**
* The same operations, returning the status and headers along with the body.
*
* @return the operations
*/
public WithRawResponse withRawResponse() {
return withRawResponse;
}
/**
* List credit grants
*
* <p>A customer's credit grants, newest first, in every currency and credit unit unless
* filtered.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @return the page: the response body, its items and the way to the next pages
*/
public CreditsGrantsListPage list(final String idOrAlias) {
return list(idOrAlias, CreditsGrantsListOptions.none(), RequestOptions.none());
}
/**
* List credit grants
*
* <p>A customer's credit grants, newest first, in every currency and credit unit unless
* filtered.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @param options the optional parameters
* @return the page: the response body, its items and the way to the next pages
*/
public CreditsGrantsListPage list(
final String idOrAlias, final CreditsGrantsListOptions options) {
return list(idOrAlias, options, RequestOptions.none());
}
/**
* List credit grants
*
* <p>A customer's credit grants, newest first, in every currency and credit unit unless
* filtered.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @param requestOptions headers, timeout and retries of this call
* @return the page: the response body, its items and the way to the next pages
*/
public CreditsGrantsListPage list(final String idOrAlias, final RequestOptions requestOptions) {
return list(idOrAlias, CreditsGrantsListOptions.none(), requestOptions);
}
/**
* List credit grants
*
* <p>A customer's credit grants, newest first, in every currency and credit unit unless
* filtered.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @param options the optional parameters
* @param requestOptions headers, timeout and retries of this call
* @return the page: the response body, its items and the way to the next pages
*/
public CreditsGrantsListPage list(
final String idOrAlias,
final CreditsGrantsListOptions options,
final RequestOptions requestOptions) {
return pageOfList(
exchangeList(idOrAlias, options, requestOptions).send(),
idOrAlias,
options,
requestOptions);
}
MeteroidHttpClient.Exchange<CreditGrantListResponse> exchangeList(
final String idOrAlias,
final CreditsGrantsListOptions options,
final RequestOptions requestOptions) {
Objects.requireNonNull(idOrAlias, "id_or_alias");
Objects.requireNonNull(options, "options");
HttpUrl.Builder url =
client.newUrlBuilder()
.addPathSegments("api/v1/customers")
.addPathSegment(Utils.pathSegment("id_or_alias", idOrAlias))
.addPathSegments("credit-grants");
Currency value1 = options.currency().orElse(null);
if (value1 != null) {
url.addQueryParameter("currency", Utils.serializeQueryParam(value1));
}
String value2 = options.creditUnit().orElse(null);
if (value2 != null) {
url.addQueryParameter("credit_unit", value2);
}
CreditGrantStatus value3 = options.status().orElse(null);
if (value3 != null) {
url.addQueryParameter("status", Utils.serializeQueryParam(value3));
}
String value4 = options.subscriptionId().orElse(null);
if (value4 != null) {
url.addQueryParameter("subscription_id", value4);
}
String value5 = options.usableBySubscriptionId().orElse(null);
if (value5 != null) {
url.addQueryParameter("usable_by_subscription_id", value5);
}
Integer value6 = options.page().orElse(null);
if (value6 != null) {
url.addQueryParameter("page", Utils.serializeQueryParam(value6));
}
Integer value7 = options.perPage().orElse(null);
if (value7 != null) {
url.addQueryParameter("per_page", Utils.serializeQueryParam(value7));
}
return client.call("GET", url.build())
.errors(
com.meteroid.models.RestErrorResponse.class,
"400",
"401",
"404",
"429",
"500")
.options(requestOptions)
.returning(CreditGrantListResponse.class);
}
private CreditsGrantsListPage pageOfList(
CreditGrantListResponse response,
final String idOrAlias,
final CreditsGrantsListOptions options,
final RequestOptions requestOptions) {
List<CreditGrant> items = itemsOfList(response);
Integer next = nextOfList(response, items, options.page().orElse(0));
return new CreditsGrantsListPage(
response,
items,
next == null
? null
: () ->
list(
idOrAlias,
options.toBuilder().page(next).build(),
requestOptions));
}
static List<CreditGrant> itemsOfList(CreditGrantListResponse response) {
return Utils.optional(response.data()).orElse(List.of());
}
/** The parameter of the page after {@code response}, null after the last one. */
static Integer nextOfList(
CreditGrantListResponse response, List<CreditGrant> items, Integer current) {
if (items.isEmpty()) {
return null;
}
long pages =
Utils.optional(response.paginationMeta())
.flatMap(v2 -> Utils.optional(v2.totalPages()))
.map(Number::longValue)
.orElse(Long.MAX_VALUE);
if (current - 0 + 1 >= pages) {
return null;
}
return current + 1;
}
/**
* Create a credit grant
*
* <p>Grants credit without charging or invoicing: PROMOTIONAL credit for free, or PAID credit
* the customer paid for outside Meteroid ("Paid outside Meteroid"). A PAID currency grant whose
* <code>cost</code> is below its <code>amount</code> is split into a PAID grant for the cost
* and a PROMOTIONAL bonus grant for the rest, returned as <code>bonus_grant_id</code>; a <code>
* cost</code> above the <code>amount</code> is refused. For a customer of a French invoicing
* entity, PAID credit that cost more than zero needs a <code>reference</code> to the invoice or
* receipt of its payment. Sell credit through Meteroid with a top-up.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @param idempotencyKey the {@code Idempotency-Key} required parameter
* @param createCreditGrantRequest the request body
* @return the response body
*/
public CreditGrant create(
final String idOrAlias,
final String idempotencyKey,
final CreateCreditGrantRequest createCreditGrantRequest) {
return create(idOrAlias, idempotencyKey, createCreditGrantRequest, RequestOptions.none());
}
/**
* Create a credit grant
*
* <p>Grants credit without charging or invoicing: PROMOTIONAL credit for free, or PAID credit
* the customer paid for outside Meteroid ("Paid outside Meteroid"). A PAID currency grant whose
* <code>cost</code> is below its <code>amount</code> is split into a PAID grant for the cost
* and a PROMOTIONAL bonus grant for the rest, returned as <code>bonus_grant_id</code>; a <code>
* cost</code> above the <code>amount</code> is refused. For a customer of a French invoicing
* entity, PAID credit that cost more than zero needs a <code>reference</code> to the invoice or
* receipt of its payment. Sell credit through Meteroid with a top-up.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @param idempotencyKey the {@code Idempotency-Key} required parameter
* @param createCreditGrantRequest the request body
* @param requestOptions headers, timeout and retries of this call
* @return the response body
*/
public CreditGrant create(
final String idOrAlias,
final String idempotencyKey,
final CreateCreditGrantRequest createCreditGrantRequest,
final RequestOptions requestOptions) {
return exchangeCreate(idOrAlias, idempotencyKey, createCreditGrantRequest, requestOptions)
.send();
}
MeteroidHttpClient.Exchange<CreditGrant> exchangeCreate(
final String idOrAlias,
final String idempotencyKey,
final CreateCreditGrantRequest createCreditGrantRequest,
final RequestOptions requestOptions) {
Objects.requireNonNull(idOrAlias, "id_or_alias");
Objects.requireNonNull(idempotencyKey, "Idempotency-Key");
Objects.requireNonNull(createCreditGrantRequest, "body");
HttpUrl url =
client.newUrlBuilder()
.addPathSegments("api/v1/customers")
.addPathSegment(Utils.pathSegment("id_or_alias", idOrAlias))
.addPathSegments("credit-grants")
.build();
Headers.Builder headers = new Headers.Builder();
headers.set("Idempotency-Key", Utils.serializeQueryParam(idempotencyKey));
return client.call("POST", url)
.headers(headers.build())
.json(createCreditGrantRequest)
.errors(
com.meteroid.models.RestErrorResponse.class,
"400",
"401",
"404",
"409",
"429",
"500")
.options(requestOptions)
.returning(CreditGrant.class);
}
/** The operations, returning the status and headers along with the body. */
public final class WithRawResponse {
private WithRawResponse() {}
/**
* List credit grants
*
* <p>A customer's credit grants, newest first, in every currency and credit unit unless
* filtered.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @return the status, headers and page
*/
public ApiResponse<CreditsGrantsListPage> list(final String idOrAlias) {
return list(idOrAlias, CreditsGrantsListOptions.none(), RequestOptions.none());
}
/**
* List credit grants
*
* <p>A customer's credit grants, newest first, in every currency and credit unit unless
* filtered.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @param options the optional parameters
* @return the status, headers and page
*/
public ApiResponse<CreditsGrantsListPage> list(
final String idOrAlias, final CreditsGrantsListOptions options) {
return list(idOrAlias, options, RequestOptions.none());
}
/**
* List credit grants
*
* <p>A customer's credit grants, newest first, in every currency and credit unit unless
* filtered.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @param requestOptions headers, timeout and retries of this call
* @return the status, headers and page
*/
public ApiResponse<CreditsGrantsListPage> list(
final String idOrAlias, final RequestOptions requestOptions) {
return list(idOrAlias, CreditsGrantsListOptions.none(), requestOptions);
}
/**
* List credit grants
*
* <p>A customer's credit grants, newest first, in every currency and credit unit unless
* filtered.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @param options the optional parameters
* @param requestOptions headers, timeout and retries of this call
* @return the status, headers and page
*/
public ApiResponse<CreditsGrantsListPage> list(
final String idOrAlias,
final CreditsGrantsListOptions options,
final RequestOptions requestOptions) {
ApiResponse<CreditGrantListResponse> response =
exchangeList(idOrAlias, options, requestOptions).sendRaw();
return new ApiResponse<>(
response.statusCode(),
response.headers(),
pageOfList(response.body(), idOrAlias, options, requestOptions));
}
/**
* Create a credit grant
*
* <p>Grants credit without charging or invoicing: PROMOTIONAL credit for free, or PAID
* credit the customer paid for outside Meteroid ("Paid outside Meteroid"). A PAID currency
* grant whose <code>cost</code> is below its <code>amount</code> is split into a PAID grant
* for the cost and a PROMOTIONAL bonus grant for the rest, returned as <code>bonus_grant_id
* </code>; a <code>cost</code> above the <code>amount</code> is refused. For a customer of
* a French invoicing entity, PAID credit that cost more than zero needs a <code>reference
* </code> to the invoice or receipt of its payment. Sell credit through Meteroid with a
* top-up.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @param idempotencyKey the {@code Idempotency-Key} required parameter
* @param createCreditGrantRequest the request body
* @return the status, headers and body
*/
public ApiResponse<CreditGrant> create(
final String idOrAlias,
final String idempotencyKey,
final CreateCreditGrantRequest createCreditGrantRequest) {
return create(
idOrAlias, idempotencyKey, createCreditGrantRequest, RequestOptions.none());
}
/**
* Create a credit grant
*
* <p>Grants credit without charging or invoicing: PROMOTIONAL credit for free, or PAID
* credit the customer paid for outside Meteroid ("Paid outside Meteroid"). A PAID currency
* grant whose <code>cost</code> is below its <code>amount</code> is split into a PAID grant
* for the cost and a PROMOTIONAL bonus grant for the rest, returned as <code>bonus_grant_id
* </code>; a <code>cost</code> above the <code>amount</code> is refused. For a customer of
* a French invoicing entity, PAID credit that cost more than zero needs a <code>reference
* </code> to the invoice or receipt of its payment. Sell credit through Meteroid with a
* top-up.
*
* @param idOrAlias the {@code id_or_alias} path parameter
* @param idempotencyKey the {@code Idempotency-Key} required parameter
* @param createCreditGrantRequest the request body
* @param requestOptions headers, timeout and retries of this call
* @return the status, headers and body
*/
public ApiResponse<CreditGrant> create(
final String idOrAlias,
final String idempotencyKey,
final CreateCreditGrantRequest createCreditGrantRequest,
final RequestOptions requestOptions) {
return CreditsGrants.this
.exchangeCreate(
idOrAlias, idempotencyKey, createCreditGrantRequest, requestOptions)
.sendRaw();
}
}
}