Skip to content

Commit 8cf5e9f

Browse files
refactor(client): deduplicate page response classes (#538)
feat(client): make pagination robust to missing data docs: add comments for page methods
1 parent 5331081 commit 8cf5e9f

File tree

74 files changed

+3191
-3584
lines changed

Some content is hidden

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

74 files changed

+3191
-3584
lines changed

finch-java-core/src/main/kotlin/com/tryfinch/api/models/HrisBenefitIndividualRetrieveManyBenefitsPage.kt

Lines changed: 11 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,7 @@
22

33
package com.tryfinch.api.models
44

5-
import com.fasterxml.jackson.annotation.JsonAnyGetter
6-
import com.fasterxml.jackson.annotation.JsonAnySetter
7-
import com.fasterxml.jackson.annotation.JsonCreator
8-
import com.fasterxml.jackson.annotation.JsonProperty
9-
import com.tryfinch.api.core.ExcludeMissing
10-
import com.tryfinch.api.core.JsonField
11-
import com.tryfinch.api.core.JsonMissing
12-
import com.tryfinch.api.core.JsonValue
13-
import com.tryfinch.api.errors.FinchInvalidDataException
145
import com.tryfinch.api.services.blocking.hris.benefits.IndividualService
15-
import java.util.Collections
166
import java.util.Objects
177
import java.util.Optional
188
import java.util.stream.Stream
@@ -24,33 +14,29 @@ class HrisBenefitIndividualRetrieveManyBenefitsPage
2414
private constructor(
2515
private val individualsService: IndividualService,
2616
private val params: HrisBenefitIndividualRetrieveManyBenefitsParams,
27-
private val response: Response,
17+
private val items: List<IndividualBenefit>,
2818
) {
2919

30-
fun response(): Response = response
31-
32-
fun items(): List<IndividualBenefit> = response().items()
20+
/** Returns the response that this page was parsed from. */
21+
fun items(): List<IndividualBenefit> = items
3322

3423
override fun equals(other: Any?): Boolean {
3524
if (this === other) {
3625
return true
3726
}
3827

39-
return /* spotless:off */ other is HrisBenefitIndividualRetrieveManyBenefitsPage && individualsService == other.individualsService && params == other.params && response == other.response /* spotless:on */
28+
return /* spotless:off */ other is HrisBenefitIndividualRetrieveManyBenefitsPage && individualsService == other.individualsService && params == other.params && items == other.items /* spotless:on */
4029
}
4130

42-
override fun hashCode(): Int = /* spotless:off */ Objects.hash(individualsService, params, response) /* spotless:on */
31+
override fun hashCode(): Int = /* spotless:off */ Objects.hash(individualsService, params, items) /* spotless:on */
4332

4433
override fun toString() =
45-
"HrisBenefitIndividualRetrieveManyBenefitsPage{individualsService=$individualsService, params=$params, response=$response}"
34+
"HrisBenefitIndividualRetrieveManyBenefitsPage{individualsService=$individualsService, params=$params, items=$items}"
4635

47-
fun hasNextPage(): Boolean {
48-
return !items().isEmpty()
49-
}
36+
fun hasNextPage(): Boolean = items.isNotEmpty()
5037

51-
fun getNextPageParams(): Optional<HrisBenefitIndividualRetrieveManyBenefitsParams> {
52-
return Optional.empty()
53-
}
38+
fun getNextPageParams(): Optional<HrisBenefitIndividualRetrieveManyBenefitsParams> =
39+
Optional.empty()
5440

5541
fun getNextPage(): Optional<HrisBenefitIndividualRetrieveManyBenefitsPage> {
5642
return getNextPageParams().map { individualsService.retrieveManyBenefits(it) }
@@ -64,104 +50,8 @@ private constructor(
6450
fun of(
6551
individualsService: IndividualService,
6652
params: HrisBenefitIndividualRetrieveManyBenefitsParams,
67-
response: Response,
68-
) = HrisBenefitIndividualRetrieveManyBenefitsPage(individualsService, params, response)
69-
}
70-
71-
class Response(
72-
private val items: JsonField<List<IndividualBenefit>>,
73-
private val additionalProperties: MutableMap<String, JsonValue>,
74-
) {
75-
76-
@JsonCreator
77-
private constructor(
78-
@JsonProperty("items") items: JsonField<List<IndividualBenefit>> = JsonMissing.of()
79-
) : this(items, mutableMapOf())
80-
81-
fun items(): List<IndividualBenefit> = items.getOptional("items").getOrNull() ?: listOf()
82-
83-
@JsonProperty("items")
84-
fun _items(): Optional<JsonField<List<IndividualBenefit>>> = Optional.ofNullable(items)
85-
86-
@JsonAnySetter
87-
private fun putAdditionalProperty(key: String, value: JsonValue) {
88-
additionalProperties.put(key, value)
89-
}
90-
91-
@JsonAnyGetter
92-
@ExcludeMissing
93-
fun _additionalProperties(): Map<String, JsonValue> =
94-
Collections.unmodifiableMap(additionalProperties)
95-
96-
private var validated: Boolean = false
97-
98-
fun validate(): Response = apply {
99-
if (validated) {
100-
return@apply
101-
}
102-
103-
items().map { it.validate() }
104-
validated = true
105-
}
106-
107-
fun isValid(): Boolean =
108-
try {
109-
validate()
110-
true
111-
} catch (e: FinchInvalidDataException) {
112-
false
113-
}
114-
115-
fun toBuilder() = Builder().from(this)
116-
117-
override fun equals(other: Any?): Boolean {
118-
if (this === other) {
119-
return true
120-
}
121-
122-
return /* spotless:off */ other is Response && items == other.items && additionalProperties == other.additionalProperties /* spotless:on */
123-
}
124-
125-
override fun hashCode(): Int = /* spotless:off */ Objects.hash(items, additionalProperties) /* spotless:on */
126-
127-
override fun toString() =
128-
"Response{items=$items, additionalProperties=$additionalProperties}"
129-
130-
companion object {
131-
132-
/**
133-
* Returns a mutable builder for constructing an instance of
134-
* [HrisBenefitIndividualRetrieveManyBenefitsPage].
135-
*/
136-
@JvmStatic fun builder() = Builder()
137-
}
138-
139-
class Builder {
140-
141-
private var items: JsonField<List<IndividualBenefit>> = JsonMissing.of()
142-
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()
143-
144-
@JvmSynthetic
145-
internal fun from(page: Response) = apply {
146-
this.items = page.items
147-
this.additionalProperties.putAll(page.additionalProperties)
148-
}
149-
150-
fun items(items: List<IndividualBenefit>) = items(JsonField.of(items))
151-
152-
fun items(items: JsonField<List<IndividualBenefit>>) = apply { this.items = items }
153-
154-
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
155-
this.additionalProperties.put(key, value)
156-
}
157-
158-
/**
159-
* Returns an immutable instance of [Response].
160-
*
161-
* Further updates to this [Builder] will not mutate the returned instance.
162-
*/
163-
fun build(): Response = Response(items, additionalProperties.toMutableMap())
164-
}
53+
items: List<IndividualBenefit>,
54+
) = HrisBenefitIndividualRetrieveManyBenefitsPage(individualsService, params, items)
16555
}
16656

16757
class AutoPager(private val firstPage: HrisBenefitIndividualRetrieveManyBenefitsPage) :

finch-java-core/src/main/kotlin/com/tryfinch/api/models/HrisBenefitIndividualRetrieveManyBenefitsPageAsync.kt

Lines changed: 11 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,41 @@
22

33
package com.tryfinch.api.models
44

5-
import com.fasterxml.jackson.annotation.JsonAnyGetter
6-
import com.fasterxml.jackson.annotation.JsonAnySetter
7-
import com.fasterxml.jackson.annotation.JsonCreator
8-
import com.fasterxml.jackson.annotation.JsonProperty
9-
import com.tryfinch.api.core.ExcludeMissing
10-
import com.tryfinch.api.core.JsonField
11-
import com.tryfinch.api.core.JsonMissing
12-
import com.tryfinch.api.core.JsonValue
13-
import com.tryfinch.api.errors.FinchInvalidDataException
145
import com.tryfinch.api.services.async.hris.benefits.IndividualServiceAsync
15-
import java.util.Collections
166
import java.util.Objects
177
import java.util.Optional
188
import java.util.concurrent.CompletableFuture
199
import java.util.concurrent.Executor
2010
import java.util.function.Predicate
21-
import kotlin.jvm.optionals.getOrNull
2211

2312
/** Get enrollment information for the given individuals. */
2413
class HrisBenefitIndividualRetrieveManyBenefitsPageAsync
2514
private constructor(
2615
private val individualsService: IndividualServiceAsync,
2716
private val params: HrisBenefitIndividualRetrieveManyBenefitsParams,
28-
private val response: Response,
17+
private val items: List<IndividualBenefit>,
2918
) {
3019

31-
fun response(): Response = response
32-
33-
fun items(): List<IndividualBenefit> = response().items()
20+
/** Returns the response that this page was parsed from. */
21+
fun items(): List<IndividualBenefit> = items
3422

3523
override fun equals(other: Any?): Boolean {
3624
if (this === other) {
3725
return true
3826
}
3927

40-
return /* spotless:off */ other is HrisBenefitIndividualRetrieveManyBenefitsPageAsync && individualsService == other.individualsService && params == other.params && response == other.response /* spotless:on */
28+
return /* spotless:off */ other is HrisBenefitIndividualRetrieveManyBenefitsPageAsync && individualsService == other.individualsService && params == other.params && items == other.items /* spotless:on */
4129
}
4230

43-
override fun hashCode(): Int = /* spotless:off */ Objects.hash(individualsService, params, response) /* spotless:on */
31+
override fun hashCode(): Int = /* spotless:off */ Objects.hash(individualsService, params, items) /* spotless:on */
4432

4533
override fun toString() =
46-
"HrisBenefitIndividualRetrieveManyBenefitsPageAsync{individualsService=$individualsService, params=$params, response=$response}"
34+
"HrisBenefitIndividualRetrieveManyBenefitsPageAsync{individualsService=$individualsService, params=$params, items=$items}"
4735

48-
fun hasNextPage(): Boolean {
49-
return !items().isEmpty()
50-
}
36+
fun hasNextPage(): Boolean = items.isNotEmpty()
5137

52-
fun getNextPageParams(): Optional<HrisBenefitIndividualRetrieveManyBenefitsParams> {
53-
return Optional.empty()
54-
}
38+
fun getNextPageParams(): Optional<HrisBenefitIndividualRetrieveManyBenefitsParams> =
39+
Optional.empty()
5540

5641
fun getNextPage():
5742
CompletableFuture<Optional<HrisBenefitIndividualRetrieveManyBenefitsPageAsync>> {
@@ -68,104 +53,8 @@ private constructor(
6853
fun of(
6954
individualsService: IndividualServiceAsync,
7055
params: HrisBenefitIndividualRetrieveManyBenefitsParams,
71-
response: Response,
72-
) = HrisBenefitIndividualRetrieveManyBenefitsPageAsync(individualsService, params, response)
73-
}
74-
75-
class Response(
76-
private val items: JsonField<List<IndividualBenefit>>,
77-
private val additionalProperties: MutableMap<String, JsonValue>,
78-
) {
79-
80-
@JsonCreator
81-
private constructor(
82-
@JsonProperty("items") items: JsonField<List<IndividualBenefit>> = JsonMissing.of()
83-
) : this(items, mutableMapOf())
84-
85-
fun items(): List<IndividualBenefit> = items.getOptional("items").getOrNull() ?: listOf()
86-
87-
@JsonProperty("items")
88-
fun _items(): Optional<JsonField<List<IndividualBenefit>>> = Optional.ofNullable(items)
89-
90-
@JsonAnySetter
91-
private fun putAdditionalProperty(key: String, value: JsonValue) {
92-
additionalProperties.put(key, value)
93-
}
94-
95-
@JsonAnyGetter
96-
@ExcludeMissing
97-
fun _additionalProperties(): Map<String, JsonValue> =
98-
Collections.unmodifiableMap(additionalProperties)
99-
100-
private var validated: Boolean = false
101-
102-
fun validate(): Response = apply {
103-
if (validated) {
104-
return@apply
105-
}
106-
107-
items().map { it.validate() }
108-
validated = true
109-
}
110-
111-
fun isValid(): Boolean =
112-
try {
113-
validate()
114-
true
115-
} catch (e: FinchInvalidDataException) {
116-
false
117-
}
118-
119-
fun toBuilder() = Builder().from(this)
120-
121-
override fun equals(other: Any?): Boolean {
122-
if (this === other) {
123-
return true
124-
}
125-
126-
return /* spotless:off */ other is Response && items == other.items && additionalProperties == other.additionalProperties /* spotless:on */
127-
}
128-
129-
override fun hashCode(): Int = /* spotless:off */ Objects.hash(items, additionalProperties) /* spotless:on */
130-
131-
override fun toString() =
132-
"Response{items=$items, additionalProperties=$additionalProperties}"
133-
134-
companion object {
135-
136-
/**
137-
* Returns a mutable builder for constructing an instance of
138-
* [HrisBenefitIndividualRetrieveManyBenefitsPageAsync].
139-
*/
140-
@JvmStatic fun builder() = Builder()
141-
}
142-
143-
class Builder {
144-
145-
private var items: JsonField<List<IndividualBenefit>> = JsonMissing.of()
146-
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()
147-
148-
@JvmSynthetic
149-
internal fun from(page: Response) = apply {
150-
this.items = page.items
151-
this.additionalProperties.putAll(page.additionalProperties)
152-
}
153-
154-
fun items(items: List<IndividualBenefit>) = items(JsonField.of(items))
155-
156-
fun items(items: JsonField<List<IndividualBenefit>>) = apply { this.items = items }
157-
158-
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
159-
this.additionalProperties.put(key, value)
160-
}
161-
162-
/**
163-
* Returns an immutable instance of [Response].
164-
*
165-
* Further updates to this [Builder] will not mutate the returned instance.
166-
*/
167-
fun build(): Response = Response(items, additionalProperties.toMutableMap())
168-
}
56+
items: List<IndividualBenefit>,
57+
) = HrisBenefitIndividualRetrieveManyBenefitsPageAsync(individualsService, params, items)
16958
}
17059

17160
class AutoPager(private val firstPage: HrisBenefitIndividualRetrieveManyBenefitsPageAsync) {

0 commit comments

Comments
 (0)