PokemonKit is a Swift package wrapping PokeAPI. It uses Foundation's URLSession and Swift's Result type. Callbacks can easily be transformed into Combine Futures.
Add .package(url: "https://github.com/nityanandaz/PokemonKit.git", from: "5.0.0")
to your dependencies.
Example: Fetch berry with id 1.
import PokemonKit
Berry.fetch(id: "1") { (result) in
if case let .success(berry) = result {
print(berry.name)
}
}
Example: Create a Combine Future.
import Combine
import PokemonKit
let favoriteVersion = Future(Version.fetch(id: "alpha-sapphire"))
favoriteVersion.subscribe(...)
Set PokemonKit.baseURL
to another URL, e.g., if you want to host your own instance of PokeAPI.
Execute
swift run gyb > Sources/PokemonKit/Types+Resource.swift
to renew Types+Resource.swift
.
Types+Resource.swift.stencil
is a template to generate all protocol conformances to Resource
that are listed in resource-conformance.csv
with Stencil.
InternalTests.swift
sets intentionally PokemonKit.baseURL
to a localhost URL.
If there are any errors decoding a Resource, let me know by creating an issue. I am happy to fix that.
- Nityananda Zbil
- forked from Yeung Yiu Hung