Ten przewodnik pokaże Ci podstawy KNET w 5 minut.
import rip.nerd.kitsunenet.core.KNETClient
// Utwórz klienta (lub użyj KNETClient.default)
val client = KNETClient()
// Wykonaj GET request
lifecycleScope.launch {
val response = client.get("https://jsonplaceholder.typicode.com/users/1")
println("Status: ${response.statusCode}") // 200
println("Body: ${response.bodyString}") // JSON string
println("Czas: ${response.responseTimeMs}ms") // 150
}
// POST z Map
val response = client.post(
url = "https://jsonplaceholder.typicode.com/posts",
data = mapOf(
"title" to "Mój post",
"body" to "Treść posta",
"userId" to 1
)
)
// Automatycznie:
// - Content-Type: application/json
// - Body jako JSON
val response = client.get("https://api.example.com/user/1")
// Jako Map
val user = response.jsonObject()
println(user["name"]) // "Jan Kowalski"
println(user["email"]) // "jan@example.com"
// Bezpieczny dostęp do zagnieżdżonych pól
val city = response.jsonPath("address.city") // "Warszawa"
data class User(
val id: Int,
val name: String,
val email: String
)
val user = response.json<User>()
println(user.name) // "Jan Kowalski"
val response = client.get("https://api.example.com/users")
val users = response.jsonList<User>()
users.forEach { user ->
println("${user.id}: ${user.name}")
}
client.getSafe("https://api.example.com/users")
.onSuccess { response ->
// Sukces!
val users = response.jsonList<User>()
updateUI(users)
}
.onFailure { error ->
// Błąd
when (error) {
is KNETError.NetworkError -> showOfflineMessage()
is KNETError.HttpError -> showHttpError(error.statusCode)
is KNETError.TimeoutError -> showTimeoutMessage()
else -> showGenericError(error.message)
}
}
val response = client.get(
url = "https://api.example.com/protected",
headers = mapOf(
"Authorization" to "Bearer $token",
"Accept" to "application/json",
"X-Custom-Header" to "value"
)
)
val response = client.get(
url = "https://api.example.com/users",
query = mapOf(
"page" to "1",
"limit" to "20",
"sort" to "name"
)
)
// URL: https://api.example.com/users?page=1&limit=20&sort=name
// Ręcznie tworzony request
val request = KNETRequest(
url = "https://api.example.com/users",
method = "POST",
headers = mapOf("Authorization" to "Bearer $token"),
data = mapOf("name" to "Jan"),
timeout = 60_000
)
val response = client.request(request)
val getRequest = KNETRequest.get("https://api.example.com/users")
val postRequest = KNETRequest.post("https://api.example.com/users", jsonBody)
val deleteRequest = KNETRequest.delete("https://api.example.com/users/1")
import rip.nerd.kitsunenet.dsl.knet
import rip.nerd.kitsunenet.dsl.POST
val response = knet {
url = "https://api.example.com/users"
method = POST
headers {
authorization("Bearer $token")
contentType("application/json")
accept("application/json")
}
body {
json(mapOf(
"name" to "Jan",
"email" to "jan@example.com"
))
}
timeout = 30_000
}
// Dla kodu nie-coroutine
client.requestAsync(
request = KNETRequest.get("https://api.example.com/users"),
onSuccess = { response ->
runOnUiThread {
updateUI(response.bodyString)
}
},
onError = { error ->
runOnUiThread {
showError(error.message)
}
},
onComplete = {
hideLoading()
}
)
val client = KNETClient.builder()
.defaultTimeout(30_000)
.addInterceptor(KNETLoggingInterceptor())
.addInterceptor(KNETHeaderInterceptor.bearer(token))
.addInterceptor(KNETRetryInterceptor(maxRetries = 3))
.build()
| Operacja | Kod |
|---|---|
| GET | client.get(url) |
| POST | client.post(url, data) |
| PUT | client.put(url, data) |
| DELETE | client.delete(url) |
| JSON → Map | response.jsonObject() |
| JSON → Object | response.json<T>() |
| Safe API | client.getSafe(url).onSuccess {}.onFailure {} |