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

Multiplatform #39

Draft
wants to merge 39 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
96bcc4d
Bump mockk from 1.12.0 to 1.12.4
dependabot[bot] May 11, 2022
1330fcd
update to kotlin 1.6.21 with multiplatform plugin for lib
luca992 May 24, 2022
bb70d3b
remove moshi. use kotlinx serialization
luca992 May 24, 2022
271d312
update dependencies
luca992 May 24, 2022
802d57b
port callCmd to ktor
luca992 May 24, 2022
5074f4a
remove unused import
luca992 May 24, 2022
9ae9c4d
remove reflect
luca992 May 24, 2022
95714ac
use ktor for add
luca992 May 24, 2022
7f680c1
include quotes around filename for string
luca992 May 25, 2022
fc0af5e
move code to common
luca992 May 25, 2022
38adc4e
fix file add
luca992 May 25, 2022
49dee73
move more dependencies to buildsrc versions
luca992 May 25, 2022
a6fdb82
add nearly all the targets ktor supports
luca992 May 25, 2022
7744979
port from using jvm file to okio filesystem
luca992 May 25, 2022
19893ed
add function to add file to ipfs using a BufferedSource
luca992 May 26, 2022
bc237d9
use experimental memory model
luca992 May 26, 2022
f740b60
change project library folder folder from being named lib to ipfs-api
luca992 Jun 2, 2022
0cb3154
update ktor
luca992 Jun 2, 2022
4596b38
add an option progress callback for adding files
luca992 Jul 2, 2022
a6963d9
update kotlin and dependencies
luca992 Jul 7, 2022
281e9a4
fix add
luca992 Jul 7, 2022
93f50a0
fix tests
luca992 Jul 7, 2022
bc0b4d5
update example program to add a file
luca992 Jul 7, 2022
63458b2
resolve warning
luca992 Jul 7, 2022
4db755c
calculate progress percentage of upload and add
luca992 Aug 6, 2022
65a18c5
Add.file: change source from BufferedSource to ByteArray.
luca992 Jul 21, 2022
b38f3f3
fix add progress callback
luca992 Jul 23, 2022
ae2a5fc
Merge branch 'add-progress' into multiplatform
luca992 Aug 6, 2022
361012a
Fix getting bytes
luca992 Aug 6, 2022
689707c
Infuras public gateway does not exist anymore add support for basic auth
luca992 Aug 13, 2022
c4c2210
gradle and coroutines updates
luca992 Aug 13, 2022
71ad41e
workaround for SocketTimeoutException
luca992 Aug 13, 2022
549cdd9
update ktor
luca992 Aug 13, 2022
65999be
add a prepareCallCmd to handle streaming data for add and get
luca992 Aug 14, 2022
cff2f9a
reformat
luca992 Aug 14, 2022
782553b
return as channel instead of callback
luca992 Aug 14, 2022
fb69325
Return content length with cat's ByteReadChannel when streaming
luca992 Aug 14, 2022
ec4d9d4
fix getting content length
luca992 Aug 14, 2022
cdc20a0
revert back to returning in callback handler to prevent channel closing
luca992 Aug 14, 2022
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
98 changes: 41 additions & 57 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,57 +1,41 @@
apply {
from("https://raw.githubusercontent.com/ligi/gradle-common/master/versions_plugin_stable_only.gradle")
}

buildscript {
repositories {
jcenter()
maven("https://jitpack.io")
}

dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}")
classpath("com.github.ben-manes:gradle-versions-plugin:${Versions.versions_plugin}")
}
}


subprojects {
repositories {
jcenter()
maven("https://jitpack.io")
maven("https://kotlin.bintray.com/kotlinx")
}

apply(plugin = "jacoco")
apply(plugin = "maven")
apply(plugin = "kotlin")

tasks.withType<Test> {
useJUnitPlatform()
}

configure<JavaPluginExtension> {
withSourcesJar()
withJavadocJar()
}

afterEvaluate {

dependencies {
"implementation"("org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}")

"testImplementation"("org.assertj:assertj-core:3.22.0")
"testImplementation"("org.junit.jupiter:junit-jupiter-api:${Versions.jupiter}")
"testImplementation"("org.junit.jupiter:junit-jupiter-params:${Versions.jupiter}")
"testRuntime"("org.junit.jupiter:junit-jupiter-engine:${Versions.jupiter}")

"testImplementation"("org.jetbrains.kotlin:kotlin-test")
"testImplementation"("io.mockk:mockk:1.12.0")
}


}
val compileTestKotlin by tasks.getting(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class) {
kotlinOptions.jvmTarget = "1.8"
}
}
//apply {
// from("https://raw.githubusercontent.com/ligi/gradle-common/master/versions_plugin_stable_only.gradle")
//}


//subprojects {
// repositories {
// mavenCentral()
// maven("https://jitpack.io")
// maven("https://kotlin.bintray.com/kotlinx")
// }
//
// tasks.withType<Test> {
// useJUnitPlatform()
// }
//
// configure<JavaPluginExtension> {
// withSourcesJar()
// withJavadocJar()
// }
//
// afterEvaluate {
//
// dependencies {
// "implementation"("org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}")
//
// "testImplementation"("org.assertj:assertj-core:3.22.0")
// "testImplementation"("org.junit.jupiter:junit-jupiter-api:${Versions.jupiter}")
// "testImplementation"("org.junit.jupiter:junit-jupiter-params:${Versions.jupiter}")
// "testImplementation"("org.junit.jupiter:junit-jupiter-engine:${Versions.jupiter}")
//
// "testImplementation"("org.jetbrains.kotlin:kotlin-test")
// "testImplementation"("io.mockk:mockk:1.12.4")
// }
//
//
// }
//// val compileTestKotlin by tasks.getting(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class) {
//// kotlinOptions.jvmTarget = "1.8"
//// }
//}
7 changes: 6 additions & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
object Versions {
const val kotlin = "1.3.72"
const val kotlin = "1.7.10"
const val serialization= "1.3.3"
const val versions_plugin = "0.28.0"
const val jupiter = "5.6.2"
const val okhttp = "4.10.0"
const val coroutines = "1.6.4"
const val ktor = "2.1.0"
const val okio = "3.2.0"
}
5 changes: 3 additions & 2 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
plugins {
kotlin("jvm")
application
}

application {
mainClassName = "org.kethereum.example_cli.ExampleCLIKt"
mainClass.set("io.ipfs.example_cli.ExampleCLIKt")
}

dependencies {
implementation(project(":lib"))
implementation(project(":ipfs-api"))
}
8 changes: 0 additions & 8 deletions example/src/main/kotlin/io/ipfs/example_cli/ExampleCLI.kt

This file was deleted.

25 changes: 25 additions & 0 deletions example/src/main/kotlin/io/ipfs/example_cli/ExampleInfuraCLI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.ipfs.example_cli

import io.ipfs.kotlin.commands.AddProgress
import io.ipfs.kotlin.commands.UploadProgress
import io.ipfs.kotlin.defaults.InfuraIPFS
import kotlinx.coroutines.runBlocking
import okio.Path.Companion.toOkioPath
import java.io.File

fun main(args: Array<String>) = runBlocking {
require(args.size == 3)
val projectId = args[0]
val projectSecret = args[1]
val filePath = args[2]
val ipfs = InfuraIPFS(projectId, projectSecret)
println(ipfs.info.version())
println(
ipfs.add.file(
File(filePath).toOkioPath()
) { uploadProgress: UploadProgress?, addProgress: AddProgress? ->
uploadProgress?.let { println(it) }
addProgress?.let { println(it) }
}
)
}
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kotlin.code.style=official

kotlin.native.binary.memoryModel=experimental
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
114 changes: 114 additions & 0 deletions ipfs-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
id("jacoco")
id("com.github.ben-manes.versions")
id("maven-publish")
}

group = "com.github.ligi"
version = "0.16"


repositories {
mavenCentral()
}

kotlin {
val darwinTargets = arrayOf(
"macosX64", "macosArm64",
"iosArm64", "iosX64", "iosSimulatorArm64",
"tvosArm64", "tvosX64", "tvosSimulatorArm64",
"watchosArm32", "watchosArm64", "watchosX86", "watchosX64", "watchosSimulatorArm64",
)
val linuxTargets = arrayOf("linuxX64", /*"linuxArm64"*/)
val mingwTargets = arrayOf("mingwX64")
val nativeTargets = linuxTargets + darwinTargets + mingwTargets

jvm {
compilations.all {
kotlinOptions.jvmTarget = "11"
}
}
js(IR) {
nodejs()
}
for (target in nativeTargets) {
targets.add(presets.getByName(target).createTarget(target))
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.serialization}")
api("io.ktor:ktor-client-core:${Versions.ktor}")
implementation("io.ktor:ktor-client-content-negotiation:${Versions.ktor}")
implementation("io.ktor:ktor-serialization-kotlinx-json:${Versions.ktor}")
api("com.squareup.okio:okio:${Versions.okio}")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutines}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.coroutines}")
}
}

val jvmMain by getting {
dependsOn(commonMain)
dependencies {
implementation("io.ktor:ktor-client-okhttp:${Versions.ktor}")
}
}

val jsMain by getting {
dependsOn(commonMain)
dependencies {
implementation("io.ktor:ktor-client-js:${Versions.ktor}")
implementation("com.squareup.okio:okio-nodefilesystem:${Versions.okio}")
}
}


val jvmTest by getting {
dependencies {
implementation("org.mockito:mockito-core:4.5.1")
implementation("com.squareup.okhttp3:mockwebserver:${Versions.okhttp}")
implementation("org.assertj:assertj-core:3.22.0")
}
}
val nativeMain by creating {
dependsOn(commonMain)
}

val darwinMain by creating {
dependsOn(nativeMain)
dependencies {
implementation("io.ktor:ktor-client-darwin:${Versions.ktor}")
}
}
darwinTargets.forEach { target ->
getByName("${target}Main") {
dependsOn(darwinMain)
}
}
val linuxAndMingwMain by creating {
dependsOn(nativeMain)
dependencies {
implementation("io.ktor:ktor-client-curl:${Versions.ktor}")
}
}
(mingwTargets + linuxTargets).forEach { target ->
getByName("${target}Main") {
dependsOn(linuxAndMingwMain)
}
}

}
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "11"
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package io.ipfs.kotlin

import com.squareup.moshi.Moshi
import io.ipfs.kotlin.commands.*
import io.ipfs.kotlin.defaults.createMoshi
import io.ipfs.kotlin.defaults.createOKHTTP
import io.ipfs.kotlin.defaults.createFileSystem
import io.ipfs.kotlin.defaults.createKTOR
import io.ipfs.kotlin.model.MessageWithCode
import okhttp3.OkHttpClient
import io.ktor.client.*
import okio.FileSystem

data class IPFSConfiguration(val base_url: String = "http://127.0.0.1:5001/api/v0/",
val okHttpClient: OkHttpClient = createOKHTTP(),
val moshi: Moshi = createMoshi())
data class IPFSConfiguration(
val base_url: String = "http://127.0.0.1:5001/api/v0/",
val ktorClient: HttpClient = createKTOR(),
val fileSystem: FileSystem = createFileSystem(),
val basicAuthCredentials: BasicAuth? = null
) {
class BasicAuth(
val username: String,
val password: String
)
}

open class IPFS(configuration: IPFSConfiguration) {

Expand Down
40 changes: 40 additions & 0 deletions ipfs-api/src/commonMain/kotlin/io/ipfs/kotlin/IPFSConnection.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.ipfs.kotlin

import io.ipfs.kotlin.model.MessageWithCode
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

open class IPFSConnection(val config: IPFSConfiguration) {

var lastError: MessageWithCode? = null

private fun buildRequest(cmd: String, block: HttpRequestBuilder.() -> Unit = {}): HttpRequestBuilder {
val request = HttpRequestBuilder().apply {
url(config.base_url + cmd)
contentType(ContentType.Any)
config.basicAuthCredentials?.apply {
basicAuth(username, password)
}
block()
}

return request
}

suspend fun callCmd(cmd: String): HttpResponse {
val request = buildRequest(cmd)
return config.ktorClient.post(request)
}

suspend fun prepareCallCmd(cmd: String, block: HttpRequestBuilder.() -> Unit = {}): HttpStatement {
val request = buildRequest(cmd, block)
return config.ktorClient.preparePost(request)
}

fun setErrorByJSON(jsonString: String) {
lastError = Json.decodeFromString(jsonString)
}
}
Loading