Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Aug 26, 2024
1 parent 4ac9ca3 commit b668d76
Show file tree
Hide file tree
Showing 26 changed files with 434 additions and 322 deletions.
5 changes: 2 additions & 3 deletions sandbox/src/commonMain/kotlin/com/example/UserFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ import io.ktor.client.call.*
import io.ktor.client.statement.*

class UserFactory : Converter.Factory {

override fun suspendResponseConverter(
typeData: TypeData,
ktorfit: Ktorfit
): Converter.SuspendResponseConverter<HttpResponse, *>? {
if (typeData.typeInfo.type == User::class) {
return object : Converter.SuspendResponseConverter<HttpResponse, Any> {

override suspend fun convert(result: KtorfitResult): Any {
when (result) {
when (result) {
is KtorfitResult.Success -> {
val response = result.response
val envelope = response.body<Envelope>()
return envelope.user
}

is KtorfitResult.Failure -> {
throw result.throwable
}
Expand Down
24 changes: 18 additions & 6 deletions sandbox/src/commonMain/kotlin/com/example/api/GithubService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import com.example.model.github.GithubFollowerResponseItem
import com.example.model.github.Issuedata
import com.example.model.github.TestReeeItem
import de.jensklingenberg.ktorfit.Call
import de.jensklingenberg.ktorfit.http.*
import de.jensklingenberg.ktorfit.http.Body
import de.jensklingenberg.ktorfit.http.GET
import de.jensklingenberg.ktorfit.http.Header
import de.jensklingenberg.ktorfit.http.Headers
import de.jensklingenberg.ktorfit.http.POST
import de.jensklingenberg.ktorfit.http.Path
import kotlinx.coroutines.flow.Flow

interface GithubService {

companion object {
const val baseUrl = "https://api.github.com/"
}
Expand All @@ -19,10 +23,16 @@ interface GithubService {
"Content-Type: application/json"
)
@POST("repos/foso/experimental/issues")
suspend fun createIssue(@Body body: Map<*,String>, @Header("Acci") headi: String?): String
suspend fun createIssue(
@Body body: Map<*, String>,
@Header("Acci") headi: String?
): String

@POST("repos/foso/experimental/issues")
suspend fun createIssue2(@Body body: Issuedata, @Header("Acci") headi: String?): Call<Map<String?, Int>>
suspend fun createIssue2(
@Body body: Issuedata,
@Header("Acci") headi: String?
): Call<Map<String?, Int>>

@Headers(
"Accept: application/vnd.github.v3+json",
Expand All @@ -38,6 +48,8 @@ interface GithubService {
"Content-Type: application/json"
)
@GET("repos/{owner}/{repo}/commits")
fun listCommits(@Path owner: String, @Path repo: String): Flow<List<TestReeeItem>>

fun listCommits(
@Path owner: String,
@Path repo: String
): Flow<List<TestReeeItem>>
}
54 changes: 38 additions & 16 deletions sandbox/src/commonMain/kotlin/com/example/api/JsonPlaceHolderApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import kotlinx.coroutines.Deferred
import kotlinx.coroutines.flow.Flow

interface JsonPlaceHolderApi {

companion object {
const val baseUrl = "https://jsonplaceholder.typicode.com/"
}
Expand All @@ -22,39 +21,56 @@ interface JsonPlaceHolderApi {
@GET("posts")
fun callPosts(): Call<List<Post>>

@HTTP("GET2","posts")
@HTTP("GET2", "posts")
fun callPostsCustomHttp(): Call<List<Post>>

@GET("posts")
fun suscallPosts(): Call<String>

@Streaming
@GET("docs/response.html#streaming")
suspend fun getPostsStreaming(@QueryMap test: Map<String, String>): HttpStatement
suspend fun getPostsStreaming(
@QueryMap test: Map<String, String>
): HttpStatement

@GET("posts/{postId}")
suspend fun getPostById(@Path postId: Int = 4): Post
suspend fun getPostById(
@Path postId: Int = 4
): Post

@GET("posts/{postId}/comments")
fun getFlowCommentsByPostId(@Path("postId") postId: Int, @ReqBuilder builder : HttpRequestBuilder.() -> Unit): Flow<List<Comment>>?
fun getFlowCommentsByPostId(
@Path("postId") postId: Int,
@ReqBuilder builder: HttpRequestBuilder.() -> Unit
): Flow<List<Comment>>?

@GET("posts/{postId}/comments")
suspend fun getCommentsByPostId(@Path("postId") postId: Int): List<Comment>?
suspend fun getCommentsByPostId(
@Path("postId") postId: Int
): List<Comment>?

@GET("posts/{postId}/comments")
suspend fun getCommentsByPostIdResponse(@RequestType(Int::class) @Path("postId") postId: String): MyOwnResponse<List<Comment>>
suspend fun getCommentsByPostIdResponse(
@RequestType(Int::class) @Path("postId") postId: String
): MyOwnResponse<List<Comment>>

@Headers(value = ["Content-Type: application/json"])
@GET("posts/{postId}/comments")
fun callCommentsByPostId(@Path("postId") postId: Int): Call<List<Comment>>
fun callCommentsByPostId(
@Path("postId") postId: Int
): Call<List<Comment>>

@Headers(value = ["Content-Type: application/json"])
@GET("posts/{postId}/comments")
suspend fun resCommentsByPostId(@Path("postId") postId: Int): Response<List<Comment>>
suspend fun resCommentsByPostId(
@Path("postId") postId: Int
): Response<List<Comment>>

@Headers(value = ["Content-Type: application/json"])
@GET("posts/{postId}/comments")
fun deferedCommentsByPostId(@Path("postId") postId: Int): Deferred<List<Comment>>
fun deferedCommentsByPostId(
@Path("postId") postId: Int
): Deferred<List<Comment>>

@Headers(value = ["Content-Type: application/json"])
@GET("comments")
Expand All @@ -64,19 +80,25 @@ interface JsonPlaceHolderApi {

@Headers(value = ["Content-Type: application/json"])
@POST("posts")
suspend fun postPosts(@Body post: Post): Post
suspend fun postPosts(
@Body post: Post
): Post

@Headers(value = ["Content-Type: application/json"])
@POST("posts")
suspend fun putPosts(@Body post: Post): Post
suspend fun putPosts(
@Body post: Post
): Post

@Headers(value = ["Content-Type: application/json"])
@PATCH("posts/{postId}/{number}")
suspend fun patchPosts(@Path("postId") postId: Int): Post
suspend fun patchPosts(
@Path("postId") postId: Int
): Post

@Headers(value = ["Content-Type: application/json"])
@DELETE("posts/{postId}")
suspend fun deletePosts(@Path("postId") postId: Int): String

suspend fun deletePosts(
@Path("postId") postId: Int
): String
}

41 changes: 24 additions & 17 deletions sandbox/src/commonMain/kotlin/com/example/api/KtorSamplesApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

interface KtorSamplesApi {

companion object {
const val baseUrl = "http://localhost:8080/"
}
Expand All @@ -20,9 +19,11 @@ interface KtorSamplesApi {
val test1: String

@POST("signup")
suspend fun sendReg(@Body param: Parameters): String
suspend fun sendReg(
@Body param: Parameters
): String

//client-submit-form
// client-submit-form
@POST("signup")
@FormUrlEncoded
suspend fun signup(
Expand All @@ -31,11 +32,9 @@ interface KtorSamplesApi {
@Field("password") password: String,
@Field("confirmation") confirmation: String,
@Field("names") names: List<String>

): String


//client-submit-form
// client-submit-form
@POST("signup")
@FormUrlEncoded
suspend fun signup(
Expand All @@ -45,35 +44,43 @@ interface KtorSamplesApi {

@Multipart
@POST("upload")
suspend fun uploadFile(@Part("description") description: String, @Part("list") file: List<PartData>,@PartMap() map : Map<String,PartData>): String
suspend fun uploadFile(
@Part("description") description: String,
@Part("list") file: List<PartData>,
@PartMap() map: Map<String, PartData>
): String

@POST("upload")
suspend fun upload(@Body map: MultiPartFormDataContent)
suspend fun upload(
@Body map: MultiPartFormDataContent
)
}


data class Query(
val working: Working
) {
data class Working(
val data: String
val data: String
)


data class NotWorking(
val reponse: String
val reponse: String
)
}


interface API {
data class JensTest(val names: List<String>)
data class JensTest(
val names: List<String>
)

@Headers(
"Content-Type: application/json", "Accept: application/json"
"Content-Type: application/json",
"Accept: application/json"
)
@POST("example/request")
suspend fun query(@Body query: Query): List<NotWorking> // not sure if non-list works, haven't tested
suspend fun query(
@Body query: Query
): List<NotWorking> // not sure if non-list works, haven't tested
}

class KtorfitTest {
Expand All @@ -87,4 +94,4 @@ class KtorfitTest {
interface ITest {
@GET("test")
fun test(): KtorfitTest.TestData
}
}
10 changes: 8 additions & 2 deletions sandbox/src/commonMain/kotlin/com/example/api/Response.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.example.api

sealed class Response<T> {
data class Success<T>(val data: T) : Response<T>()
class Error(val ex:Throwable) : Response<Nothing>()
data class Success<T>(
val data: T
) : Response<T>()

class Error(
val ex: Throwable
) : Response<Nothing>()

companion object {
fun <T> success(data: T) = Success(data)

fun error(ex: Throwable) = Error(ex)
}
}
8 changes: 4 additions & 4 deletions sandbox/src/commonMain/kotlin/com/example/api/StarWarsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import de.jensklingenberg.ktorfit.http.GET
import de.jensklingenberg.ktorfit.http.Path

interface StarWarsApi {

companion object{
companion object {
const val baseUrl = "https://swapi.dev/api/"
}

@GET("people/{id}/")
fun getPersonById(@Path("id") peopleId: Int): Call<People>
fun getPersonById(
@Path("id") peopleId: Int
): Call<People>
}

40 changes: 21 additions & 19 deletions sandbox/src/commonMain/kotlin/com/example/model/CommonClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,28 @@ import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json


val commonClient = HttpClient() {

install(ContentNegotiation) {
json(Json { isLenient = true; ignoreUnknownKeys = true })
val commonClient =
HttpClient {

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



val commonKtorfit = ktorfit {
baseUrl(JsonPlaceHolderApi.baseUrl)
httpClient(commonClient)
converterFactories(
CallConverterFactory(),
StringToIntRequestConverterFactory(),
MyOwnResponseConverterFactory()
)
}

val commonKtorfit =
ktorfit {
baseUrl(JsonPlaceHolderApi.baseUrl)
httpClient(commonClient)
converterFactories(
CallConverterFactory(),
StringToIntRequestConverterFactory(),
MyOwnResponseConverterFactory()
)
}

val jsonPlaceHolderApi = commonKtorfit.createJsonPlaceHolderApi()

10 changes: 8 additions & 2 deletions sandbox/src/commonMain/kotlin/com/example/model/Envelope.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.example.model

@kotlinx.serialization.Serializable
data class Envelope(val success: Boolean, val user: User)
data class Envelope(
val success: Boolean,
val user: User
)

@kotlinx.serialization.Serializable
data class User(val id: Int, val name: String)
data class User(
val id: Int,
val name: String
)
12 changes: 9 additions & 3 deletions sandbox/src/commonMain/kotlin/com/example/model/MyOwnResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import kotlinx.serialization.Serializable

@Serializable
sealed class MyOwnResponse<T> {
data class Success<T>(val data: T) : MyOwnResponse<T>()
class Error<T>(val ex: Throwable) : MyOwnResponse<Nothing>()
data class Success<T>(
val data: T
) : MyOwnResponse<T>()

class Error<T>(
val ex: Throwable
) : MyOwnResponse<Nothing>()

companion object {
fun <T> success(data: T) = Success(data)

fun error(ex: Throwable) = Error<Nothing>(ex)
}
}
}
Loading

0 comments on commit b668d76

Please sign in to comment.