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

Implement sandwich-ktor module #190

Merged
merged 5 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ android {

dependencies {
implementation(project(":sandwich"))
implementation(project(":sandwich-ktor"))
implementation(project(":sandwich-retrofit"))
implementation(project(":sandwich-retrofit-datasource"))
implementation(project(":sandwich-retrofit-serialization"))
Expand All @@ -68,6 +69,11 @@ dependencies {
implementation(libs.moshi)
ksp(libs.moshi.codegen)

implementation(libs.ktor.negotiation)
implementation(libs.ktor.okhttp)
implementation(libs.ktor.json)
implementation(libs.serialization)

implementation(libs.glide)
implementation(libs.timber)

Expand Down
26 changes: 26 additions & 0 deletions app/src/main/kotlin/com/skydoves/sandwichdemo/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,44 @@ import com.skydoves.sandwich.onException
import com.skydoves.sandwich.retrofit.statusCode
import com.skydoves.sandwich.suspendOnSuccess
import com.skydoves.sandwichdemo.model.Poster
import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.request.accept
import io.ktor.http.ContentType
import io.ktor.http.contentType
import io.ktor.serialization.kotlinx.json.json
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import timber.log.Timber

class MainViewModel(mainRepository: MainRepository) : ViewModel() {

val toastLiveData = MutableLiveData<String>()

private val client = HttpClient(OkHttp) {
defaultRequest {
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
}

install(ContentNegotiation) {
json(
Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
},
)
}
}

// Use Case 1 - update the fetched posters as a property
val posterList: StateFlow<List<Poster>> = mainRepository.fetchPostersFlow().map {
if (it.isSuccess) {
Expand Down
17 changes: 10 additions & 7 deletions app/src/main/kotlin/com/skydoves/sandwichdemo/model/Poster.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
package com.skydoves.sandwichdemo.model

import com.squareup.moshi.JsonClass
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@JsonClass(generateAdapter = true)
data class Poster(
val id: Long,
val name: String,
val release: String,
val playtime: String,
val description: String,
val plot: String,
val poster: String,
@SerialName("id") val id: Long,
@SerialName("name") val name: String,
@SerialName("release") val release: String,
@SerialName("playtime") val playtime: String,
@SerialName("description") val description: String,
@SerialName("plot") val plot: String,
@SerialName("poster") val poster: String,
)
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ material = "1.9.0"
retrofit = "2.9.0"
okio = "3.6.0"
okhttp = "4.12.0"
ktor = "2.3.5"
coroutines = "1.7.3"
kotlinSerialization = "1.6.0"
ksp = "1.9.10-1.0.13"
Expand Down Expand Up @@ -51,6 +52,11 @@ retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref =
retrofit-moshi = { group = "com.squareup.retrofit2", name = "converter-moshi", version.ref = "retrofit" }
okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttp" }
okio = { group = "com.squareup.okio", name = "okio", version.ref = "okio" }
ktor-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
ktor-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" }
ktor-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktor" }
ktor-okhttp = { group = "io.ktor", name = "ktor-client-okhttp", version.ref = "ktor" }
ktor-darwin = { group = "io.ktor", name = "ktor-client-darwin", version.ref = "ktor" }
coroutines = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutines" }
serialization = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinSerialization" }
moshi = { group = "com.squareup.moshi", name = "moshi-kotlin", version.ref = "moshi" }
Expand Down
1 change: 1 addition & 0 deletions sandwich-ktor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
18 changes: 18 additions & 0 deletions sandwich-ktor/api/sandwich-ktor.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public final class com/skydoves/sandwich/ktor/ApiResponseExtensionKt {
public static final fun bodyChannel (Lcom/skydoves/sandwich/ApiResponse$Failure$Error;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun bodyString (Lcom/skydoves/sandwich/ApiResponse$Failure$Error;Ljava/nio/charset/Charset;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun bodyString$default (Lcom/skydoves/sandwich/ApiResponse$Failure$Error;Ljava/nio/charset/Charset;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun getHeaders (Lcom/skydoves/sandwich/ApiResponse$Failure$Error;)Lio/ktor/http/Headers;
public static final fun getHeaders (Lcom/skydoves/sandwich/ApiResponse$Success;)Lio/ktor/http/Headers;
public static final fun getHttpResponse (Lcom/skydoves/sandwich/ApiResponse$Success;)Lio/ktor/client/statement/HttpResponse;
public static final fun getPayloadResponse (Lcom/skydoves/sandwich/ApiResponse$Failure$Error;)Lio/ktor/client/statement/HttpResponse;
public static final fun getStatusCode (Lcom/skydoves/sandwich/ApiResponse$Failure$Error;)Lcom/skydoves/sandwich/StatusCode;
public static final fun getStatusCode (Lcom/skydoves/sandwich/ApiResponse$Success;)Lcom/skydoves/sandwich/StatusCode;
public static final fun getStatusCode (Lio/ktor/client/statement/HttpResponse;)Lcom/skydoves/sandwich/StatusCode;
public static final fun getTagResponse (Lcom/skydoves/sandwich/ApiResponse$Success;)Lio/ktor/client/statement/HttpResponse;
}

public synthetic class com/skydoves/sandwich/ktor/ApiResponseExtensionKt$EntriesMappings {
public static final synthetic field entries$0 Lkotlin/enums/EnumEntries;
}

126 changes: 126 additions & 0 deletions sandwich-ktor/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Designed and developed by 2020 skydoves (Jaewoong Eum)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.github.skydoves.sandwich.Configuration

plugins {
id(libs.plugins.kotlin.multiplatform.get().pluginId)
id(libs.plugins.kotlin.serialization.get().pluginId)
id(libs.plugins.nexus.plugin.get().pluginId)
java
}

apply(from = "${rootDir}/scripts/publish-module.gradle.kts")

mavenPublishing {
pom {
version = rootProject.extra.get("libVersion").toString()
group = Configuration.artifactGroup
}
}

kotlin {
jvmToolchain(11)
jvm {
compilations.all {
kotlinOptions.jvmTarget = libs.versions.jvmTarget.get()
}
withJava()
}
ios()
iosSimulatorArm64()
macosArm64()
macosX64()

sourceSets {
all { languageSettings.optIn("kotlin.contracts.ExperimentalContracts") }
val commonMain by getting {
dependencies {
api(project(":sandwich"))
api(libs.ktor.core)
implementation(libs.coroutines)
}
}

val commonTest by getting {
dependencies {
dependsOn(commonMain)
}
}

val jvmMain by getting

val jvmTest by getting

val appleTest by creating {
dependsOn(commonTest)
}
val appleMain by creating {
dependsOn(commonMain)
}
val iosMain by getting {
dependsOn(appleMain)
}
val macosArm64Main by getting {
dependsOn(appleMain)
}
val macosX64Main by getting {
dependsOn(appleMain)
}
val iosSimulatorArm64Main by getting {
dependsOn(appleMain)
}

val iosArm64Main by getting {
dependsOn(appleTest)
}
val iosArm64Test by getting {
dependsOn(appleTest)
}

val iosX64Main by getting {
dependsOn(appleTest)
}
val iosX64Test by getting {
dependsOn(appleTest)
}

val iosTest by getting {
dependsOn(appleTest)
}
val iosSimulatorArm64Test by getting {
dependsOn(appleTest)
}
val macosX64Test by getting {
dependsOn(appleTest)
}
val macosArm64Test by getting {
dependsOn(appleTest)
}
}

explicitApi()
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

tasks.withType(JavaCompile::class.java).configureEach {
this.targetCompatibility = JavaVersion.VERSION_11.toString()
this.sourceCompatibility = JavaVersion.VERSION_11.toString()
}
3 changes: 3 additions & 0 deletions sandwich-ktor/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POM_ARTIFACT_ID=sandwich-ktor
POM_NAME=sandwich-ktor
POM_DESCRIPTION=A lightweight and pluggable sealed API library for modeling Retrofit responses and handling exceptions on Kotlin and Android.
Loading