📡 Metody HTTP

KNET obsługuje wszystkie standardowe metody HTTP z prostym i intuicyjnym API.

Przegląd metod

Metoda Użycie Body Opis
GET client.get(url) ❌ Pobieranie zasobów
POST client.post(url, data) ✅ Tworzenie nowych zasobów
PUT client.put(url, data) ✅ Pełna aktualizacja zasobu
PATCH client.patch(url, data) ✅ Częściowa aktualizacja
DELETE client.delete(url) ❌ Usuwanie zasobów
HEAD client.head(url) ❌ Tylko nagłówki (bez body)
OPTIONS client.options(url) ❌ Opcje dostępne na serwerze

GET - Pobieranie danych

// Prosty GET
val response = client.get("https://api.example.com/users")

// Z query parameters
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

// Z nagłówkami
val response = client.get(
    url = "https://api.example.com/users",
    headers = mapOf("Authorization" to "Bearer $token")
)

// Safe API
client.getSafe("https://api.example.com/users")
    .onSuccess { response -> processUsers(response) }
    .onFailure { error -> handleError(error) }

POST - Tworzenie zasobów

// POST z Map (automatycznie jako JSON)
val response = client.post(
    url = "https://api.example.com/users",
    data = mapOf(
        "name" to "Jan Kowalski",
        "email" to "jan@example.com",
        "age" to 30
    )
)

// POST z obiektem (automatyczna serializacja)
data class CreateUserRequest(val name: String, val email: String)

val response = client.post(
    url = "https://api.example.com/users",
    body = CreateUserRequest("Jan", "jan@example.com")
)

// POST z raw JSON string
val response = client.post(
    url = "https://api.example.com/users",
    data = """{"name":"Jan","email":"jan@example.com"}""",
    contentType = "application/json"
)

// POST form-urlencoded
val request = KNETRequest(
    url = "https://api.example.com/login",
    method = "POST",
    data = "username=jan&password=secret",
    headers = mapOf("Content-Type" to "application/x-www-form-urlencoded")
)
val response = client.request(request)

PUT - Pełna aktualizacja

// PUT zastępuje cały zasób
val response = client.put(
    url = "https://api.example.com/users/123",
    data = mapOf(
        "id" to 123,
        "name" to "Jan Nowak",      // zmienione
        "email" to "jan@example.com",
        "age" to 31                  // zmienione
    )
)

// PUT z obiektem
val response = client.put(
    url = "https://api.example.com/users/123",
    body = updatedUser
)

PATCH - Częściowa aktualizacja

// PATCH aktualizuje tylko podane pola
val response = client.patch(
    url = "https://api.example.com/users/123",
    data = mapOf(
        "email" to "nowy.email@example.com"  // tylko to pole
    )
)

// JSON Patch (RFC 6902)
val response = client.patch(
    url = "https://api.example.com/users/123",
    data = listOf(
        mapOf("op" to "replace", "path" to "/email", "value" to "new@example.com")
    )
)

DELETE - Usuwanie

// Proste DELETE
val response = client.delete("https://api.example.com/users/123")

// DELETE z nagłówkami
val response = client.delete(
    url = "https://api.example.com/users/123",
    headers = mapOf("Authorization" to "Bearer $token")
)

// Sprawdź wynik
if (response.statusCode == 204) {
    println("Użytkownik usunięty")
}

HEAD - Tylko nagłówki

// HEAD zwraca tylko nagłówki (bez body)
val response = client.head("https://api.example.com/large-file.zip")

// Przydatne do sprawdzenia:
val contentLength = response.headers["Content-Length"]?.toLongOrNull()
val lastModified = response.headers["Last-Modified"]
val etag = response.headers["ETag"]

println("Rozmiar pliku: ${contentLength?.div(1024)} KB")
println("Ostatnia modyfikacja: $lastModified")

OPTIONS - Preflight

// OPTIONS sprawdza dostępne metody
val response = client.options("https://api.example.com/users")

val allowedMethods = response.headers["Allow"]
// "GET, POST, PUT, DELETE, OPTIONS"

val corsHeaders = response.headers["Access-Control-Allow-Methods"]
// "GET, POST, PUT, DELETE"

Custom Request

// Dla niestandardowych metod lub pełnej kontroli
val request = KNETRequest(
    url = "https://api.example.com/resource",
    method = "CUSTOM_METHOD",
    headers = mapOf(
        "Authorization" to "Bearer $token",
        "X-Custom-Header" to "value"
    ),
    query = mutableMapOf("param" to "value"),
    data = mapOf("key" to "value"),
    timeout = 60_000,
    tag = "my-request-tag"
)

val response = client.request(request)

📚 Zobacz też