Skip to content

Commit f6a3a55

Browse files
committed
docs: improve readme examples and text
1 parent 1b812d8 commit f6a3a55

78 files changed

Lines changed: 308 additions & 228 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: 27 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Lithic Java API Library
22

3-
The Lithic Java SDK provides convenient access to the Lithic REST API from applications written in Java or Kotlin. It includes helper classes with helpful types and documentation for every request and response property.
3+
The Lithic Java SDK provides convenient access to the Lithic REST API from applications written in Java. It includes helper classes with helpful types and documentation for every request and response property.
44

55
## Documentation
66

@@ -12,12 +12,6 @@ The API documentation can be found [here](https://docs.lithic.com).
1212

1313
### Install dependencies
1414

15-
During private beta, this package is not published to a public repository. To make it available as a dependency, you can either publish it to your private repository or make it available in local maven with:
16-
17-
```sh
18-
./gradlew publishToMavenLocal
19-
```
20-
2115
#### Gradle
2216

2317
```kotlin
@@ -52,9 +46,7 @@ Alternately, set the environment variable `LITHIC_API_KEY` and use `LithicClient
5246
LithicClient client = LithicClient.fromEnv();
5347
```
5448

55-
A `LithicInvalidDataException` will be thrown if a required client property is not provided.
56-
57-
Read the documentation for more configuration options. See [Dependency injection](#dependency-injection) below for advice on injecting Lithic services into your application classes.
49+
Read the documentation for more configuration options.
5850

5951
---
6052

@@ -110,7 +102,7 @@ card.builder().state(Card.State.CLOSED).build();
110102
```
111103

112104
Over time, the Lithic API may add new values to the property that are not yet represented by the enum type in
113-
this SDK. If an unrecognized value is found, the enum is set to a special sentinel value `_UNKNOWN` and you can use `value` to read the string that was received:
105+
this SDK. If an unrecognized value is found, the enum is set to a special sentinel value `_UNKNOWN` and you can use `asString` to read the string that was received:
114106

115107
```java
116108
switch (card.state().value()) {
@@ -123,7 +115,7 @@ switch (card.state().value()) {
123115
return;
124116
case Card.State.Value._UNKNOWN:
125117
// ... handle unrecognized enum value as string
126-
String cardState = card.state().value();
118+
String cardState = card.state().asString();
127119
return;
128120
}
129121
```
@@ -144,41 +136,20 @@ In [Example: creating a resource](#example-creating-a-resource) above, we used t
144136
the `create` method of the `cards` service.
145137

146138
Sometimes, the API may support other properties that are not yet supported in the Java SDK types. In that case,
147-
you can attach them using the `additionalProperty` method.
139+
you can attach them using the `putAdditionalProperty` method.
148140

149141
```java
150142
CardCreateParams params = CardCreateParams.builder()
151143
// ... normal properties
152-
.additionalProperty("secret_param", "4242")
144+
.putAdditionalProperty("secret_param", "4242")
153145
.build();
154146
```
155147

156-
### (Kotlin) Shorthand syntax
157-
158-
For building entities from Kotlin, you can use the following shorthand syntax:
159-
160-
```kotlin
161-
val params = CardCreateParams {
162-
type = CardCreateParams.Type.VIRTUAL
163-
}
164-
```
165-
166-
The shorthand syntax is also supported directly when passing parameters to the API methods:
167-
168-
```kotlin
169-
// No need to create an explicit CardCreateParams instance!
170-
val page = client.cards().create {
171-
type = Type.VIRTUAL
172-
};
173-
```
174-
175148
## Responses
176149

177150
### Response validation
178151

179-
When receiving a response, the Lithic Java SDK tries to deserialize it into instances of the well-typed model classes. If the API returns a response property that doesn't match the expected type, the deserialization will still succeed and model instances will be created. However, if the client code tries to access a property that had an invalid type, a `LithicInvalidDataException` will be thrown.
180-
181-
If you would like to immediately raise an exception if a response contains at least one unexpected type, you can call `.validate()` on the returned model.
152+
When receiving a response, the Lithic Java SDK will deserialize it into instances of the typed model classes. In rare cases, the API may return a response property that doesn't match the expected Java type. If you directly access the mistaken property, the SDK will throw an unchecked `LithicInvalidDataException` at runtime. If you would prefer to check in advance that that response is completely well-typed, call `.validate()` on the returned model.
182153

183154
```java
184155
Card card = client.cards().create().validate();
@@ -193,12 +164,30 @@ Model properties that are optional or allow a null value are represented as `Opt
193164
card.cvv().isPresent(); // false
194165
```
195166

167+
### Response properties as JSON
168+
169+
In rare cases, you may want to access the underlying JSON value for a response property rather than using the typed version provided by
170+
this SDK. Each model property has a corresponding JSON version, with an underscore before the method name, which returns a `JsonField` value.
171+
172+
```java
173+
JsonField state() = card._state();
174+
175+
if (state().isMissing()) {
176+
// Value was not specified in the JSON response
177+
} else if (state().isNull()) {
178+
// Value was provided as a literall null
179+
} else {
180+
// See if value was provided as a string
181+
Optional<String> jsonString = state().asString();
182+
}
183+
```
184+
196185
### Additional model properties
197186

198-
Sometimes, the server response may include additional properties that are not yet available in this library's types. You can access them using the model's `additionalProperties` map:
187+
Sometimes, the server response may include additional properties that are not yet available in this library's types. You can access them using the model's `_additionalProperties` method:
199188

200189
```java
201-
String secret = card.additionalProperties().get("secret_field");
190+
String secret = card._additionalProperties().get("secret_field");
202191
```
203192

204193
---
@@ -298,43 +287,4 @@ Requests are made to the production environment by default. You can connect to o
298287

299288
```java
300289
LithicClient client = LithicClient.builder().fromEnv().sandbox().build()
301-
```
302-
303-
---
304-
305-
## Dependency injection
306-
307-
If you're using a dependency injection framework and are providing a `LithicClient`
308-
instance to it, then you can also directly inject a `CardService` instead of calling `client.create()`.
309-
310-
### Details and example
311-
312-
For your convenience, the client library is designed to work with all modern dependency injection frameworks that support `javax.inject` annotations, which includes Spring, Dagger and Guice. You can simply inject the services in your code as you do with all other dependencies. Here's an example with constructor injection:
313-
314-
```java
315-
public class YourClass {
316-
private final CardService cardsService;
317-
318-
@javax.inject.Inject
319-
YourClass(CardService cardsService) {
320-
this.cardsService = cardsService;
321-
}
322-
323-
void yourMethod() {
324-
Card card = cardsService.create();
325-
}
326-
}
327-
```
328-
329-
For this to work, you need to provide a `LithicClient` instance to your dependency injection framework. Here's an example for Dagger:
330-
331-
```java
332-
@Module
333-
class YourDaggerModule {
334-
@Provides
335-
@Singleton
336-
static LithicClient LithicClient() {
337-
return LithicClient.create("<your private API key">);
338-
}
339-
}
340290
```

lithic-java-core/src/main/kotlin/com/lithic/api/core/Values.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ sealed class JsonField<out T : Any> {
5959
else -> Optional.empty()
6060
}
6161

62+
fun asStringOrThrow(): String =
63+
when (this) {
64+
is JsonString -> value
65+
else -> throw LithicInvalidDataException("Value is not a string")
66+
}
67+
6268
fun asArray(): Optional<List<JsonValue>> =
6369
when (this) {
6470
is JsonArray -> Optional.of(values)

lithic-java-core/src/main/kotlin/com/lithic/api/errors/LithicError.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class LithicError constructor(private val additionalProperties: Map<String, Json
4444
}
4545

4646
@JsonAnySetter
47-
fun putAdditionalProperties(key: String, value: JsonValue) = apply {
47+
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
4848
this.additionalProperties.put(key, value)
4949
}
5050

lithic-java-core/src/main/kotlin/com/lithic/api/models/Account.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.lithic.api.core.JsonMissing
1111
import com.lithic.api.core.JsonValue
1212
import com.lithic.api.core.NoAutoDetect
1313
import com.lithic.api.core.toUnmodifiable
14+
import com.lithic.api.errors.LithicInvalidDataException
1415
import java.util.Objects
1516
import java.util.Optional
1617

@@ -212,7 +213,7 @@ private constructor(
212213
}
213214

214215
@JsonAnySetter
215-
fun putAdditionalProperties(key: String, value: JsonValue) = apply {
216+
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
216217
this.additionalProperties.put(key, value)
217218
}
218219

@@ -358,7 +359,7 @@ private constructor(
358359
}
359360

360361
@JsonAnySetter
361-
fun putAdditionalProperties(key: String, value: JsonValue) = apply {
362+
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
362363
this.additionalProperties.put(key, value)
363364
}
364365

@@ -421,7 +422,9 @@ private constructor(
421422
when (this) {
422423
ACTIVE -> Known.ACTIVE
423424
PAUSED -> Known.PAUSED
424-
else -> throw IllegalArgumentException("Unknown Account.State: $value")
425+
else -> throw LithicInvalidDataException("Unknown Account.State: $value")
425426
}
427+
428+
fun asString(): String = _value().asStringOrThrow()
426429
}
427430
}

lithic-java-core/src/main/kotlin/com/lithic/api/models/AccountHolder.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.lithic.api.core.JsonMissing
1111
import com.lithic.api.core.JsonValue
1212
import com.lithic.api.core.NoAutoDetect
1313
import com.lithic.api.core.toUnmodifiable
14+
import com.lithic.api.errors.LithicInvalidDataException
1415
import java.util.Objects
1516
import java.util.Optional
1617

@@ -183,7 +184,7 @@ private constructor(
183184
}
184185

185186
@JsonAnySetter
186-
fun putAdditionalProperties(key: String, value: JsonValue) = apply {
187+
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
187188
this.additionalProperties.put(key, value)
188189
}
189190

@@ -258,8 +259,10 @@ private constructor(
258259
REJECTED -> Known.REJECTED
259260
PENDING_RESUBMIT -> Known.PENDING_RESUBMIT
260261
PENDING_DOCUMENT -> Known.PENDING_DOCUMENT
261-
else -> throw IllegalArgumentException("Unknown AccountHolder.Status: $value")
262+
else -> throw LithicInvalidDataException("Unknown AccountHolder.Status: $value")
262263
}
264+
265+
fun asString(): String = _value().asStringOrThrow()
263266
}
264267

265268
class StatusReason @JsonCreator private constructor(private val value: JsonField<String>) {
@@ -375,7 +378,10 @@ private constructor(
375378
OTHER_VERIFICATION_FAILURE -> Known.OTHER_VERIFICATION_FAILURE
376379
RISK_THRESHOLD_FAILURE -> Known.RISK_THRESHOLD_FAILURE
377380
WATCHLIST_ALERT_FAILURE -> Known.WATCHLIST_ALERT_FAILURE
378-
else -> throw IllegalArgumentException("Unknown AccountHolder.StatusReason: $value")
381+
else ->
382+
throw LithicInvalidDataException("Unknown AccountHolder.StatusReason: $value")
379383
}
384+
385+
fun asString(): String = _value().asStringOrThrow()
380386
}
381387
}

0 commit comments

Comments
 (0)