At the moment we already have an initial project (You can check Part 1 if you don’t) and now we can progress with an API integration.

NOTE: This part will be useful for Android devs, iOS part will be handled in the future

So, we will build an application that can pull data from https://openweathermap.org/api and show a toast with it on an application. This part will cover working with Ktor client.

What is Ktor?

Ktor is a framework for building asynchronous servers and clients in connected systems using the powerful Kotlin programming language. The main advantage of Ktor in comparison with the other — it supports multiplatform, so you could use it inside the Shared module. We are interested in Ktor client.

Integration with Weather API

Ok, as we learned from the previous part that the Shared module has different sourceSets with their own dependency sets. So, inside you shared build.gradle you need to add 2 new dependencies: Ktor and coroutines (Yeah, coroutines supports multiplatform as well)

apply plugin: 'kotlin-multiplatform' kotlin {

targets {

fromPreset(presets.jvm, 'android')

} sourceSets {

commonMain.dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib-common:1.3.10" //It is expected interfaces

implementation "io.ktor:ktor-client:1.0.0"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.0.1"

} androidMain.dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.10" //It is actual platform implementations

implementation "io.ktor:ktor-client-android:1.0.0"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1"

}

}

}

You can find all possible dependencies in the multiplatform section https://ktor.io/clients/http-client/multiplatform.html

Create WeatherApi file inside the Shared-> commonMain sourceSet

class WeatherApi() { private val client = HttpClient() suspend fun fetchWeather(): String {

return client.get<String> {

url("$baseUrl/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22")

}

} companion object {

private const val baseUrl = "https://samples.openweathermap.org"

} }

Let’s move to the main android module, here you need to add the internet permission inside the manifest

<uses-permission android:name="android.permission.INTERNET"/>

and coroutines dependency to build.gradle

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1"

And finally, import WeatherApi and you will have something like that

class MainActivity : AppCompatActivity(), CoroutineScope { private val job = Job() override val coroutineContext: CoroutineContext

get() = job override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)



val weatherApi = WeatherApi()

launch(Dispatchers.Main) {

try {

val result = withContext(Dispatchers.IO) { weatherApi.fetchWeather() }

Toast.makeText(this@MainActivity, result, Toast.LENGTH_LONG).show()

} catch (e: Exception) {

Toast.makeText(this@MainActivity, e.message, Toast.LENGTH_LONG).show()

}

}

}

}

NOTE: I tried to simplify everything here to keep it clear, don’t do like that, use DI to inject API