docs(examples): rewrite the consumer examples as teaching material#15
Conversation
|
The author of this PR, MarketDataDev03, is not an activated member of this organization on Codecov. |
| */ | ||
| fun main() { | ||
| // `use {}` closes the client when the block ends — the Kotlin equivalent of try-with-resources. | ||
| MarketDataClient().use { client -> |
There was a problem hiding this comment.
The no-arg MarketDataClient() constructor runs with validateOnStartup=true and fires a real /user/ probe — so with a present-but-invalid/expired token it throws AuthenticationError here, before .use {} is entered. That means the catch (e: AuthenticationError) inside the lambda never fires, and the example dies with an uncaught stack trace instead of printing the friendly "Set MARKETDATA_TOKEN" hint.
The Java examples avoid this because try (MarketDataClient client = new MarketDataClient()) puts construction inside the try-with-resources, so their identical catch fires. Of all the examples, the Kotlin interop showcase is the one that crashes ugly on the most common misconfiguration — and the repo's own .env may carry a stale dev token.
Suggest wrapping construction inside the try so the catch covers it:
try {
MarketDataClient().use { client ->
val quote = client.stocks().quote(StockQuoteRequest.of("AAPL")).values()[0]
println("Sync: ${quote.symbol()} last=${quote.last()}")
client.stocks()
.quoteAsync(StockQuoteRequest.of("AAPL"))
.thenAccept { resp -> println("Async: ${resp.values()[0].last()}") }
.join()
}
} catch (e: AuthenticationError) {
println("Set MARKETDATA_TOKEN (env var or .env) to run this example.")
}There was a problem hiding this comment.
Good catch, i'm going to fix it
docs(examples): rewrite the consumer examples as teaching material
The old
examples/consumer-test/apps doubled as an external verification harness: they scripteda mock server, asserted contracts (peak-in-flight == 50, every exception subtype, retry timing), and
wrapped each SDK call in
Consolehelpers and per-call try/catch. Great for catching regressions,but the actual SDK call was buried — not something a consumer could read and copy.
This PR replaces them with examples that teach. Each file is a small, runnable program optimized
for reading: plain
System.out.println, a single top-leveltry, the most common calls perresource, and short comments explaining why. They consume the published artifact via
mavenLocal,so what you read is the real public API. Net −1921 lines.
What changed
resources/). One conciseXExampleper resource (Utilities, Stocks,Options, Funds, Markets) showing the first calls you'd write, plus an
XAdvancedExampleeach forthe less common surface — universal params, date windows, column projection, CSV output, sealed
chain filters. All run against the live API.
common/), covered once rather than repeated per resource:SyncVsAsyncExample(live),ConcurrentRequestsExample(the 50-permit concurrency cap),RetryAndBackoffExample(auto retry +Retry-After),ErrorHandlingExample(the sealedMarketDataExceptionhierarchy),ConfigurationExample(cascade, redaction, fail-fast — offline),ResponseFormatsExample(the response wrapper surface).src/main/kotlin/.../Quickstart.kt) — the same SDK from Kotlin, sync +async. The examples module gains the
kotlin("jvm")plugin; the SDK artifact stays Java-only(ADR-001 is untouched — this is a consumer module, not the published JAR).
MockServerteaching aid — trimmed from the 235-lineMockServerControl(dropped theStats/assert framing). It exists only so the three behavior examples are runnable without a paidtoken, and is explicitly not part of the SDK.
*Appclasses +Console+MockServerControl. Rewrotebuild.gradle.kts(16 run tasks under theexamplesgroup), the rootMakefile(example-*targets), and the module
README.mdas an index/guide.Design principles
purpose; that's what the unit/integration suites are for.
(concurrency, retry, error types). Resource examples run live.
common/, not duplicated across resources.ParseError, strict-default) weredropped from the examples — those were contract tests, not teaching.
Verification
Every example was run and its output checked — not just compiled.
examples/consumer-test/.env): all resource examples + advanced variants +sync/async + response formats + the Kotlin quickstart returned real market data (e.g. AAPL candles,
a 5-call option chain with greeks, the exchange calendar correctly flagging the Juneteenth
holiday).
503→503→200in ~3 s and honoredRetry-After: 3; error handling routed the sealed hierarchy andprinted the support-info dump.
limit()doesn't trimcandles (the count contradicted the label); CSV date columns rendered raw unix epochs (added
dateFormat→ readable dates, also confirming it composes with theasCsv()facet); aRateLimitError.getRetryAfter()printed asOptional[PT5S](unwrapped to5s)../gradlew buildgreen (Java + Kotlin).SDK impact
None. This PR touches only
examples/, the rootMakefile, and the examplesREADME. Nosrc/changes, no effect on the published artifact.
Commit
be8cda6