Kotlin SDK
Last updated · Changelog · OpenAPI spec · Markdown
Our official Kotlin SDK provides native Android integration with coroutines support and modern Kotlin idioms.
Installation
// build.gradle.kts
dependencies {
implementation("com.shopsavvy:shopsavvy-sdk-kotlin:1.1.0")
}Quick Start
import com.shopsavvy.sdk.*
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
// Create a new client
val client = ShopSavvyClient("ss_live_your_api_key_here")
try {
// Get product details
val product = client.getProductDetails("012345678901")
println("Product: ${product.data.name}")
// Get current offers
val offers = client.getCurrentOffers("012345678901")
offers.data.forEach { offer ->
println("${offer.retailer}: $${offer.price}")
}
// Get price history
val history = client.getPriceHistory(
"012345678901",
"2024-01-01",
"2024-01-31"
)
} catch (e: Exception) {
println("Error: ${e.message}")
}
}Android Integration
class ProductRepository(private val client: ShopSavvyClient) {
suspend fun getProduct(barcode: String): Result<Product> =
try {
val response = client.getProductDetails(barcode)
Result.success(response.data)
} catch (e: Exception) {
Result.failure(e)
}
}
// In your ViewModel
class ProductViewModel(
private val repository: ProductRepository
) : ViewModel() {
private val _product = MutableLiveData<Product?>()
val product: LiveData<Product?> = _product
fun loadProduct(barcode: String) {
viewModelScope.launch {
repository.getProduct(barcode)
.onSuccess { _product.value = it }
.onFailure { /* handle error */ }
}
}
}Features
- Full coroutines support
- Null safety with Kotlin types
- Android lifecycle awareness
- OkHttp-based HTTP client
- Serialization with kotlinx.serialization
- Support for Android API 21+
Was this page helpful?