fetchRockets module for solution application.
Simple library in Kotlin Multiplatform Mobile that provides REST API requests.
Those are used in our example app in Quanti mobile-assignment.
KMM is used for both iOS (Swift) and Android (Kotlin) versions of mobile development. It is a modern way of creating shared code for apps.
This project offers use cases of RocketClient. Such use cases do the work of creating API requests, serializing (parsing) data from json to struct/class and then providing the data to platform-specific projects.
fetchAllRockets(): RocketResult<List<RocketKMM>>
fetchRocketById(rocketId: String): RocketResutl<RocketKMM>
fetchFailRockets: RocketResult<RocketException> // Made for testing error handlingRocketKMM is basically a DTO model for rocket API - https://api.spacexdata.com/v4/rockets/.
- Additional information is available in the SpaceX API.
RocketResult is custom result type (Success and Failure) used because Swift cannot handle built-in Result type and casts it as a Any?.
- All functions are using a
@NativeCoroutinesmodifier provided via special library: KMP-Native-Coroutines. - The library basically creates "new" functions, that are thread-safe. Those functions are called differently.
Calling basic functions in Swift is very easy, just declare the struct and then use the functions like so:
let rocketApi = RocketApi()
let rockets = try await rocketApi.fetchAllRockets()and handle errors in do-catch clause.
Functions in NativeCoroutines are handled this way:
let rockets = try await asyncFunction(for: rocketApi.fetchAllRockets())- Note that the
asyncFunctionis from the NativeCoroutines library that needs to be imported.
do {
let rockets = try await asyncFunction(for: rocketApi.fetchAllRockets())
//MARK: Even though warning is saying "always fails" it in fact does not fail at all. Swift is confused about KMM. - Ignore this warrning
switch rockets {
case let success as RocketResultSuccess<AnyObject>:
//Custom mapping into domain model
case let failure as RocketResult<RocketException>:
//Custoom error mapping to domain error
default:
throw DomainError.undefinedError
}
} catch {
throw error
}