Skip to content

Commit

Permalink
Add networking example using Ktor (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gchristov committed Oct 11, 2022
1 parent b549091 commit 5bc0a1c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
package com.gchristov.thecodinglove

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

external fun require(module:String) : dynamic
external var exports: dynamic

fun main(args: Array<String>) {
val fireFunctions = require("firebase-functions")
exports.myTestFun = fireFunctions.https.onRequest { request, response ->
response.send(Messenger().message())
val client = provideHttpClient()
GlobalScope.launch {
val userResponse: Response = client.get("https://reqres.in/api/users").body()
response.send(Messenger().message() + ", " + userResponse.page)
}
}
}

private fun provideHttpClient() = HttpClient {
install(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
}
)
}
}
install(Logging) {
logger = Logger.SIMPLE
level = LogLevel.ALL
}
}

@Serializable
private data class Response(
val page: Int
)
1 change: 1 addition & 0 deletions gradle-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ repositories {

dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20-RC")
implementation("org.jetbrains.kotlin:kotlin-serialization:1.7.20-RC")
implementation("dev.petuska:npm-publish-gradle-plugin:3.0.3")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.gchristov.thecodinglove.gradleplugins

class Deps {
object Ktor {
private const val ktorVersion = "2.1.2"
const val client = "io.ktor:ktor-client-core:$ktorVersion"
const val contentNegotiation = "io.ktor:ktor-client-content-negotiation:$ktorVersion"
const val serialisation = "io.ktor:ktor-serialization-kotlinx-json:$ktorVersion"
const val logging = "io.ktor:ktor-client-logging:$ktorVersion"
const val logback = "ch.qos.logback:logback-classic:1.2.10"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ abstract class JavascriptPlatformPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.configureJavascript()
target.configureTests()
target.configureNetwork()
}
}

Expand Down Expand Up @@ -46,4 +47,17 @@ private fun Project.configureTests() {
implementation(kotlin("test"))
}
}
}

private fun Project.configureNetwork() {
plugins.apply("org.jetbrains.kotlin.plugin.serialization")
extensions.configure(KotlinMultiplatformExtension::class.java) {
sourceSets.maybeCreate("commonMain").dependencies {
implementation(Deps.Ktor.client)
implementation(Deps.Ktor.contentNegotiation)
implementation(Deps.Ktor.serialisation)
implementation(Deps.Ktor.logging)
implementation(Deps.Ktor.logback)
}
}
}
7 changes: 6 additions & 1 deletion kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@
resolved "https://registry.yarnpkg.com/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz#aa58042711d6e3275dd37dc597e5d31e8c290a44"
integrity sha512-sL/cEvJWAnClXw0wHk85/2L0G6Sj8UB0Ctc1TEMbKSsmpRosqhwj9gWgFRZSrBr2f9tiXISwNhCPmlfqUqyb9Q==

abort-controller@^3.0.0:
abort-controller@3.0.0, abort-controller@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392"
integrity sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==
Expand Down Expand Up @@ -2407,6 +2407,11 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==

[email protected]:
version "8.5.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.5.0.tgz#bfb4be96600757fe5382de12c670dab984a1ed4f"
integrity sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==

ws@>=7.4.6:
version "8.9.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.9.0.tgz#2a994bb67144be1b53fe2d23c53c028adeb7f45e"
Expand Down

0 comments on commit 5bc0a1c

Please sign in to comment.