|
3 | 3 | ## Introduction |
4 | 4 |
|
5 | 5 | PokeKotlin is a modern [Kotlin Multiplatform] client for [PokéAPI]. You can use |
6 | | -it to integrate all sorts of Pokémon data into your Kotlin projects. |
| 6 | +it to integrate all sorts of Pokémon data into your Kotlin, Java, JS, or Swift |
| 7 | +projects. |
7 | 8 |
|
8 | | -Under the hood, it's built on [Ktor], [Kotlin Serialization], and coroutines. |
| 9 | +Under the hood, it's built on [Ktor], [Kotlin Serialization], and [coroutines]. |
9 | 10 |
|
10 | | -## Supported platforms |
| 11 | +## Features |
11 | 12 |
|
12 | | -- Kotlin/JVM, including Android |
13 | | -- Kotlin/JS for browser and Node |
14 | | -- Kotlin/WASM for browser and Node |
15 | | -- Kotlin/Native for Linux, Windows, macOS, iOS, tvOS, and watchOS |
| 13 | +- **Multiplatform**: Works on JVM, Node, the web, and native platforms like iOS, |
| 14 | + macOS, Linux, and Windows. |
| 15 | +- **Caching**: Uses Ktor's built-in caching to reduce API calls and speed up |
| 16 | + responses. |
| 17 | +- **Asynchronous**: Built on coroutines for non-blocking calls. |
16 | 18 |
|
17 | | -## Installation |
| 19 | +## Languages |
18 | 20 |
|
19 | | -This library is published via [Maven Central], and snapshot builds of `main` are |
20 | | -additionally available on [GitHub Packages]. |
| 21 | +### Kotlin |
21 | 22 |
|
22 | | -=== "Releases (Maven Central)" |
| 23 | +On Kotlin, we support all major Kotlin Multiplatform targets, including JVM, JS, |
| 24 | +and native platforms like iOS and macOS. The API is based on `suspend fun` |
| 25 | +calls: |
23 | 26 |
|
24 | | - The latest release is **v{{ gradle.release_version }}**. In your Gradle version catalog, add: |
25 | | - |
26 | | - ```toml title="libs.versions.toml" |
27 | | - [libraries] |
28 | | - pokekotlin = { module = "co.pokeapi.pokekotlin:pokekotlin", version = "{{ gradle.release_version }}" } |
29 | | - ``` |
30 | | - |
31 | | -=== "Snapshots (GitHub Packages)" |
32 | | - |
33 | | - !!! warning |
34 | | - |
35 | | - The published documentation is for the latest release, and may not match the snapshot |
36 | | - version. If using snapshots, always refer to the [latest source code][repo] for the most |
37 | | - accurate information. |
38 | | - |
39 | | - First, follow [GitHub's guide][gh-packages-guide] for authenticating to GitHub Packages. Your |
40 | | - settings.gradle.kts should have something like this: |
41 | | - |
42 | | - ```kotlin title="settings.gradle.kts" |
43 | | - repositories { |
44 | | - maven { |
45 | | - url = uri("https://maven.pkg.github.com/pokeapi/pokekotlin") |
46 | | - credentials { |
47 | | - username = project.findProperty("gpr.user") as String? ?: System.getenv("GH_USERNAME") |
48 | | - password = project.findProperty("gpr.key") as String? ?: System.getenv("GH_TOKEN") |
49 | | - } |
50 | | - } |
| 27 | +```kotlin |
| 28 | +fun main() = runBlocking { |
| 29 | + with(PokeApi) { |
| 30 | + val list = getPokemonSpeciesList(offset = 0, limit = 10) |
| 31 | + for (handle in list.results) { |
| 32 | + val species = handle.get() |
| 33 | + println("Found: $species") |
51 | 34 | } |
52 | | - ``` |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
53 | 38 |
|
54 | | - The latest snapshot is **v{{ gradle.snapshot_version }}**. In your Gradle version catalog, add: |
| 39 | +On JVM targets, the Java APIs below are also available. Similarly, on JS |
| 40 | +targets, the JavaScript APIs below are available. |
55 | 41 |
|
56 | | - ```toml title="libs.versions.toml" |
57 | | - [libraries] |
58 | | - pokekotlin = { module = "co.pokeapi.pokekotlin:pokekotlin", version = "{{ gradle.snapshot_version }}" } |
59 | | - ``` |
| 42 | +### Java |
60 | 43 |
|
61 | | -In your Gradle build script, add: |
| 44 | +For Java, we provide an API based on `CompletableFuture`: |
62 | 45 |
|
63 | | -```kotlin title="build.gradle.kts" |
64 | | -commonMain.dependencies { |
65 | | - implementation(libs.maplibre.compose) |
| 46 | +```java |
| 47 | +public class Example { |
| 48 | + public static void main(String[] args) { |
| 49 | + PokeApi.getPokemonSpeciesListAsync(0, 10).thenAccept(list -> { |
| 50 | + for (NamedHandle<PokemonSpecies> handle : list.getResults()) { |
| 51 | + PokeApi.getAsync(handle).thenAccept(species -> { |
| 52 | + System.out.println("Found: " + species); |
| 53 | + }); |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
66 | 57 | } |
67 | 58 | ``` |
68 | 59 |
|
69 | | -## Usage |
70 | | - |
71 | | -For basic usage, use the global `PokeApi` instance: |
| 60 | +We also provide a synchronous API: |
72 | 61 |
|
73 | | -```kotlin |
74 | | --8<- "src/commonTest/kotlin/co/pokeapi/pokekotlin/example/example.kt:simple" |
| 62 | +```java |
| 63 | +public class Example { |
| 64 | + public static void main(String[] args) { |
| 65 | + PokemonSpeciesList list = PokeApi.getPokemonSpeciesListBlocking(0, 10); |
| 66 | + for (NamedHandle<PokemonSpecies> handle : list.getResults()) { |
| 67 | + PokemonSpecies species = PokeApi.getBlocking(handle); |
| 68 | + System.out.println("Found: " + species); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
75 | 72 | ``` |
76 | 73 |
|
77 | | -By default, the client will connect to the official `https://pokeapi.co/` |
78 | | -instance and cache results in memory. |
| 74 | +### JS |
79 | 75 |
|
80 | | -If you want to customize the client, create a custom instance of the client: |
| 76 | +For JavaScript, we provide an ESM module with TypeScript typings and a |
| 77 | +`Promise`-based API that works in browsers and in Node: |
81 | 78 |
|
82 | | -```kotlin |
83 | | --8<- "src/commonTest/kotlin/co/pokeapi/pokekotlin/example/example.kt:custom" |
| 79 | +```typescript |
| 80 | +import { PokeApi } from "@pokeapi/pokekotlin"; |
| 81 | + |
| 82 | +const list = await PokeApi.Default.getPokemonSpeciesListAsync(0, 10); |
| 83 | +for (const handle of list.results) { |
| 84 | + const species = await PokeApi.Default.getAsync(handle); |
| 85 | + console.log(`Found: ${species}`); |
| 86 | +} |
84 | 87 | ``` |
85 | 88 |
|
86 | | -For further details, see the Dokka [API Reference](./api). |
| 89 | +### Swift |
| 90 | + |
| 91 | +The Swift package is not yet published. |
87 | 92 |
|
88 | 93 | [Kotlin Multiplatform]: https://kotlinlang.org/docs/multiplatform.html |
89 | 94 | [PokéAPI]: https://pokeapi.co/ |
90 | | -[Maven Central]: https://central.sonatype.com/namespace/co.pokeapi.pokekotlin |
91 | | -[GitHub Packages]: |
92 | | - https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-gradle-registry |
93 | | -[gh-packages-guide]: |
94 | | - https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-gradle-registry#using-a-published-package |
95 | | -[repo]: https://github.com/pokeapi/pokekotlin |
96 | 95 | [Ktor]: https://ktor.io/ |
97 | 96 | [Kotlin Serialization]: https://github.com/Kotlin/kotlinx.serialization |
98 | 97 | [coroutines]: https://kotlinlang.org/docs/coroutines-guide.html |
0 commit comments