Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add networking example using Ktor #16

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update ktor to latest version
  • Loading branch information
gchristov committed Oct 11, 2022
commit 53c304b1cb2f30d9ed682f04c503ce0a404e77c2
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.gchristov.thecodinglove

import io.ktor.client.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.features.logging.*
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
Expand All @@ -17,17 +19,19 @@ fun main(args: Array<String>) {
exports.myTestFun = fireFunctions.https.onRequest { request, response ->
val client = provideHttpClient()
GlobalScope.launch {
val userResponse = client.get<Response>("https://reqres.in/api/users")
val userResponse: Response = client.get("https://reqres.in/api/users").body()
response.send(Messenger().message() + ", " + userResponse.page)
}
}
}

private fun provideHttpClient() = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(kotlinx.serialization.json.Json {
ignoreUnknownKeys = true
})
install(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
}
)
}
install(Logging) {
logger = Logger.SIMPLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.gchristov.thecodinglove.gradleplugins

class Deps {
object Ktor {
private const val ktorVersion = "1.6.7"
const val clientCore = "io.ktor:ktor-client-core:$ktorVersion"
const val clientSerialization = "io.ktor:ktor-client-serialization:$ktorVersion"
const val clientLogging = "io.ktor:ktor-client-logging:$ktorVersion"
const val clientJavascript = "io.ktor:ktor-client-js:$ktorVersion"
const val logbackClassic = "ch.qos.logback:logback-classic:1.2.10"
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 @@ -53,13 +53,11 @@ private fun Project.configureNetwork() {
plugins.apply("org.jetbrains.kotlin.plugin.serialization")
extensions.configure(KotlinMultiplatformExtension::class.java) {
sourceSets.maybeCreate("commonMain").dependencies {
implementation(Deps.Ktor.clientCore)
implementation(Deps.Ktor.clientSerialization)
implementation(Deps.Ktor.clientLogging)
implementation(Deps.Ktor.logbackClassic)
}
sourceSets.maybeCreate("jsMain").dependencies {
implementation(Deps.Ktor.clientJavascript)
implementation(Deps.Ktor.client)
implementation(Deps.Ktor.contentNegotiation)
implementation(Deps.Ktor.serialisation)
implementation(Deps.Ktor.logging)
implementation(Deps.Ktor.logback)
}
}
}
20 changes: 4 additions & 16 deletions kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -691,11 +691,6 @@ arrify@^2.0.0:
resolved "https://registry.yarnpkg.com/arrify/-/arrify-2.0.1.tgz#c9655e9331e0abcd588d2a7cad7e9956f66701fa"
integrity sha512-3duEwti880xqi4eAMN8AyR4a0ByT90zoYdLlevfrvU43vb0YZwZVfxOgxWrLXXXpyugL0hNZc9G6BiB5B3nUug==

async-limiter@~1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd"
integrity sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==

async-retry@^1.3.3:
version "1.3.3"
resolved "https://registry.yarnpkg.com/async-retry/-/async-retry-1.3.3.tgz#0e7f36c04d8478e7a58bdbed80cedf977785f280"
Expand Down Expand Up @@ -1860,11 +1855,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==

[email protected]:
version "2.6.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==

[email protected], node-fetch@^2.6.1, node-fetch@^2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
Expand Down Expand Up @@ -2417,12 +2407,10 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==

[email protected]:
version "6.2.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.1.tgz#442fdf0a47ed64f59b6a5d8ff130f4748ed524fb"
integrity sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==
dependencies:
async-limiter "~1.0.0"
[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"
Expand Down