-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add networking example using Ktor (#16)
- Loading branch information
Showing
5 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
38 changes: 36 additions & 2 deletions
38
appJs/appJs/src/commonMain/kotlin/com/gchristov/thecodinglove/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
gradle-plugins/src/main/kotlin/com/gchristov/thecodinglove/gradleplugins/Deps.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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== | ||
|
@@ -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" | ||
|