Skip to content

Commit 70694bc

Browse files
feat(client): allow providing some params positionally
1 parent 51c85db commit 70694bc

142 files changed

Lines changed: 8581 additions & 1967 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,7 @@ import app.knock.api.core.http.HttpResponseFor;
188188
import app.knock.api.models.users.User;
189189
import app.knock.api.models.users.UserGetParams;
190190

191-
UserGetParams params = UserGetParams.builder()
192-
.userId("dnedry")
193-
.build();
194-
HttpResponseFor<User> user = client.users().withRawResponse().get(params);
191+
HttpResponseFor<User> user = client.users().withRawResponse().get("dnedry");
195192

196193
int statusCode = user.statusCode();
197194
Headers headers = user.headers();
@@ -463,7 +460,6 @@ import app.knock.api.core.JsonValue;
463460
import app.knock.api.models.workflows.WorkflowTriggerParams;
464461

465462
WorkflowTriggerParams params = WorkflowTriggerParams.builder()
466-
.key("dinosaurs-loose")
467463
.recipients(JsonValue.from(42))
468464
.data(WorkflowTriggerParams.Data.builder()
469465
.putAdditionalProperty("dinosaur", JsonValue.from("triceratops"))

knock-java-core/src/main/kotlin/app/knock/api/core/Check.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package app.knock.api.core
55
import com.fasterxml.jackson.core.Version
66
import com.fasterxml.jackson.core.util.VersionUtil
77

8+
fun checkRequired(name: String, condition: Boolean) =
9+
check(condition) { "`$name` is required, but was not set" }
10+
811
fun <T : Any> checkRequired(name: String, value: T?): T =
912
checkNotNull(value) { "`$name` is required, but was not set" }
1013

knock-java-core/src/main/kotlin/app/knock/api/models/audiences/AudienceAddMembersParams.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import kotlin.jvm.optionals.getOrNull
2525
/** Adds one or more members to the specified audience. */
2626
class AudienceAddMembersParams
2727
private constructor(
28-
private val key: String,
28+
private val key: String?,
2929
private val body: Body,
3030
private val additionalHeaders: Headers,
3131
private val additionalQueryParams: QueryParams,
3232
) : Params {
3333

34-
fun key(): String = key
34+
fun key(): Optional<String> = Optional.ofNullable(key)
3535

3636
/**
3737
* A list of audience members to add.
@@ -63,7 +63,6 @@ private constructor(
6363
*
6464
* The following fields are required:
6565
* ```java
66-
* .key()
6766
* .members()
6867
* ```
6968
*/
@@ -86,7 +85,10 @@ private constructor(
8685
additionalQueryParams = audienceAddMembersParams.additionalQueryParams.toBuilder()
8786
}
8887

89-
fun key(key: String) = apply { this.key = key }
88+
fun key(key: String?) = apply { this.key = key }
89+
90+
/** Alias for calling [Builder.key] with `key.orElse(null)`. */
91+
fun key(key: Optional<String>) = key(key.getOrNull())
9092

9193
/**
9294
* Sets the entire request body.
@@ -240,15 +242,14 @@ private constructor(
240242
*
241243
* The following fields are required:
242244
* ```java
243-
* .key()
244245
* .members()
245246
* ```
246247
*
247248
* @throws IllegalStateException if any required field is unset.
248249
*/
249250
fun build(): AudienceAddMembersParams =
250251
AudienceAddMembersParams(
251-
checkRequired("key", key),
252+
key,
252253
body.build(),
253254
additionalHeaders.build(),
254255
additionalQueryParams.build(),
@@ -259,7 +260,7 @@ private constructor(
259260

260261
fun _pathParam(index: Int): String =
261262
when (index) {
262-
0 -> key
263+
0 -> key ?: ""
263264
else -> ""
264265
}
265266

knock-java-core/src/main/kotlin/app/knock/api/models/audiences/AudienceListMembersParams.kt

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
package app.knock.api.models.audiences
44

55
import app.knock.api.core.Params
6-
import app.knock.api.core.checkRequired
76
import app.knock.api.core.http.Headers
87
import app.knock.api.core.http.QueryParams
98
import java.util.Objects
9+
import java.util.Optional
10+
import kotlin.jvm.optionals.getOrNull
1011

1112
/** Returns a paginated list of members for the specified audience. */
1213
class AudienceListMembersParams
1314
private constructor(
14-
private val key: String,
15+
private val key: String?,
1516
private val additionalHeaders: Headers,
1617
private val additionalQueryParams: QueryParams,
1718
) : Params {
1819

19-
fun key(): String = key
20+
fun key(): Optional<String> = Optional.ofNullable(key)
2021

2122
fun _additionalHeaders(): Headers = additionalHeaders
2223

@@ -26,13 +27,10 @@ private constructor(
2627

2728
companion object {
2829

30+
@JvmStatic fun none(): AudienceListMembersParams = builder().build()
31+
2932
/**
3033
* Returns a mutable builder for constructing an instance of [AudienceListMembersParams].
31-
*
32-
* The following fields are required:
33-
* ```java
34-
* .key()
35-
* ```
3634
*/
3735
@JvmStatic fun builder() = Builder()
3836
}
@@ -51,7 +49,10 @@ private constructor(
5149
additionalQueryParams = audienceListMembersParams.additionalQueryParams.toBuilder()
5250
}
5351

54-
fun key(key: String) = apply { this.key = key }
52+
fun key(key: String?) = apply { this.key = key }
53+
54+
/** Alias for calling [Builder.key] with `key.orElse(null)`. */
55+
fun key(key: Optional<String>) = key(key.getOrNull())
5556

5657
fun additionalHeaders(additionalHeaders: Headers) = apply {
5758
this.additionalHeaders.clear()
@@ -155,25 +156,14 @@ private constructor(
155156
* Returns an immutable instance of [AudienceListMembersParams].
156157
*
157158
* Further updates to this [Builder] will not mutate the returned instance.
158-
*
159-
* The following fields are required:
160-
* ```java
161-
* .key()
162-
* ```
163-
*
164-
* @throws IllegalStateException if any required field is unset.
165159
*/
166160
fun build(): AudienceListMembersParams =
167-
AudienceListMembersParams(
168-
checkRequired("key", key),
169-
additionalHeaders.build(),
170-
additionalQueryParams.build(),
171-
)
161+
AudienceListMembersParams(key, additionalHeaders.build(), additionalQueryParams.build())
172162
}
173163

174164
fun _pathParam(index: Int): String =
175165
when (index) {
176-
0 -> key
166+
0 -> key ?: ""
177167
else -> ""
178168
}
179169

knock-java-core/src/main/kotlin/app/knock/api/models/audiences/AudienceRemoveMembersParams.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import kotlin.jvm.optionals.getOrNull
2525
/** Removes one or more members from the specified audience. */
2626
class AudienceRemoveMembersParams
2727
private constructor(
28-
private val key: String,
28+
private val key: String?,
2929
private val body: Body,
3030
private val additionalHeaders: Headers,
3131
private val additionalQueryParams: QueryParams,
3232
) : Params {
3333

34-
fun key(): String = key
34+
fun key(): Optional<String> = Optional.ofNullable(key)
3535

3636
/**
3737
* A list of audience members to remove.
@@ -63,7 +63,6 @@ private constructor(
6363
*
6464
* The following fields are required:
6565
* ```java
66-
* .key()
6766
* .members()
6867
* ```
6968
*/
@@ -86,7 +85,10 @@ private constructor(
8685
additionalQueryParams = audienceRemoveMembersParams.additionalQueryParams.toBuilder()
8786
}
8887

89-
fun key(key: String) = apply { this.key = key }
88+
fun key(key: String?) = apply { this.key = key }
89+
90+
/** Alias for calling [Builder.key] with `key.orElse(null)`. */
91+
fun key(key: Optional<String>) = key(key.getOrNull())
9092

9193
/**
9294
* Sets the entire request body.
@@ -240,15 +242,14 @@ private constructor(
240242
*
241243
* The following fields are required:
242244
* ```java
243-
* .key()
244245
* .members()
245246
* ```
246247
*
247248
* @throws IllegalStateException if any required field is unset.
248249
*/
249250
fun build(): AudienceRemoveMembersParams =
250251
AudienceRemoveMembersParams(
251-
checkRequired("key", key),
252+
key,
252253
body.build(),
253254
additionalHeaders.build(),
254255
additionalQueryParams.build(),
@@ -259,7 +260,7 @@ private constructor(
259260

260261
fun _pathParam(index: Int): String =
261262
when (index) {
262-
0 -> key
263+
0 -> key ?: ""
263264
else -> ""
264265
}
265266

knock-java-core/src/main/kotlin/app/knock/api/models/bulkoperations/BulkOperationGetParams.kt

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
package app.knock.api.models.bulkoperations
44

55
import app.knock.api.core.Params
6-
import app.knock.api.core.checkRequired
76
import app.knock.api.core.http.Headers
87
import app.knock.api.core.http.QueryParams
98
import java.util.Objects
9+
import java.util.Optional
10+
import kotlin.jvm.optionals.getOrNull
1011

1112
/** Retrieves a bulk operation (if it exists) and displays the current state of it. */
1213
class BulkOperationGetParams
1314
private constructor(
14-
private val id: String,
15+
private val id: String?,
1516
private val additionalHeaders: Headers,
1617
private val additionalQueryParams: QueryParams,
1718
) : Params {
1819

19-
fun id(): String = id
20+
fun id(): Optional<String> = Optional.ofNullable(id)
2021

2122
fun _additionalHeaders(): Headers = additionalHeaders
2223

@@ -26,14 +27,9 @@ private constructor(
2627

2728
companion object {
2829

29-
/**
30-
* Returns a mutable builder for constructing an instance of [BulkOperationGetParams].
31-
*
32-
* The following fields are required:
33-
* ```java
34-
* .id()
35-
* ```
36-
*/
30+
@JvmStatic fun none(): BulkOperationGetParams = builder().build()
31+
32+
/** Returns a mutable builder for constructing an instance of [BulkOperationGetParams]. */
3733
@JvmStatic fun builder() = Builder()
3834
}
3935

@@ -51,7 +47,10 @@ private constructor(
5147
additionalQueryParams = bulkOperationGetParams.additionalQueryParams.toBuilder()
5248
}
5349

54-
fun id(id: String) = apply { this.id = id }
50+
fun id(id: String?) = apply { this.id = id }
51+
52+
/** Alias for calling [Builder.id] with `id.orElse(null)`. */
53+
fun id(id: Optional<String>) = id(id.getOrNull())
5554

5655
fun additionalHeaders(additionalHeaders: Headers) = apply {
5756
this.additionalHeaders.clear()
@@ -155,25 +154,14 @@ private constructor(
155154
* Returns an immutable instance of [BulkOperationGetParams].
156155
*
157156
* Further updates to this [Builder] will not mutate the returned instance.
158-
*
159-
* The following fields are required:
160-
* ```java
161-
* .id()
162-
* ```
163-
*
164-
* @throws IllegalStateException if any required field is unset.
165157
*/
166158
fun build(): BulkOperationGetParams =
167-
BulkOperationGetParams(
168-
checkRequired("id", id),
169-
additionalHeaders.build(),
170-
additionalQueryParams.build(),
171-
)
159+
BulkOperationGetParams(id, additionalHeaders.build(), additionalQueryParams.build())
172160
}
173161

174162
fun _pathParam(index: Int): String =
175163
when (index) {
176-
0 -> id
164+
0 -> id ?: ""
177165
else -> ""
178166
}
179167

0 commit comments

Comments
 (0)