← Back to notebook index

Notebook Topic

Networking Apis

English topic guide inspired by the original iOS Developer Notebook, expanded for both iOS and Android teams.

What this topic covers

Execution checklist

iOS implementation notes

Android implementation notes

Code examples

Swift

func fetch(_ request: URLRequest, as type: T.Type) async throws -> T {
    let (data, response) = try await URLSession.shared.data(for: request)
    guard let http = response as? HTTPURLResponse, 200..<300 ~= http.statusCode else {
        throw URLError(.badServerResponse)
    }
    return try JSONDecoder().decode(T.self, from: data)
}

Kotlin

suspend inline fun  apiCall(crossinline block: suspend () -> Response): T {
    val res = block()
    if (!res.isSuccessful) throw HttpException(res)
    return res.body() ?: error("Empty body")
}

Reference links

Related topics