From b668d7695f5a2238b91651faa1072ec19acbf7fd Mon Sep 17 00:00:00 2001 From: Jens Klingenberg Date: Mon, 26 Aug 2024 22:01:20 +0200 Subject: [PATCH] Reformat --- .../kotlin/com/example/UserFactory.kt | 5 +- .../kotlin/com/example/api/GithubService.kt | 24 ++- .../com/example/api/JsonPlaceHolderApi.kt | 54 +++++-- .../kotlin/com/example/api/KtorSamplesApi.kt | 41 +++-- .../kotlin/com/example/api/Response.kt | 10 +- .../kotlin/com/example/api/StarWarsApi.kt | 8 +- .../kotlin/com/example/model/CommonClient.kt | 40 ++--- .../kotlin/com/example/model/Envelope.kt | 10 +- .../kotlin/com/example/model/MyOwnResponse.kt | 12 +- .../example/model/MyOwnResponseConverter.kt | 22 ++- .../kotlin/com/example/model/People.kt | 8 +- .../kotlin/com/example/model/Post.kt | 15 +- .../kotlin/com/example/model/Specie.kt | 2 - .../model/StringToIntRequestConverter.kt | 17 +- .../model/github/GithubFollowerResponse.kt | 37 +++-- .../com/example/model/github/Issuedata.kt | 5 +- .../com/example/model/github/TestReee.kt | 146 +++++++++--------- sandbox/src/jsMain/kotlin/JsMain.kt | 10 +- .../ktorfit/demo/CreateIssue.kt | 46 +++--- .../ktorfit/demo/HeaderTestApi.kt | 39 +++-- .../jensklingenberg/ktorfit/demo/JvMMain.kt | 67 ++++---- .../ktorfit/demo/JvmPlaceHolderApi.kt | 37 +++-- .../ktorfit/demo/QueryTestApi.kt | 46 ++++-- .../jensklingenberg/ktorfit/demo/TestApi.kt | 27 ++-- .../jensklingenberg/ktorfit/demo/TestApi2.kt | 25 +-- sandbox/src/linuxX64Main/kotlin/LinuxMain.kt | 3 +- 26 files changed, 434 insertions(+), 322 deletions(-) diff --git a/sandbox/src/commonMain/kotlin/com/example/UserFactory.kt b/sandbox/src/commonMain/kotlin/com/example/UserFactory.kt index ef9fb16da..87d99ff70 100644 --- a/sandbox/src/commonMain/kotlin/com/example/UserFactory.kt +++ b/sandbox/src/commonMain/kotlin/com/example/UserFactory.kt @@ -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? { if (typeData.typeInfo.type == User::class) { return object : Converter.SuspendResponseConverter { - override suspend fun convert(result: KtorfitResult): Any { - when (result) { + when (result) { is KtorfitResult.Success -> { val response = result.response val envelope = response.body() return envelope.user } + is KtorfitResult.Failure -> { throw result.throwable } diff --git a/sandbox/src/commonMain/kotlin/com/example/api/GithubService.kt b/sandbox/src/commonMain/kotlin/com/example/api/GithubService.kt index 53f2b16be..a44da5bdb 100644 --- a/sandbox/src/commonMain/kotlin/com/example/api/GithubService.kt +++ b/sandbox/src/commonMain/kotlin/com/example/api/GithubService.kt @@ -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/" } @@ -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> + suspend fun createIssue2( + @Body body: Issuedata, + @Header("Acci") headi: String? + ): Call> @Headers( "Accept: application/vnd.github.v3+json", @@ -38,6 +48,8 @@ interface GithubService { "Content-Type: application/json" ) @GET("repos/{owner}/{repo}/commits") - fun listCommits(@Path owner: String, @Path repo: String): Flow> - + fun listCommits( + @Path owner: String, + @Path repo: String + ): Flow> } diff --git a/sandbox/src/commonMain/kotlin/com/example/api/JsonPlaceHolderApi.kt b/sandbox/src/commonMain/kotlin/com/example/api/JsonPlaceHolderApi.kt index 0ce342cf5..0e5aa7391 100644 --- a/sandbox/src/commonMain/kotlin/com/example/api/JsonPlaceHolderApi.kt +++ b/sandbox/src/commonMain/kotlin/com/example/api/JsonPlaceHolderApi.kt @@ -11,7 +11,6 @@ import kotlinx.coroutines.Deferred import kotlinx.coroutines.flow.Flow interface JsonPlaceHolderApi { - companion object { const val baseUrl = "https://jsonplaceholder.typicode.com/" } @@ -22,7 +21,7 @@ interface JsonPlaceHolderApi { @GET("posts") fun callPosts(): Call> - @HTTP("GET2","posts") + @HTTP("GET2", "posts") fun callPostsCustomHttp(): Call> @GET("posts") @@ -30,31 +29,48 @@ interface JsonPlaceHolderApi { @Streaming @GET("docs/response.html#streaming") - suspend fun getPostsStreaming(@QueryMap test: Map): HttpStatement + suspend fun getPostsStreaming( + @QueryMap test: Map + ): 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>? + fun getFlowCommentsByPostId( + @Path("postId") postId: Int, + @ReqBuilder builder: HttpRequestBuilder.() -> Unit + ): Flow>? @GET("posts/{postId}/comments") - suspend fun getCommentsByPostId(@Path("postId") postId: Int): List? + suspend fun getCommentsByPostId( + @Path("postId") postId: Int + ): List? @GET("posts/{postId}/comments") - suspend fun getCommentsByPostIdResponse(@RequestType(Int::class) @Path("postId") postId: String): MyOwnResponse> + suspend fun getCommentsByPostIdResponse( + @RequestType(Int::class) @Path("postId") postId: String + ): MyOwnResponse> @Headers(value = ["Content-Type: application/json"]) @GET("posts/{postId}/comments") - fun callCommentsByPostId(@Path("postId") postId: Int): Call> + fun callCommentsByPostId( + @Path("postId") postId: Int + ): Call> @Headers(value = ["Content-Type: application/json"]) @GET("posts/{postId}/comments") - suspend fun resCommentsByPostId(@Path("postId") postId: Int): Response> + suspend fun resCommentsByPostId( + @Path("postId") postId: Int + ): Response> @Headers(value = ["Content-Type: application/json"]) @GET("posts/{postId}/comments") - fun deferedCommentsByPostId(@Path("postId") postId: Int): Deferred> + fun deferedCommentsByPostId( + @Path("postId") postId: Int + ): Deferred> @Headers(value = ["Content-Type: application/json"]) @GET("comments") @@ -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 } - diff --git a/sandbox/src/commonMain/kotlin/com/example/api/KtorSamplesApi.kt b/sandbox/src/commonMain/kotlin/com/example/api/KtorSamplesApi.kt index 283c024e1..0cb551dd5 100644 --- a/sandbox/src/commonMain/kotlin/com/example/api/KtorSamplesApi.kt +++ b/sandbox/src/commonMain/kotlin/com/example/api/KtorSamplesApi.kt @@ -10,7 +10,6 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable interface KtorSamplesApi { - companion object { const val baseUrl = "http://localhost:8080/" } @@ -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( @@ -31,11 +32,9 @@ interface KtorSamplesApi { @Field("password") password: String, @Field("confirmation") confirmation: String, @Field("names") names: List - ): String - - //client-submit-form + // client-submit-form @POST("signup") @FormUrlEncoded suspend fun signup( @@ -45,35 +44,43 @@ interface KtorSamplesApi { @Multipart @POST("upload") - suspend fun uploadFile(@Part("description") description: String, @Part("list") file: List,@PartMap() map : Map): String + suspend fun uploadFile( + @Part("description") description: String, + @Part("list") file: List, + @PartMap() map: Map + ): 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) + data class JensTest( + val names: List + ) @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 // not sure if non-list works, haven't tested + suspend fun query( + @Body query: Query + ): List // not sure if non-list works, haven't tested } class KtorfitTest { @@ -87,4 +94,4 @@ class KtorfitTest { interface ITest { @GET("test") fun test(): KtorfitTest.TestData -} \ No newline at end of file +} diff --git a/sandbox/src/commonMain/kotlin/com/example/api/Response.kt b/sandbox/src/commonMain/kotlin/com/example/api/Response.kt index 889cae8cc..93b947e26 100644 --- a/sandbox/src/commonMain/kotlin/com/example/api/Response.kt +++ b/sandbox/src/commonMain/kotlin/com/example/api/Response.kt @@ -1,11 +1,17 @@ package com.example.api sealed class Response { - data class Success(val data: T) : Response() - class Error(val ex:Throwable) : Response() + data class Success( + val data: T + ) : Response() + + class Error( + val ex: Throwable + ) : Response() companion object { fun success(data: T) = Success(data) + fun error(ex: Throwable) = Error(ex) } } diff --git a/sandbox/src/commonMain/kotlin/com/example/api/StarWarsApi.kt b/sandbox/src/commonMain/kotlin/com/example/api/StarWarsApi.kt index 57edb3ea8..4ba91f5e0 100644 --- a/sandbox/src/commonMain/kotlin/com/example/api/StarWarsApi.kt +++ b/sandbox/src/commonMain/kotlin/com/example/api/StarWarsApi.kt @@ -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 + fun getPersonById( + @Path("id") peopleId: Int + ): Call } - diff --git a/sandbox/src/commonMain/kotlin/com/example/model/CommonClient.kt b/sandbox/src/commonMain/kotlin/com/example/model/CommonClient.kt index 7b97c2d5e..613774f58 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/CommonClient.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/CommonClient.kt @@ -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() - diff --git a/sandbox/src/commonMain/kotlin/com/example/model/Envelope.kt b/sandbox/src/commonMain/kotlin/com/example/model/Envelope.kt index 5fd2635f6..bb5c4f0ec 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/Envelope.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/Envelope.kt @@ -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) \ No newline at end of file +data class User( + val id: Int, + val name: String +) diff --git a/sandbox/src/commonMain/kotlin/com/example/model/MyOwnResponse.kt b/sandbox/src/commonMain/kotlin/com/example/model/MyOwnResponse.kt index 29f8051ef..bd99ef81d 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/MyOwnResponse.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/MyOwnResponse.kt @@ -4,11 +4,17 @@ import kotlinx.serialization.Serializable @Serializable sealed class MyOwnResponse { - data class Success(val data: T) : MyOwnResponse() - class Error(val ex: Throwable) : MyOwnResponse() + data class Success( + val data: T + ) : MyOwnResponse() + + class Error( + val ex: Throwable + ) : MyOwnResponse() companion object { fun success(data: T) = Success(data) + fun error(ex: Throwable) = Error(ex) } -} \ No newline at end of file +} diff --git a/sandbox/src/commonMain/kotlin/com/example/model/MyOwnResponseConverter.kt b/sandbox/src/commonMain/kotlin/com/example/model/MyOwnResponseConverter.kt index b8d056ab8..33a039b6a 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/MyOwnResponseConverter.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/MyOwnResponseConverter.kt @@ -4,20 +4,16 @@ import de.jensklingenberg.ktorfit.Ktorfit import de.jensklingenberg.ktorfit.converter.Converter import de.jensklingenberg.ktorfit.converter.KtorfitResult import de.jensklingenberg.ktorfit.converter.TypeData -import io.ktor.client.call.* -import io.ktor.client.statement.* - +import io.ktor.client.call.body +import io.ktor.client.statement.HttpResponse class MyOwnResponseConverterFactory : Converter.Factory { - override fun suspendResponseConverter( typeData: TypeData, ktorfit: Ktorfit ): Converter.SuspendResponseConverter? { if (typeData.typeInfo.type == MyOwnResponse::class) { - return object : Converter.SuspendResponseConverter { - override suspend fun convert(result: KtorfitResult): Any { return when (result) { is KtorfitResult.Failure -> { @@ -27,11 +23,13 @@ class MyOwnResponseConverterFactory : Converter.Factory { is KtorfitResult.Success -> { val response = result.response return try { - val convertedBody = ktorfit.nextSuspendResponseConverter( - null, - typeData.typeArgs.first() - )?.convert(result) - ?: response.body(typeData.typeArgs.first().typeInfo) + val convertedBody = + ktorfit + .nextSuspendResponseConverter( + null, + typeData.typeArgs.first() + )?.convert(result) + ?: response.body(typeData.typeArgs.first().typeInfo) MyOwnResponse.success(convertedBody) } catch (ex: Throwable) { MyOwnResponse.error(ex) @@ -43,4 +41,4 @@ class MyOwnResponseConverterFactory : Converter.Factory { } return null } -} \ No newline at end of file +} diff --git a/sandbox/src/commonMain/kotlin/com/example/model/People.kt b/sandbox/src/commonMain/kotlin/com/example/model/People.kt index 817fb876c..0f3b13c02 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/People.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/People.kt @@ -1,7 +1,6 @@ package com.example.model @kotlinx.serialization.Serializable - data class People( val films: List? = null, val homeworld: String? = null, @@ -10,14 +9,13 @@ data class People( val edited: String? = null, val created: String? = null, val mass: String? = null, - //val vehicles: List? = null, + // val vehicles: List? = null, val url: String? = null, val hairColor: String? = null, val birthYear: String? = null, val eyeColor: String? = null, - //val species: List? = null, - //val starships: List? = null, + // val species: List? = null, + // val starships: List? = null, val name: String? = null, val height: String? = null ) - diff --git a/sandbox/src/commonMain/kotlin/com/example/model/Post.kt b/sandbox/src/commonMain/kotlin/com/example/model/Post.kt index f5afa4f30..3efc25c34 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/Post.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/Post.kt @@ -3,7 +3,18 @@ package com.example.model import kotlinx.serialization.Serializable @Serializable -data class Post(val userId: Int, val id: Int, val title: String, val body: String) +data class Post( + val userId: Int, + val id: Int, + val title: String, + val body: String +) @Serializable -data class Comment(val postId: Int, val id: Int, val name: String, val body: String, val email: String) +data class Comment( + val postId: Int, + val id: Int, + val name: String, + val body: String, + val email: String +) diff --git a/sandbox/src/commonMain/kotlin/com/example/model/Specie.kt b/sandbox/src/commonMain/kotlin/com/example/model/Specie.kt index b5f791f36..29e4fde9e 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/Specie.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/Specie.kt @@ -1,7 +1,6 @@ package com.example.model @kotlinx.serialization.Serializable - data class Specie( val films: List? = null, val skinColors: String? = null, @@ -19,4 +18,3 @@ data class Specie( val designation: String? = null, val averageLifespan: String? = null ) - diff --git a/sandbox/src/commonMain/kotlin/com/example/model/StringToIntRequestConverter.kt b/sandbox/src/commonMain/kotlin/com/example/model/StringToIntRequestConverter.kt index 6dd32cb86..47844b0c8 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/StringToIntRequestConverter.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/StringToIntRequestConverter.kt @@ -3,16 +3,15 @@ package com.example.model import de.jensklingenberg.ktorfit.converter.Converter import kotlin.reflect.KClass - class StringToIntRequestConverterFactory : Converter.Factory { - class StringToIntRequestConverter : Converter.RequestParameterConverter { - override fun convert(data: Any): Any { - return (data as String).toInt() - } + override fun convert(data: Any): Any = (data as String).toInt() } - private fun supportedType(parameterType: KClass<*>, requestType: KClass<*>): Boolean { + private fun supportedType( + parameterType: KClass<*>, + requestType: KClass<*> + ): Boolean { val parameterIsString = parameterType == String::class val requestIsInt = requestType == Int::class return parameterIsString && requestIsInt @@ -24,11 +23,9 @@ class StringToIntRequestConverterFactory : Converter.Factory { ): Converter.RequestParameterConverter? { if (supportedType(parameterType, requestType)) { return object : Converter.RequestParameterConverter { - override fun convert(data: Any): Any { - return (data as String).toInt() - } + override fun convert(data: Any): Any = (data as String).toInt() } } return null } -} \ No newline at end of file +} diff --git a/sandbox/src/commonMain/kotlin/com/example/model/github/GithubFollowerResponse.kt b/sandbox/src/commonMain/kotlin/com/example/model/github/GithubFollowerResponse.kt index c9f679c88..e28905e6b 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/github/GithubFollowerResponse.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/github/GithubFollowerResponse.kt @@ -2,23 +2,22 @@ package com.example.model.github @kotlinx.serialization.Serializable data class GithubFollowerResponseItem( - val gistsUrl: String? = null, - val reposUrl: String? = null, - val followingUrl: String? = null, - val starredUrl: String? = null, - val login: String? = null, - val followersUrl: String? = null, - val type: String? = null, - val url: String? = null, - val subscriptionsUrl: String? = null, - val receivedEventsUrl: String? = null, - val avatarUrl: String? = null, - val eventsUrl: String? = null, - val htmlUrl: String? = null, - val siteAdmin: Boolean? = null, - val id: Int? = null, - val gravatarId: String? = null, - val nodeId: String? = null, - val organizationsUrl: String? = null + val gistsUrl: String? = null, + val reposUrl: String? = null, + val followingUrl: String? = null, + val starredUrl: String? = null, + val login: String? = null, + val followersUrl: String? = null, + val type: String? = null, + val url: String? = null, + val subscriptionsUrl: String? = null, + val receivedEventsUrl: String? = null, + val avatarUrl: String? = null, + val eventsUrl: String? = null, + val htmlUrl: String? = null, + val siteAdmin: Boolean? = null, + val id: Int? = null, + val gravatarId: String? = null, + val nodeId: String? = null, + val organizationsUrl: String? = null ) - diff --git a/sandbox/src/commonMain/kotlin/com/example/model/github/Issuedata.kt b/sandbox/src/commonMain/kotlin/com/example/model/github/Issuedata.kt index b6dda7674..2ed441aaf 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/github/Issuedata.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/github/Issuedata.kt @@ -1,4 +1,7 @@ package com.example.model.github @kotlinx.serialization.Serializable -data class Issuedata(val title: String, val body: String) \ No newline at end of file +data class Issuedata( + val title: String, + val body: String +) diff --git a/sandbox/src/commonMain/kotlin/com/example/model/github/TestReee.kt b/sandbox/src/commonMain/kotlin/com/example/model/github/TestReee.kt index 801923123..389e8b8a4 100644 --- a/sandbox/src/commonMain/kotlin/com/example/model/github/TestReee.kt +++ b/sandbox/src/commonMain/kotlin/com/example/model/github/TestReee.kt @@ -1,96 +1,100 @@ package com.example.model.github data class TestReee( - val testReee: List? = null + val testReee: List? = null ) -@kotlinx.serialization.Serializable +@kotlinx.serialization.Serializable data class Author( - val date: String? = null, - val name: String? = null, - val email: String? = null, - val gistsUrl: String? = null, - val reposUrl: String? = null, - val followingUrl: String? = null, - val starredUrl: String? = null, - val login: String? = null, - val followersUrl: String? = null, - val type: String? = null, - val url: String? = null, - val subscriptionsUrl: String? = null, - val receivedEventsUrl: String? = null, - val avatarUrl: String? = null, - val eventsUrl: String? = null, - val htmlUrl: String? = null, - val siteAdmin: Boolean? = null, - val id: Int? = null, - val gravatarId: String? = null, - val nodeId: String? = null, - val organizationsUrl: String? = null + val date: String? = null, + val name: String? = null, + val email: String? = null, + val gistsUrl: String? = null, + val reposUrl: String? = null, + val followingUrl: String? = null, + val starredUrl: String? = null, + val login: String? = null, + val followersUrl: String? = null, + val type: String? = null, + val url: String? = null, + val subscriptionsUrl: String? = null, + val receivedEventsUrl: String? = null, + val avatarUrl: String? = null, + val eventsUrl: String? = null, + val htmlUrl: String? = null, + val siteAdmin: Boolean? = null, + val id: Int? = null, + val gravatarId: String? = null, + val nodeId: String? = null, + val organizationsUrl: String? = null ) -@kotlinx.serialization.Serializable +@kotlinx.serialization.Serializable data class Tree( - val sha: String? = null, - val url: String? = null + val sha: String? = null, + val url: String? = null ) + @kotlinx.serialization.Serializable data class TestReeeItem( - val committer: Committer? = null, - val author: Author? = null, - val htmlUrl: String? = null, - val commit: Commit? = null, - val commentsUrl: String? = null, - val sha: String? = null, - val url: String? = null, - val nodeId: String? = null, - val parents: List? = null + val committer: Committer? = null, + val author: Author? = null, + val htmlUrl: String? = null, + val commit: Commit? = null, + val commentsUrl: String? = null, + val sha: String? = null, + val url: String? = null, + val nodeId: String? = null, + val parents: List? = null ) + @kotlinx.serialization.Serializable data class Verification( - val reason: String? = null, - val signature: String? = null, - val payload: String? = null, - val verified: Boolean? = null + val reason: String? = null, + val signature: String? = null, + val payload: String? = null, + val verified: Boolean? = null ) + @kotlinx.serialization.Serializable data class Committer( - val date: String? = null, - val name: String? = null, - val email: String? = null, - val gistsUrl: String? = null, - val reposUrl: String? = null, - val followingUrl: String? = null, - val starredUrl: String? = null, - val login: String? = null, - val followersUrl: String? = null, - val type: String? = null, - val url: String? = null, - val subscriptionsUrl: String? = null, - val receivedEventsUrl: String? = null, - val avatarUrl: String? = null, - val eventsUrl: String? = null, - val htmlUrl: String? = null, - val siteAdmin: Boolean? = null, - val id: Int? = null, - val gravatarId: String? = null, - val nodeId: String? = null, - val organizationsUrl: String? = null + val date: String? = null, + val name: String? = null, + val email: String? = null, + val gistsUrl: String? = null, + val reposUrl: String? = null, + val followingUrl: String? = null, + val starredUrl: String? = null, + val login: String? = null, + val followersUrl: String? = null, + val type: String? = null, + val url: String? = null, + val subscriptionsUrl: String? = null, + val receivedEventsUrl: String? = null, + val avatarUrl: String? = null, + val eventsUrl: String? = null, + val htmlUrl: String? = null, + val siteAdmin: Boolean? = null, + val id: Int? = null, + val gravatarId: String? = null, + val nodeId: String? = null, + val organizationsUrl: String? = null ) + @kotlinx.serialization.Serializable data class ParentsItem( - val htmlUrl: String? = null, - val sha: String? = null, - val url: String? = null + val htmlUrl: String? = null, + val sha: String? = null, + val url: String? = null ) + @kotlinx.serialization.Serializable data class Commit( - val commentCount: Int? = null, - val committer: Committer? = null, - val author: Author? = null, - val tree: Tree? = null, - val message: String? = null, - val url: String? = null, - val verification: Verification? = null + val commentCount: Int? = null, + val committer: Committer? = null, + val author: Author? = null, + val tree: Tree? = null, + val message: String? = null, + val url: String? = null, + val verification: Verification? = null ) - diff --git a/sandbox/src/jsMain/kotlin/JsMain.kt b/sandbox/src/jsMain/kotlin/JsMain.kt index dcdb1181c..7c41327a9 100644 --- a/sandbox/src/jsMain/kotlin/JsMain.kt +++ b/sandbox/src/jsMain/kotlin/JsMain.kt @@ -1,5 +1,3 @@ - - import com.example.model.Comment import com.example.model.MyOwnResponse import com.example.model.jsonPlaceHolderApi @@ -7,15 +5,13 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch - fun main() { - GlobalScope.launch { println("Launch") when (val test = jsonPlaceHolderApi.getCommentsByPostIdResponse("3")) { is MyOwnResponse.Success -> { - val list = test.data as List + val list = test.data as List println(list.size) } @@ -26,9 +22,5 @@ fun main() { } delay(3000) - } - - } - diff --git a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/CreateIssue.kt b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/CreateIssue.kt index ea665c2b6..71be0d292 100644 --- a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/CreateIssue.kt +++ b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/CreateIssue.kt @@ -10,40 +10,36 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.Json - fun main() { - - val jvmClient = HttpClient() { - install(ContentNegotiation) { - - json(Json { isLenient = true; ignoreUnknownKeys = true; }) + val jvmClient = + HttpClient { + install(ContentNegotiation) { + json( + Json { + isLenient = true + ignoreUnknownKeys = true + } + ) + } + expectSuccess = false } - expectSuccess = false - - - } - - val jvmKtorfit = ktorfit { - baseUrl(GithubService.baseUrl) - httpClient(jvmClient) - } + val jvmKtorfit = + ktorfit { + baseUrl(GithubService.baseUrl) + httpClient(jvmClient) + } val testApi = jvmKtorfit.createGithubService() - runBlocking { - - testApi.listCommits("foso","Experimental").collect{ + testApi.listCommits("foso", "Experimental").collect { println(it.first().author) } - - // println( testApi.createIsseu(Issuedata("hey","ho"))) -//BODY {"title":"title","body":"This is a test"} - // BODY Issuedata(title=Hallo, body=hhhh) + // println( testApi.createIsseu(Issuedata("hey","ho"))) +// BODY {"title":"title","body":"This is a test"} + // BODY Issuedata(title=Hallo, body=hhhh) delay(3000) - } - -} \ No newline at end of file +} diff --git a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/HeaderTestApi.kt b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/HeaderTestApi.kt index bffe751f3..1a6953395 100644 --- a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/HeaderTestApi.kt +++ b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/HeaderTestApi.kt @@ -1,26 +1,41 @@ package de.jensklingenberg.ktorfit.demo import com.example.model.People -import de.jensklingenberg.ktorfit.http.* +import de.jensklingenberg.ktorfit.http.GET +import de.jensklingenberg.ktorfit.http.Header +import de.jensklingenberg.ktorfit.http.HeaderMap +import de.jensklingenberg.ktorfit.http.Headers +import de.jensklingenberg.ktorfit.http.Path interface HeaderTestApi { - @GET("people/{id}/") - suspend fun multipleHeader(@Path("id") peopleId: Int, @Header("huhu") name: Array,@Header("hey") name2: String): People + suspend fun multipleHeader( + @Path("id") peopleId: Int, + @Header("huhu") name: Array, + @Header("hey") name2: String + ): People @GET("people/{id}/") - suspend fun testHeaderWithArray(@Path("id") peopleId: Int, @Header("huhu") name: Array): People + suspend fun testHeaderWithArray( + @Path("id") peopleId: Int, + @Header("huhu") name: Array + ): People @GET("people/{id}/") - suspend fun testHeaderWithList(@Path("id") peopleId: Int, @Header("huhu") name: List): People - + suspend fun testHeaderWithList( + @Path("id") peopleId: Int, + @Header("huhu") name: List + ): People - @Headers("Accept2: application/json","Accept: application/json2") + @Headers("Accept2: application/json", "Accept: application/json2") @GET("people/{id}/") - suspend fun testHeaders(@Path("id") peopleId: Int ): People + suspend fun testHeaders( + @Path("id") peopleId: Int + ): People @GET("people/{id}/") - suspend fun testHeaderMap(@Path("id") peopleId: Int, @HeaderMap() name: Map?): People - - -} \ No newline at end of file + suspend fun testHeaderMap( + @Path("id") peopleId: Int, + @HeaderMap() name: Map? + ): People +} diff --git a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvMMain.kt b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvMMain.kt index b9da8cfc7..65041ab35 100644 --- a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvMMain.kt +++ b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvMMain.kt @@ -1,6 +1,5 @@ package de.jensklingenberg.ktorfit.demo - import com.example.UserFactory import com.example.api.JsonPlaceHolderApi import com.example.model.ExampleApi @@ -20,47 +19,49 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.Json +val jvmClient = + HttpClient { -val jvmClient = HttpClient { + install(Logging) { + // level = LogLevel.ALL + } - install(Logging) { - //level = LogLevel.ALL - } + install(ContentNegotiation) { + json( + Json { + isLenient = true + ignoreUnknownKeys = true + } + ) + } - install(ContentNegotiation) { - json(Json { isLenient = true; ignoreUnknownKeys = true }) + this.developmentMode = true + expectSuccess = false } - this.developmentMode = true - expectSuccess = false -} - - -val jvmKtorfit = ktorfit { - baseUrl(JsonPlaceHolderApi.baseUrl) - httpClient(jvmClient) - -} - - -val userKtorfit = ktorfit { - baseUrl("https://foso.github.io/Ktorfit/") - httpClient(jvmClient) +val jvmKtorfit = + ktorfit { + baseUrl(JsonPlaceHolderApi.baseUrl) + httpClient(jvmClient) + } - converterFactories( - FlowConverterFactory(), - MyOwnResponseConverterFactory(), - UserFactory(), - CallConverterFactory() - ) -} +val userKtorfit = + ktorfit { + baseUrl("https://foso.github.io/Ktorfit/") + httpClient(jvmClient) + + converterFactories( + FlowConverterFactory(), + MyOwnResponseConverterFactory(), + UserFactory(), + CallConverterFactory() + ) + } -val api : ExampleApi = userKtorfit.createExampleApi() +val api: ExampleApi = userKtorfit.createExampleApi() fun main() { - runBlocking { - val user = api.getUserResponse() when (user) { @@ -74,6 +75,4 @@ fun main() { } delay(3000) } - } - diff --git a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvmPlaceHolderApi.kt b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvmPlaceHolderApi.kt index 2bdb9b061..557558191 100644 --- a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvmPlaceHolderApi.kt +++ b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvmPlaceHolderApi.kt @@ -8,31 +8,48 @@ import de.jensklingenberg.ktorfit.http.* import io.ktor.client.statement.* internal interface JvmPlaceHolderApi : StarWarsApi { - @GET("people/{id}/") - suspend fun getPersonById2(@Path("id") peopleId: Int): People + suspend fun getPersonById2( + @Path("id") peopleId: Int + ): People @GET("people/{id}/") - suspend fun testQuery(@Path("id") peopleId: Int, @Query world: String? = "World"): People + suspend fun testQuery( + @Path("id") peopleId: Int, + @Query world: String? = "World" + ): People @GET("people/{id}/") - suspend fun testQueryName(@Path("id") peopleId: Int, @QueryName na : List?): People + suspend fun testQueryName( + @Path("id") peopleId: Int, + @QueryName na: List? + ): People @GET("people/{id}/") - suspend fun testQueryName2(@Path("id") peopleId: Int, @QueryName na : Map?, @QueryMap na2 : Map?): People + suspend fun testQueryName2( + @Path("id") peopleId: Int, + @QueryName na: Map?, + @QueryMap na2: Map? + ): People @Streaming @GET("people/1/") suspend fun getPostsStreaming(): HttpStatement @GET("people/{id}/") - fun getPersonById2AsResponse(@Path("id") peopleId: Int): Response + fun getPersonById2AsResponse( + @Path("id") peopleId: Int + ): Response @Headers(value = ["Content-Type: application/json"]) - @GET("people/{id}/") - suspend fun callPersonById2AsResponse(@Path("id") peopleId: Int): Call> + suspend fun callPersonById2AsResponse( + @Path("id") peopleId: Int + ): Call> @GET() - suspend fun getPersonByIdByUrl(@Url peopleId: String, @QueryMap name: Map?): People -} \ No newline at end of file + suspend fun getPersonByIdByUrl( + @Url peopleId: String, + @QueryMap name: Map? + ): People +} diff --git a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/QueryTestApi.kt b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/QueryTestApi.kt index ed9614afd..23a9d2ba5 100644 --- a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/QueryTestApi.kt +++ b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/QueryTestApi.kt @@ -1,11 +1,14 @@ package de.jensklingenberg.ktorfit.demo import com.example.model.People -import de.jensklingenberg.ktorfit.http.* +import de.jensklingenberg.ktorfit.http.GET +import de.jensklingenberg.ktorfit.http.Path +import de.jensklingenberg.ktorfit.http.Query +import de.jensklingenberg.ktorfit.http.QueryMap +import de.jensklingenberg.ktorfit.http.QueryName import ktorfit.Test interface QueryTestApi { - @GET("people/{id}/") suspend fun testQueryWithEncodedString( @Path("id") peopleId: Int, @@ -14,25 +17,46 @@ interface QueryTestApi { ): People @GET("people/{id}/") - suspend fun testQueryWithEncodedInt(@Path("id") peopleId: Int, @Query("huhu", true) name: Int): People + suspend fun testQueryWithEncodedInt( + @Path("id") peopleId: Int, + @Query("huhu", true) name: Int + ): People @GET("people/{id}/") - suspend fun testQueryWithString(@Path("id") peopleId: Int, @Query("huhu", false) name: String): People + suspend fun testQueryWithString( + @Path("id") peopleId: Int, + @Query("huhu", false) name: String + ): People @GET("people/{id}/") - suspend fun testQueryWithEncodedArray(@Path("id") peopleId: Int, @Query("huhu", true) name: Array): People + suspend fun testQueryWithEncodedArray( + @Path("id") peopleId: Int, + @Query("huhu", true) name: Array + ): People @GET("people/{id}/") - suspend fun testQueryWithList(@Path("id") peopleId: Int, @Query("huhu") name: List): People + suspend fun testQueryWithList( + @Path("id") peopleId: Int, + @Query("huhu") name: List + ): People @GET("people/{id}/") - suspend fun testQueryWithEncodedList(@Path("id") peopleId: Int, @Query("huhu", true) name: List): People + suspend fun testQueryWithEncodedList( + @Path("id") peopleId: Int, + @Query("huhu", true) name: List + ): People @GET("people/{id}/") - suspend fun testQueryName(@Path("id") peopleId: Int, @QueryName name: String): People + suspend fun testQueryName( + @Path("id") peopleId: Int, + @QueryName name: String + ): People @GET("people/{id}/") - suspend fun testQueryNameList(@Path("id") peopleId: Int, @QueryName(false) name: List): People + suspend fun testQueryNameList( + @Path("id") peopleId: Int, + @QueryName(false) name: List + ): People @GET("people/{id}/") suspend fun testQueryEncodedMap( @@ -40,6 +64,4 @@ interface QueryTestApi { @QueryMap name: Map?, @QueryMap(true) name2: Map? ): People - - -} \ No newline at end of file +} diff --git a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/TestApi.kt b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/TestApi.kt index 64de238ba..f07147314 100644 --- a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/TestApi.kt +++ b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/TestApi.kt @@ -7,36 +7,35 @@ import io.ktor.client.request.forms.* import io.reactivex.rxjava3.core.Observable import kotlinx.coroutines.flow.Flow - - - -interface TestApi { - +interface TestApi { @GET("pos4ts") fun getPosts(): Call> @GET("posts/{userId}") - suspend fun getPost(@Path("userId") myUserId: Int = 4): Post + suspend fun getPost( + @Path("userId") myUserId: Int = 4 + ): Post @POST("posts") - suspend fun postPost(@Body otherID: Post): Post + suspend fun postPost( + @Body otherID: Post + ): Post @GET("posts/{userId}") - suspend fun getPostsByUserId(@Path("userId") myUserId: Int): List + suspend fun getPostsByUserId( + @Path("userId") myUserId: Int + ): List @Headers(value = ["Accept: application/json"]) @GET("posts") fun getFlowPosts(): Flow> - @POST("upload") - suspend fun uppi(@Body map: MultiPartFormDataContent) + suspend fun uppi( + @Body map: MultiPartFormDataContent + ) @Headers(value = ["Accept: application/json"]) @GET("posts") fun getObserPosts(): Observable> - - - } - diff --git a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/TestApi2.kt b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/TestApi2.kt index 9b8e9b55f..8f47e9a09 100644 --- a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/TestApi2.kt +++ b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/TestApi2.kt @@ -6,22 +6,27 @@ import de.jensklingenberg.ktorfit.http.GET import de.jensklingenberg.ktorfit.http.Path import de.jensklingenberg.ktorfit.http.QueryName -interface TestApi2 : StarWarsApi, QueryNameTestApi { +interface TestApi2 : + StarWarsApi, + QueryNameTestApi { @GET("people/{id}/") fun tste() - } -data class Test(val name: String) - +data class Test( + val name: String +) interface QueryNameTestApi { - @GET("people/{id}/") - suspend fun testQueryName(@Path("id") peopleId: Int, @QueryName name: String): People + suspend fun testQueryName( + @Path("id") peopleId: Int, + @QueryName name: String + ): People @GET("people/{id}/") - suspend fun testQueryNameList(@Path("id") peopleId: Int, @QueryName(false) name: List): People - - -} \ No newline at end of file + suspend fun testQueryNameList( + @Path("id") peopleId: Int, + @QueryName(false) name: List + ): People +} diff --git a/sandbox/src/linuxX64Main/kotlin/LinuxMain.kt b/sandbox/src/linuxX64Main/kotlin/LinuxMain.kt index 0e0526fe3..051ab0699 100644 --- a/sandbox/src/linuxX64Main/kotlin/LinuxMain.kt +++ b/sandbox/src/linuxX64Main/kotlin/LinuxMain.kt @@ -1,8 +1,7 @@ import com.example.api.JsonPlaceHolderApi import de.jensklingenberg.ktorfit.Ktorfit import de.jensklingenberg.ktorfit.converter.FlowConverterFactory - -import io.ktor.client.* +import io.ktor.client.HttpClient import kotlinx.coroutines.runBlocking fun main() {