I'm trying to make a multiplatform library that wr...
# ktor
z
I'm trying to make a multiplatform library that wraps some API functions with ktor and I've run into the issue of how to design the libraries client constructor. Should the user just be able to call it with no args and have the library create the HttpClient, or some other design? This is the first time I've tried making a library so im a little unfamiliar right now
n
I have similar issues as well and I don't have a good solution yet 😕 On one side, the
HttpClient
should be customizable by the library user. On the other side, there may be some requirements regarding the
HttpClient
configuration by the library (eg. some plugins must be installed for the library to function properly).
a
Think about customization, do your client that customization??
z
@Norbi yes, it requires the content negotiation plugin for Json support
a
@zt since it's a multiplatform lib, you have to give room for customization as need. For example
engine
should be initiated as needed. This will help you avoid bloating up the client project if they are only using it on maybe 2 platforms
z
So I could have the user provide the engine as a constructor param?
a
I think you should have a sensible default for the HttpClient. Also, you can have a parameter in the constructor with the
HttpClient
type that you can use to create a clone of an HttpClient with the
ContentNegotiation
plugin installed by using the
HttpClient.config
method.
🙏 1
👍🏻 1
z
I'm a little confused on what you mean. How should I set up my dependencies? Right now its:
Copy code
val ktorVersion = "2.1.2"

val commonMain by getting {
    dependencies {
        implementation("io.ktor:ktor-client-core:$ktorVersion")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0")
    }
}
val commonTest by getting {
    dependencies {
        implementation(kotlin("test"))
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0")
        implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
        implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
    }
}
val jvmMain by getting {
    dependencies {
        implementation("io.ktor:ktor-client-cio:$ktorVersion")
    }
}
val jsTest by getting {
    dependencies {
        implementation("io.ktor:ktor-client-js:$ktorVersion")
    }
}
val nativeMain by getting {
    dependencies {
        implementation("io.ktor:ktor-client-curl:$ktorVersion")
    }
}