diff --git a/404.html b/404.html index 929c035b2..f323cd3ff 100644 --- a/404.html +++ b/404.html @@ -18,4 +18,4 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ---> Ktorfit
\ No newline at end of file +--> Ktorfit
\ No newline at end of file diff --git a/CHANGELOG/index.html b/CHANGELOG/index.html index e688a3b66..33a20581a 100644 --- a/CHANGELOG/index.html +++ b/CHANGELOG/index.html @@ -1,4 +1,4 @@ - Changelog - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Changelog

All important changes of this project must be documented in this file.

The format is based on Keep a Changelog and this project orients towards Semantic Versioning. Note: This project needs KSP to work and every new Ktorfit with an update of the KSP version is technically a breaking change. But there is no intent to bump the Ktorfit major version for every KSP update.

2.0.1

2.0.1 - 2024-08-08

Fixed

  • Endpoint with types from other module #594
  • Ktorfit plugin doesn’t include correct generate source if build directory changes #591
  • RequestConverter causing compile error #621
  • Build with Ktor 2.3.12

ktorfit-annotations

  • 2.0.1: The annotations are now also avaiable for WasmJs

compilerPlugin

  • Kotlin 2.0.0: 2.0.1-2.0.0 - 2024-08-08
  • Kotlin 2.0.10: 2.0.1-2.0.10 - 2024-08-10
  • Kotlin 2.0.20-RC: 2.0.1-2.0.20-RC - 2024-08-13
  • Kotlin 2.0.20-RC2: 2.0.1-2.0.20-RC2 - 2024-08-13
  • Kotlin 2.0.20: 2.0.1-2.0.20 - 2024-08-23

ktorfit-ksp

  • KSP 1.0.24: ktorfit-ksp-2.0.1-1.0.24 - 2024-08-08

Ktor3

The “normal” dependencies will stay on Ktor 2.x till 3.0 is stable. But here are versions that you can use when want to use Ktor3 and WasmJs

Project Version
ktorfit-lib-light-ktor-3.0.0-beta-2 Maven Central
ktorfit-lib-ktor-3.0.0-beta-2 Maven Central
ktorfit-converters-flow-ktor-3.0.0-beta-2 Maven Central
ktorfit-converters-call-ktor-3.0.0-beta-2 Maven Central
ktorfit-converters-response-ktor-3.0.0-beta-2 Maven Central

2.0.0

ktorfit-ksp-2.0.0-1.0.24 - 2024-08-06

  • Build with KSP 1.0.24

ktorfit-ksp-2.0.0-1.0.23 - 2024-07-14

- Build with KSP 1.0.23

ktorfit-ksp-2.0.0-1.0.22 - 2024-06-06

  • Build with KSP 1.0.22

2.0.0 - 2024-05-27

Changed

  • Build with KSP 1.0.21, Kotlin 2.0.0, Ktor 2.3.11
  • The needed dependencies for Ktorfit KSP processor are now included in the Ktorfit Gradle plugin. You can remove the ksp() block from your build.gradle.kts file. You still need to apply the KSP plugin.
plugins {
+ Changelog - Ktorfit       
🤔 Documentation issue? Report or edit

Changelog

All important changes of this project must be documented in this file.

The format is based on Keep a Changelog and this project orients towards Semantic Versioning. Note: This project needs KSP to work and every new Ktorfit with an update of the KSP version is technically a breaking change. But there is no intent to bump the Ktorfit major version for every KSP update.

2.1.0

  • Supported Kotlin version: (min) 2.0.0
  • Supported KSP version: (min) 1.0.24
  • Ktor version: 2.3.12

Added

Changed

  • Allow Http Delete with Body #647
  • By default, nullable response types will not throw an exception. You can now override this behavior by adding the DontSwallowExceptionsConverterFactory or your own ConverterFactory to the converterFactories. #618

Fixed

  • Task with path ‘kspCommonMainKotlinMetadata’ not found in project #593
  • Ktorfit Gradle Plugin not compatible with Android Multiplatform Library plugin #638

Ktor3

The “normal” dependencies will stay on Ktor 2.x till 3.0 is stable. But here are versions that you can use when want to use Ktor3 and WasmJs

de.jensklingenberg.ktorfit:ktorfit-lib-light-ktor-3.0.0-beta-2:2.1.0

de.jensklingenberg.ktorfit:ktorfit-lib-ktor-3.0.0-beta-2:2.1.0

de.jensklingenberg.ktorfit:ktorfit-converters-flow-ktor-3.0.0-beta-2:2.1.0

de.jensklingenberg.ktorfit:ktorfit-converters-call-ktor-3.0.0-beta-2:2.1.0

de.jensklingenberg.ktorfit:ktorfit-converters-response-ktor-3.0.0-beta-2:2.1.0

2.0.1

  • Supported Kotlin version: (min) 2.0.0 (max) 2.0.20
  • Supported KSP version: (min) 1.0.24 (max) 1.0.24
  • Ktor version: 2.3.12

2.0.1 - 2024-08-08

Fixed

  • Endpoint with types from other module #594
  • Ktorfit plugin doesn’t include correct generate source if build directory changes #591
  • RequestConverter causing compile error #621
  • Build with Ktor 2.3.12

ktorfit-annotations

  • 2.0.1: The annotations are now also available for WasmJs

compilerPlugin

  • Kotlin 2.0.0: 2.0.1-2.0.0 - 2024-08-08
  • Kotlin 2.0.10: 2.0.1-2.0.10 - 2024-08-10
  • Kotlin 2.0.20-RC: 2.0.1-2.0.20-RC - 2024-08-13
  • Kotlin 2.0.20-RC2: 2.0.1-2.0.20-RC2 - 2024-08-13
  • Kotlin 2.0.20: 2.0.1-2.0.20 - 2024-08-23

ktorfit-ksp

  • KSP 1.0.24: ktorfit-ksp-2.0.1-1.0.24 - 2024-08-08

Ktor3

The “normal” dependencies will stay on Ktor 2.x till 3.0 is stable. But here are versions that you can use when want to use Ktor3 and WasmJs

Project Version
ktorfit-lib-light-ktor-3.0.0-beta-2 Maven Central
ktorfit-lib-ktor-3.0.0-beta-2 Maven Central
ktorfit-converters-flow-ktor-3.0.0-beta-2 Maven Central
ktorfit-converters-call-ktor-3.0.0-beta-2 Maven Central
ktorfit-converters-response-ktor-3.0.0-beta-2 Maven Central

2.0.0

ktorfit-ksp-2.0.0-1.0.24 - 2024-08-06

  • Build with KSP 1.0.24

ktorfit-ksp-2.0.0-1.0.23 - 2024-07-14

- Build with KSP 1.0.23

ktorfit-ksp-2.0.0-1.0.22 - 2024-06-06

  • Build with KSP 1.0.22

2.0.0 - 2024-05-27

Changed

  • Build with KSP 1.0.21, Kotlin 2.0.0, Ktor 2.3.11
  • The needed dependencies for Ktorfit KSP processor are now included in the Ktorfit Gradle plugin. You can remove the ksp() block from your build.gradle.kts file. You still need to apply the KSP plugin.
plugins {
 id("com.google.devtools.ksp") version "CURRENT_KSP_VERSION"
 id("de.jensklingenberg.ktorfit") version "2.0.0"
 }
@@ -14,7 +14,7 @@
 

However, if you want to keep the old behavior and generate qualifiedTypename, you can set it in the Ktorfit config generateQualifiedTypeName to true in your build.gradle.kts file.

build.gradle.kts
ktorfit {
   generateQualifiedTypeName = true
 }
-

This change was made to provide more flexibility and control to the developers over the generated code. Please update your code accordingly if you were relying on the automatic generation of qualifiedTypename.

Fixed

2.0.0-rc01 - 2024-05-19

2.0.0-beta1 - 2024-04-28

Breaking Changes

The deprecated code got removed. This will simplify the codebase and make it easier to maintain. When you haven’t used the deprecated converters, there is not much you need to change. Some converters that were previously auto applied now need to be added manually. See the migration guide for more information: https://foso.github.io/Ktorfit/migration/#from-2-to-200

QualifiedTypeName in Ktorfit

In the previous versions of Ktorfit, the qualifiedTypename was always generated in the code. This was used in the TypeData.createTypeData() function to provide a fully qualified type name for the data type being used.

val _typeData = TypeData.createTypeData(
+

This change was made to provide more flexibility and control to the developers over the generated code. Please update your code accordingly if you were relying on the automatic generation of qualifiedTypename.

Fixed

2.0.0-rc01 - 2024-05-19

2.0.0-beta1 - 2024-04-28

Breaking Changes

The deprecated code got removed. This will simplify the codebase and make it easier to maintain. When you haven’t used the deprecated converters, there is not much you need to change. Some converters that were previously auto applied now need to be added manually. See the migration guide for more information: https://foso.github.io/Ktorfit/migration/#from-2-to-200

QualifiedTypeName in Ktorfit

In the previous versions of Ktorfit, the qualifiedTypename was always generated in the code. This was used in the TypeData.createTypeData() function to provide a fully qualified type name for the data type being used.

val _typeData = TypeData.createTypeData(
     typeInfo = typeInfo<Call<People>>(),
     qualifiedTypename = "de.jensklingenberg.ktorfit.Call<com.example.model.People>"
 )
@@ -24,10 +24,10 @@
 

However, if you want to keep the old behavior and generate qualifiedTypename, you can set a KSP argument Ktorfit_QualifiedTypeName to true in your build.gradle.kts file.

ksp {
     arg("Ktorfit_QualifiedTypeName", "true")
 }
-

This change was made to provide more flexibility and control to the developers over the generated code. Please update your code accordingly if you were relying on the automatic generation of qualifiedTypename.

1.14.0 - 2024-04-15

  • Build with KSP 1.0.20, Kotlin 2.0.0-RC1, Ktor 2.3.10

1.13.0 - 2024-04-14

  • Build with KSP 1.0.20, Kotlin 1.9.23, Ktor 2.3.10

1.12.0 - 2024-01-16

  • Compatible with KSP 1.0.16 and Kotlin 1.9.22

1.11.1 - 2023-12-21

  • Fix compile errors #505 #496

1.11.0 - 2023-12-06

Compatible with KSP 1.0.15 and Kotlin 1.9.21

Changed

  • KSP 1.0.15 required
  • Upgrade dependencies: Ktor 2.3.6

1.10.2 - 2023-11-29

Fixed

  • fix compile errors because of missing import #490 #486

1.10.1 - 2023-11-15

Fixed

  • Using @FieldMap generates uncompiled code due to missing import #479

1.10.0 - 2023-11-06

Compatible with KSP 1.0.14 and Kotlin 1.9.20

Added

  • LinuxArm64 support #475

Changed

  • KSP 1.0.14 required
  • Optimized code generation

1.9.1 - 2023-10-22

Compatible with KSP 1.0.13 and Kotlin 1.9.10/1.9.20-RC

When you are still using the Ktorfit Gradle plugin in version 1.0.0, please also update this to 1.9.1

Added

Changed

  • The generated code will not produce warnings anymore

1.8.1 - 2023-10-09

Compatible with KSP 1.0.13 and Kotlin 1.9.10/1.9.20-Beta2

Added

Changed

  • Allow nullable body type #424
  • Use @Path parameter name as default value #426
  • Use @Query parameter name as default value #428
  • Use @Field parameter name as default value #430
  • You can now also get exceptions like NetworkException with SuspendResponseConverter.#389

Deprecated

! When you still use the deprecated converters because there are use cases which you can’t do with the converter factories, please write a GitHub Issue. The deprecated converters will be removed in one of the upcoming versions

Removed

Fixed

Security

1.8.0 - 2023-10-09

Skip this version, it had an issue with Kotlin 1.9.20

1.7.0-1.9.20-Beta2 - 2023-09-16

Version of 1.7.0 that is compatible with Kotlin 1.9.20-Beta2

1.7.0 - 2023-09-16

Gradle plugin

From now on with every Ktorfit release there will also be a Gradle plugin with the same version. That means that drop you can drop the Gradle extension block where you previously set the version number and just bump the number of the Gradle plugin. https://foso.github.io/Ktorfit/installation/

plugins {
+

This change was made to provide more flexibility and control to the developers over the generated code. Please update your code accordingly if you were relying on the automatic generation of qualifiedTypename.

1.14.0 - 2024-04-15

  • Build with KSP 1.0.20, Kotlin 2.0.0-RC1, Ktor 2.3.10

1.13.0 - 2024-04-14

  • Build with KSP 1.0.20, Kotlin 1.9.23, Ktor 2.3.10

1.12.0 - 2024-01-16

  • Compatible with KSP 1.0.16 and Kotlin 1.9.22

1.11.1 - 2023-12-21

  • Fix compile errors #505 #496

1.11.0 - 2023-12-06

Compatible with KSP 1.0.15 and Kotlin 1.9.21

Changed

  • KSP 1.0.15 required
  • Upgrade dependencies: Ktor 2.3.6

1.10.2 - 2023-11-29

Fixed

  • fix compile errors because of missing import #490 #486

1.10.1 - 2023-11-15

Fixed

  • Using @FieldMap generates uncompiled code due to missing import #479

1.10.0 - 2023-11-06

Compatible with KSP 1.0.14 and Kotlin 1.9.20

Added

  • LinuxArm64 support #475

Changed

  • KSP 1.0.14 required
  • Optimized code generation

1.9.1 - 2023-10-22

Compatible with KSP 1.0.13 and Kotlin 1.9.10/1.9.20-RC

When you are still using the Ktorfit Gradle plugin in version 1.0.0, please also update this to 1.9.1

Added

Changed

  • The generated code will not produce warnings anymore

1.8.1 - 2023-10-09

Compatible with KSP 1.0.13 and Kotlin 1.9.10/1.9.20-Beta2

Added

Changed

  • Allow nullable body type #424
  • Use @Path parameter name as default value #426
  • Use @Query parameter name as default value #428
  • Use @Field parameter name as default value #430
  • You can now also get exceptions like NetworkException with SuspendResponseConverter.#389

Deprecated

! When you still use the deprecated converters because there are use cases which you can’t do with the converter factories, please write a GitHub Issue. The deprecated converters will be removed in one of the upcoming versions

Removed

Fixed

Security

1.8.0 - 2023-10-09

Skip this version, it had an issue with Kotlin 1.9.20

1.7.0-1.9.20-Beta2 - 2023-09-16

Version of 1.7.0 that is compatible with Kotlin 1.9.20-Beta2

1.7.0 - 2023-09-16

Gradle plugin

From now on with every Ktorfit release there will also be a Gradle plugin with the same version. That means that drop you can drop the Gradle extension block where you previously set the version number and just bump the number of the Gradle plugin. https://foso.github.io/Ktorfit/installation/

plugins {
 id("de.jensklingenberg.ktorfit") version "1.7.0"
 }
-

Added

  • Added a compiler type checks if the type used for the create function is an interface

Changed

  • Upgrade dependencies: Ktor 2.3.4

1.6.0 - 2023-08-24

Changed

  • KSP version 1.9.10-1.0.13 is now required
  • Upgrade dependencies: Ktor 2.3.3

1.5.0 - 2023-08-04

Changed

  • KSP version 1.9.0-1.0.13 is now needed

1.4.4 - 2023-07-26

Changed

  • Upgrade dependencies: Ktor 2.3.2

1.4.3 - 2023-07-13

Fixed

372 Crash with Xiaomi on create Ktorfit.Builder by @princeparadoxes

1.4.2 - 2023-06-25

Fixed

323 Code generation issue for @Multipart / @FormUrlEncoded by @Ph1ll1pp

1.4.1 - 2023-06-03

Changed

  • Upgrade dependencies: Ktor 2.3.1

Fixed

236 Parsing error for list/array

1.4.0 - 2023-05-27

Added

  • 85 Added a Response class that can be used as a wrapper around the API Response, the converter for it is automatically applied. thx to @vovahost, @DATL4G

e.g.

interface ExampleApi{
+

Added

  • Added a compiler type checks if the type used for the create function is an interface

Changed

  • Upgrade dependencies: Ktor 2.3.4

1.6.0 - 2023-08-24

Changed

  • KSP version 1.9.10-1.0.13 is now required
  • Upgrade dependencies: Ktor 2.3.3

1.5.0 - 2023-08-04

Changed

  • KSP version 1.9.0-1.0.13 is now needed

1.4.4 - 2023-07-26

Changed

  • Upgrade dependencies: Ktor 2.3.2

1.4.3 - 2023-07-13

Fixed

372 Crash with Xiaomi on create Ktorfit.Builder by @princeparadoxes

1.4.2 - 2023-06-25

Fixed

323 Code generation issue for @Multipart / @FormUrlEncoded by @Ph1ll1pp

1.4.1 - 2023-06-03

Changed

  • Upgrade dependencies: Ktor 2.3.1

Fixed

236 Parsing error for list/array

1.4.0 - 2023-05-27

Added

  • 85 Added a Response class that can be used as a wrapper around the API Response, the converter for it is automatically applied. thx to @vovahost, @DATL4G

e.g.

interface ExampleApi{
   suspend fun getUser(): Response<User>
 }
 
@@ -38,7 +38,7 @@
 }else{
   user.errorBody()
 }
-
  • Ktorfit is now using converters factories to apply the converters, similar to Retrofit see more here https://foso.github.io/Ktorfit/converters/converters/
  • TypeData now has a field “typeInfo” can be used to convert the Ktor HttpResponse body to the wanted type
  • CallConverterFactory for replacement of CallResponseConverter
  • FlowConverterFactory for replacement of FlowResponseConverter

  • Added support for targets: macosArm64, tvosArm64, tvosX64, tvosSimulatorArm64, watchosSimulatorArm64 #315

Changed

  • Upgrade dependencies: Kotlin 1.8.21

Deprecated

  • ResponseConverter, use Converter.ResponseConverter instead
  • SuspendResponseConverter, use Converter.SuspendResponseConverter instead
  • RequestConverter, use Converter.RequestParameterConverter instead
  • See also: https://foso.github.io/Ktorfit/converters/migration/

1.3.0 - 2023-05-14

Changed

  • Optimized generated code, the generated code that is used for a request will now directly set the Ktor code instead of delegating it to a Ktorfit class. This will make the code easier to understand.

  • KSP version 1.0.11 is now needed

Fixed

[Bug]: IllegalArgumentException with Custom Http Annotation #274

Bumped

KSP version to 1.0.11

1.2.0 - 2023-05-05

Bumped

Now based on Ktor 2.3.0

1.1.0 - 2023-04-15

Added

From now on there are two versions of the ktorfit-lib.

“de.jensklingenberg.ktorfit:ktorfit-lib” will stay like before and add the platform client dependencies for the clients.

“de.jensklingenberg.ktorfit:ktorfit-lib-light” this will only add the client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as “ktorfit-lib”

Changed

  • Kotlin version 1.8.20 is now needed
  • KSP version 1.8.20-1.0.10 is now needed

Bumped

  • Kotlin to 1.8.20
  • KSP version to 1.8.20-1.0.10

1.0.1 - 2023-03-20

Added

From now on there are two versions of the ktorfit-lib.

“de.jensklingenberg.ktorfit:ktorfit-lib” will stay like before and add the platform client dependencies for the clients.

“de.jensklingenberg.ktorfit:ktorfit-lib-light” this will only add the client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as “ktorfit-lib”

Fixed

[Bug]: Post request body serialization doesn’t work #202


1.0.0 - 2023-03-02

This project is now following semver

Added

  • internal optimizations
  • throw compile error when generated class can not be found

Fixed

  • Timeout throws exception outside of scope of SuspendResponseConverter #127
  • Fix broken/outdated docs link (#140) by @T-Spoon

Bumped

  • based on Ktor 2.2.4

1.0.0-beta18 (12-02-2023)

NEW:

  • You can now disable the check if the baseUrl ends with a /
Ktorfit.Builder().baseUrl(testBaseUrl, checkUrl = false).build()
+
  • Ktorfit is now using converters factories to apply the converters, similar to Retrofit see more here https://foso.github.io/Ktorfit/converters/converters/
  • TypeData now has a field “typeInfo” can be used to convert the Ktor HttpResponse body to the wanted type
  • CallConverterFactory for replacement of CallResponseConverter
  • FlowConverterFactory for replacement of FlowResponseConverter

  • Added support for targets: macosArm64, tvosArm64, tvosX64, tvosSimulatorArm64, watchosSimulatorArm64 #315

Changed

  • Upgrade dependencies: Kotlin 1.8.21

Deprecated

  • ResponseConverter, use Converter.ResponseConverter instead
  • SuspendResponseConverter, use Converter.SuspendResponseConverter instead
  • RequestConverter, use Converter.RequestParameterConverter instead
  • See also: https://foso.github.io/Ktorfit/converters/migration/

1.3.0 - 2023-05-14

Changed

  • Optimized generated code, the generated code that is used for a request will now directly set the Ktor code instead of delegating it to a Ktorfit class. This will make the code easier to understand.

  • KSP version 1.0.11 is now needed

Fixed

[Bug]: IllegalArgumentException with Custom Http Annotation #274

Bumped

KSP version to 1.0.11

1.2.0 - 2023-05-05

Bumped

Now based on Ktor 2.3.0

1.1.0 - 2023-04-15

Added

From now on there are two versions of the ktorfit-lib.

“de.jensklingenberg.ktorfit:ktorfit-lib” will stay like before and add the platform client dependencies for the clients.

“de.jensklingenberg.ktorfit:ktorfit-lib-light” this will only add the client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as “ktorfit-lib”

Changed

  • Kotlin version 1.8.20 is now needed
  • KSP version 1.8.20-1.0.10 is now needed

Bumped

  • Kotlin to 1.8.20
  • KSP version to 1.8.20-1.0.10

1.0.1 - 2023-03-20

Added

From now on there are two versions of the ktorfit-lib.

“de.jensklingenberg.ktorfit:ktorfit-lib” will stay like before and add the platform client dependencies for the clients.

“de.jensklingenberg.ktorfit:ktorfit-lib-light” this will only add the client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as “ktorfit-lib”

Fixed

[Bug]: Post request body serialization doesn’t work #202


1.0.0 - 2023-03-02

This project is now following semver

Added

  • internal optimizations
  • throw compile error when generated class can not be found

Fixed

  • Timeout throws exception outside of scope of SuspendResponseConverter #127
  • Fix broken/outdated docs link (#140) by @T-Spoon

Bumped

  • based on Ktor 2.2.4

1.0.0-beta18 (12-02-2023)

NEW:

  • You can now disable the check if the baseUrl ends with a /
Ktorfit.Builder().baseUrl(testBaseUrl, checkUrl = false).build()
 

🐛 Bugs fixed

  • Fixed Ktorfit breaking incremental compilation #110

⬆️ Deps updates

  • based on Ktor 2.2.3

1.0.0-beta17 (21-01-2023)

💥 Breaking changes:

  • Ktorfit now needs an additional gradle plugin. This will solve several issues with multi-module projects.

Add this plugin:

plugins {
   id "de.jensklingenberg.ktorfit" version "1.0.0"
 }
@@ -61,4 +61,4 @@
 "Authorization: token ghp_abcdefgh",
 "Content-Type: application/json"
 )
-

1.0.0-beta09

  • 15 fix encoding of query parameters

1.0.0-beta08

  • fix issue with Koin Annotations

1.0.0-beta07

  • fix issue with FormUrlEncoded
  • based on Ktor 2.0.2

1.0.0-beta06

  • fix issue with KSP 1.0.5 #19

1.0.0-beta05

  • fixed: Custom Http Method with @Body is now possible #6
  • based on Ktor 2.0.1
  • cleanup example project @mattrob33

1.0.0-beta04

initial release


Last update: August 23, 2024
\ No newline at end of file +

1.0.0-beta09

  • 15 fix encoding of query parameters

1.0.0-beta08

  • fix issue with Koin Annotations

1.0.0-beta07

  • fix issue with FormUrlEncoded
  • based on Ktor 2.0.2

1.0.0-beta06

  • fix issue with KSP 1.0.5 #19

1.0.0-beta05

  • fixed: Custom Http Method with @Body is now possible #6
  • based on Ktor 2.0.1
  • cleanup example project @mattrob33

1.0.0-beta04

initial release


Last update: September 1, 2024
\ No newline at end of file diff --git a/License/index.html b/License/index.html index 9367d579d..6fdabd7cf 100644 --- a/License/index.html +++ b/License/index.html @@ -1,4 +1,4 @@ - License - Ktorfit
🤔 Documentation issue? Report or edit

License

                             Apache License
+ License - Ktorfit      
🤔 Documentation issue? Report or edit

License

                             Apache License
                        Version 2.0, January 2004
                     http://www.apache.org/licenses/
 

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

  1. Definitions.

    “License” shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.

    “Licensor” shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.

    “Legal Entity” shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, “control” means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.

    “You” (or “Your”) shall mean an individual or Legal Entity exercising permissions granted by this License.

    “Source” form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.

    “Object” form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.

    “Work” shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).

    “Derivative Works” shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.

    “Contribution” shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, “submitted” means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as “Not a Contribution.”

    “Contributor” shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.

  2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.

  3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.

  4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:

    (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and

    (b) You must cause any modified files to carry prominent notices stating that You changed the files; and

    © You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and

    (d) If the Work includes a “NOTICE” text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.

    You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.

  5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.

  6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.

  7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.

  8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.

  9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

  To apply the Apache License to your work, attach the following
@@ -10,4 +10,4 @@
   same "printed page" as the copyright notice for easier
   identification within third-party archives.
 

Copyright 2022 Jens Klingenberg

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.


Last update: March 3, 2023
\ No newline at end of file +

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.


Last update: March 3, 2023
\ No newline at end of file diff --git a/android/proguard/index.html b/android/proguard/index.html index a8dce6493..64a94d146 100644 --- a/android/proguard/index.html +++ b/android/proguard/index.html @@ -1,3 +1,3 @@ - proguard - Ktorfit
🤔 Documentation issue? Report or edit

proguard

proguard-rules.pro

-keep class de.jensklingenberg.ktorfit.** { *; }
+ proguard - Ktorfit      
🤔 Documentation issue? Report or edit

proguard

proguard-rules.pro

-keep class de.jensklingenberg.ktorfit.** { *; }
 -keepclassmembers class de.jensklingenberg.ktorfit.** { *; }
-


Last update: August 26, 2024
\ No newline at end of file +


Last update: August 26, 2024
\ No newline at end of file diff --git a/architecture/index.html b/architecture/index.html index 735f8b049..a0d9f1ad6 100644 --- a/architecture/index.html +++ b/architecture/index.html @@ -1,4 +1,4 @@ - Architecture - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

How Ktorfit works under the hood

Ktorfit consists of three main components KSP-Plugin, Compiler plugin and the Ktorfit lib

KSP-Plugin

This will generate the code for the implementation of the interfaces

Compiler plugin

This transforms the create() function from the Ktorfit lib

Ktorfit lib

A wrapper around Ktor to simplify code generation

Example

package com.example
+ Architecture - Ktorfit       
🤔 Documentation issue? Report or edit

How Ktorfit works under the hood

Ktorfit consists of three main components KSP-Plugin, Compiler plugin and the Ktorfit lib

KSP-Plugin

This will generate the code for the implementation of the interfaces

Compiler plugin

This transforms the create() function from the Ktorfit lib

Ktorfit lib

A wrapper around Ktor to simplify code generation

Example

package com.example
 
 import com.example.model.People
 import de.jensklingenberg.ktorfit.http.GET
@@ -11,7 +11,7 @@
 public class _ExampleApiImpl(
     private val _ktorfit: Ktorfit,
 ) : ExampleApi {
-    public val _converter: KtorfitConverterHelper = KtorfitConverterHelper(_ktorfit)
+    private val _helper: KtorfitConverterHelper = KtorfitConverterHelper(_ktorfit)
 
     override suspend fun exampleGet(): People {
         val _ext: HttpRequestBuilder.() -> Unit = {
@@ -20,14 +20,18 @@
                 takeFrom(_ktorfit.baseUrl + "/test")
             }
         }
-        val _typeData = TypeData.createTypeData(qualifiedTypename = "com.example.model.People",
-            typeInfo = typeInfo<People>())
-
-        return _converter.suspendRequest<People, People>(_typeData,_ext)!!
+        val _typeData = TypeData.createTypeData(
+            typeInfo = typeInfo<People>(),
+        )
+        return _helper.suspendRequest(_typeData,_ext)!!
     }
 }
 
-public fun Ktorfit.createExampleApi(): ExampleApi = this.create(_ExampleApiImpl(this))
+public class _ExampleApiProvider : ClassProvider<ExampleApi> {
+    override fun create(_ktorfit: Ktorfit): ExampleApi = _ExampleApiImpl(_ktorfit)
+}
+
+public fun Ktorfit.createExampleApi(): ExampleApi = _ExampleApiImpl(this)
 

The next part is the compiler plugin which is added by the gradle plugin. It looks for the every usage of the create function from the Ktorfit-lib and adds an object of the wanted implementation class as an argument. Because of the naming convention of the generated classes we can deduce the name of the class from the name of type parameter.

val api = jvmKtorfit.create<ExampleApi>()
 

will be transformed to:

val api = jvmKtorfit.create<ExampleApi>(_ExampleApiImpl(jvmKtorfit))
 

The create() function is used, checks that the compiler plugin replaced the default value

public fun <T> create(data: T? = null): T {
@@ -36,4 +40,4 @@
     }
     return data
 }
-

Last update: October 29, 2023
\ No newline at end of file +

Last update: September 1, 2024
\ No newline at end of file diff --git a/configuration/index.html b/configuration/index.html index 1b523dcdc..9ebf07f2d 100644 --- a/configuration/index.html +++ b/configuration/index.html @@ -1,7 +1,7 @@ - Configuration - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Compile errors

By default, Ktorfit will throw compile error when it finds conditions under which it can’t ensure that it will work correct. You can set it in the Ktorfit config to change this

ktorfit{
+ Configuration - Ktorfit       
🤔 Documentation issue? Report or edit

Gradle

Compile errors

By default, Ktorfit will throw compile error when it finds conditions under which it can’t ensure that it will work correct. You can set it in the Ktorfit config to change this

ktorfit{
     errorCheckingMode = ErrorCheckingMode.NONE
 }
-

You can set it in your build.gradle.kts file,

  • NONE : Turn off all Ktorfit related error checking

  • ERROR: Check for errors

  • WARNING: Turn errors into warnings

QualifiedTypeName

By default, Ktorfit will keep qualifiedTypename for TypeData in the generated code empty. You can set it in the Ktorfit config to change this:

ktorfit {
+

You can set it in your build.gradle.kts file,

  • NONE : Turn off all Ktorfit related error checking

  • ERROR: Check for errors

  • WARNING: Turn errors into warnings

QualifiedTypeName

By default, Ktorfit will keep qualifiedTypename for TypeData in the generated code empty. You can set it in the Ktorfit config to change this:

ktorfit {
     generateQualifiedTypeName = true
 }
 
Default code generation
...
@@ -15,6 +15,6 @@
     qualifiedTypename = "de.jensklingenberg.ktorfit.Call<com.example.model.People>"
 )
 ...
-

Add your own Ktor client

You can set your Ktor client instance to the Ktorfit builder:

val myClient = HttpClient()
+

Ktorfit Builder

Add your own Ktor client

You can set your Ktor client instance to the Ktorfit builder:

val myClient = HttpClient()
 val ktorfit = Ktorfit.Builder().httpClient(myClient).build()
-

Last update: May 27, 2024
\ No newline at end of file +

Last update: September 1, 2024
\ No newline at end of file diff --git a/converters/converters/index.html b/converters/converters/index.html index c57b9f706..13ed4556e 100644 --- a/converters/converters/index.html +++ b/converters/converters/index.html @@ -1,4 +1,4 @@ - Converters - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Converters

Converters are used to convert the HTTPResponse or parameters.

They are added inside of a Converter.Factory which will then be added to the Ktorfit builder with the converterfactories() function.

Converter Types

Existing converter factories

  • CallConverterFactory

Add this dependency:

implementation("de.jensklingenberg.ktorfit:ktorfit-converters-call:$CONVERTER_VERSION")
+ Converters - Ktorfit       
🤔 Documentation issue? Report or edit

Converters

Converters are used to convert the HTTPResponse or parameters.

They are added inside of a Converter.Factory which will then be added to the Ktorfit builder with the converterfactories() function.

Converter Types

Existing converter factories

  • CallConverterFactory

Add this dependency:

implementation("de.jensklingenberg.ktorfit:ktorfit-converters-call:$CONVERTER_VERSION")
 

You can find all available versions here

  • FlowConverterFactory

Add this dependency:

implementation("de.jensklingenberg.ktorfit:ktorfit-converters-flow:$CONVERTER_VERSION")
 

You can find all available versions here

  • ResponseConverterFactory

Add this dependency:

implementation("de.jensklingenberg.ktorfit:ktorfit-converters-response:$CONVERTER_VERSION")
-

You can find all available versions here


Last update: May 26, 2024
\ No newline at end of file +

You can find all available versions here


Last update: May 26, 2024
\ No newline at end of file diff --git a/converters/example1/index.html b/converters/example1/index.html index f59cad187..8c0e51b85 100644 --- a/converters/example1/index.html +++ b/converters/example1/index.html @@ -1,4 +1,4 @@ - Example1 - Ktorfit
🤔 Documentation issue? Report or edit

Example1

Let’s say you want to get an user from an API and the response you get looks like below:

API response
{
+ Example1 - Ktorfit      
🤔 Documentation issue? Report or edit

Example1

Let’s say you want to get an user from an API and the response you get looks like below:

API response
{
     "success": true,
     "user":
     {
@@ -50,4 +50,4 @@
     return envelope.user
 }
 

Finally, add your converter factory to the Ktorfit Builder

Ktorfit.Builder().converterFactories(UserFactory()).baseUrl("foo").build()
-

Last update: May 27, 2023
\ No newline at end of file +

Last update: May 27, 2023
\ No newline at end of file diff --git a/converters/migration/index.html b/converters/migration/index.html index ba33969d6..fd6e2fff0 100644 --- a/converters/migration/index.html +++ b/converters/migration/index.html @@ -1,4 +1,4 @@ - Migration - Ktorfit
🤔 Documentation issue? Report or edit

Migration

  • SuspendResponseConverter -> Converter.SuspendResponseConverter
SuspendResponseConverter
 override suspend fun <RequestType> wrapSuspendResponse(
+ Migration - Ktorfit      
🤔 Documentation issue? Report or edit

Migration

  • SuspendResponseConverter -> Converter.SuspendResponseConverter
SuspendResponseConverter
 override suspend fun <RequestType> wrapSuspendResponse(
     typeData: TypeData,
     requestFunction: suspend () -> Pair<TypeInfo, HttpResponse>,
     ktorfit: Ktorfit
@@ -98,4 +98,4 @@
         return null
     }
 }
-

Last update: May 27, 2023
\ No newline at end of file +

Last update: May 27, 2023
\ No newline at end of file diff --git a/converters/requestparameterconverter/index.html b/converters/requestparameterconverter/index.html index e59858d02..e51f634fc 100644 --- a/converters/requestparameterconverter/index.html +++ b/converters/requestparameterconverter/index.html @@ -1,4 +1,4 @@ - RequestParameterConverter - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

RequestParameterConverter

@GET("posts/{postId}/comments")
+ RequestParameterConverter - Ktorfit       
🤔 Documentation issue? Report or edit

RequestParameterConverter

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

You can set RequestType at a parameter with a type to which the parameter should be converted.

Then you need to implement a Converter factory with a RequestParameterConverter.

class StringToIntRequestConverterFactory : Converter.Factory {
     override fun requestParameterConverter(
@@ -13,4 +13,4 @@
     }
 }
 
ktorfit.converterFactories(StringToIntRequestConverterFactory())
-

Last update: May 27, 2023
\ No newline at end of file +

Last update: May 27, 2023
\ No newline at end of file diff --git a/converters/responseconverter/index.html b/converters/responseconverter/index.html index bfe7c2efd..a7beb9d46 100644 --- a/converters/responseconverter/index.html +++ b/converters/responseconverter/index.html @@ -1,4 +1,4 @@ - ResponseConverter - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

ResponseConverter

You only need ResponseConverters for cases where you can’t use a suspend function in your interface. For every other case you want SuspendResponseConverter

Because Ktor relies on Coroutines by default your functions need to have the suspend modifier.

Let’s say you have API endpoint to get a list of comments and you want to get them as Flow.

Ktorfit already has a converter for Flow, but it’s used as an example

@GET("/user")
+ ResponseConverter - Ktorfit       
🤔 Documentation issue? Report or edit

ResponseConverter

You only need ResponseConverters for cases where you can’t use a suspend function in your interface. For every other case you want SuspendResponseConverter

Because Ktor relies on Coroutines by default your functions need to have the suspend modifier.

Let’s say you have API endpoint to get a list of comments and you want to get them as Flow.

Ktorfit already has a converter for Flow, but it’s used as an example

@GET("/user")
 fun getUser(): Flow<List<Commment>>
 

Now you need a converter that can convert the HTTPResponse and return a Flow. Create a class that extends Converter.Factory

class FlowConverterFactory : Converter.Factory {
 }
@@ -55,4 +55,4 @@
         //Do something with exception
     }
 })
-

You can use Call to receive the response in a Callback.


Last update: May 26, 2024
\ No newline at end of file +

You can use Call to receive the response in a Callback.


Last update: May 26, 2024
\ No newline at end of file diff --git a/converters/suspendresponseconverter/index.html b/converters/suspendresponseconverter/index.html index a346d7a6d..d236c053f 100644 --- a/converters/suspendresponseconverter/index.html +++ b/converters/suspendresponseconverter/index.html @@ -1,4 +1,4 @@ - SuspendResponseConverter - Ktorfit
🤔 Documentation issue? Report or edit

SuspendResponseConverter

Let`s say you have a function that requests a list of comments

@GET("posts/{postId}/comments")
+ SuspendResponseConverter - Ktorfit      
🤔 Documentation issue? Report or edit

SuspendResponseConverter

Let`s say you have a function that requests a list of comments

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

But now you want to directly wrap your comment list in your data holder class e.g. “MyOwnResponse”

sealed class MyOwnResponse<T> {
     data class Success<T>(val data: T) : MyOwnResponse<T>()
@@ -33,4 +33,4 @@
 

You can then add the ResponseConverter to your Ktorfit object.

ktorfit.converterFactories(MyOwnResponseConverterFactory())
 

Now add MyOwnResponse to your function

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


Last update: May 27, 2023
\ No newline at end of file +


Last update: May 27, 2023
\ No newline at end of file diff --git a/development/index.html b/development/index.html index fc0da2324..7be69c4c5 100644 --- a/development/index.html +++ b/development/index.html @@ -1 +1 @@ - Development - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Development

Update Ktorfit for new Kotlin version

  • Bump kotlin in libs.versions
  • Change ktorfitCompiler in libs.versions to KTORFIT_VERSION-NEW_KOTLIN_VERSION
  • Run tests in :ktorfit-compiler-plugin
  • Create a PR against master
  • Merge PR
  • Run GitHub Actions “publish” workflow

Last update: August 23, 2024
\ No newline at end of file + Development - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Development

Update Ktorfit for new Kotlin version

  • Bump kotlin in libs.versions
  • Change ktorfitCompiler in libs.versions to KTORFIT_VERSION-NEW_KOTLIN_VERSION
  • Run tests in :ktorfit-compiler-plugin
  • Create a PR against master
  • Merge PR
  • Run GitHub Actions “publish” workflow

👷 Project Structure

  • compiler plugin - module with source for the compiler plugin
  • ktorfit-annotations - module with annotations for the Ktorfit
  • ktorfit-ksp - module with source for the KSP plugin
  • ktorfit-lib-core - module with source for the Ktorfit lib
  • ktorfit-lib - ktorfit-lib-core + dependencies on platform specific clients
  • sandbox - experimental test module to try various stuff

  • example - contains example projects that use Ktorfit

  • docs - contains the source for the GitHub page

Last update: September 1, 2024
\ No newline at end of file diff --git a/fundamentals/scope/index.html b/fundamentals/scope/index.html index 688383ebf..414dc0bac 100644 --- a/fundamentals/scope/index.html +++ b/fundamentals/scope/index.html @@ -1 +1 @@ - Scope - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Scope of Ktorfit

The goal of Ktorfit is to provide a similar developer experience like Retrofit for Kotlin Multiplatform projects. It`s not a 100% drop-in replacement for Retrofit. It uses Ktor clients because they are available on nearly every compile target of KMP. Every feature should be implemented so that it works on all platforms that Ktor supports. Before a new functionality is added to Ktorfit, it should be checked if there is already a Ktor plugin for it which solves the same problem.


Last update: March 4, 2023
\ No newline at end of file + Scope - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Scope of Ktorfit

The goal of Ktorfit is to provide a similar developer experience like Retrofit for Kotlin Multiplatform projects. It`s not a 100% drop-in replacement for Retrofit. It uses Ktor clients because they are available on nearly every compile target of KMP. Every feature should be implemented so that it works on all platforms that Ktor supports. Before a new functionality is added to Ktorfit, it should be checked if there is already a Ktor plugin for it which solves the same problem.


Last update: March 4, 2023
\ No newline at end of file diff --git a/index.html b/index.html index 958cf45dd..0bfea7db6 100644 --- a/index.html +++ b/index.html @@ -1 +1 @@ - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Ktorfit

Maven License

Introduction

Ktorfit is a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit

Compatibility

Ktorfit-version Kotlin KSP Ktor
2.0.1 >=2.0.0 >=1.0.24 min 2.3.12
2.0.0 2.0.0 1.0.21 (min) 1.0.24 (max) 2.3.11

Installation

Please see Installation

Quick start

Please see Quick start

Requests

See Requests

Converters

See documentation Here

Changelog

See changelog

Acknowledgments

Some parts of this project are reusing ideas that are originally coming from Retrofit from Square. Thank you for Retrofit!

Thanks to JetBrains for Ktor and Kotlin!

Contributions

When you find unexpected behaviour please write an issue


Last update: August 26, 2024
\ No newline at end of file + Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Ktorfit

Maven License

Introduction

Ktorfit is a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit

Compatibility

Ktorfit-version
2.1.0 https://github.com/Foso/Ktorfit/blob/master/docs/CHANGELOG.md#210
2.0.1 https://github.com/Foso/Ktorfit/blob/master/docs/CHANGELOG.md#201
2.0.0 https://github.com/Foso/Ktorfit/blob/master/docs/CHANGELOG.md#200

Installation

Please see Installation

Quick start

Please see Quick start

Requests

See Requests

Converters

See documentation Here

Changelog

See changelog

Acknowledgments

Some parts of this project are reusing ideas that are originally coming from Retrofit from Square. Thank you for Retrofit!

Thanks to JetBrains for Ktor and Kotlin!

Contributions

When you find unexpected behaviour please write an issue


Last update: September 1, 2024
\ No newline at end of file diff --git a/installation/index.html b/installation/index.html index f12375347..2eca87a15 100644 --- a/installation/index.html +++ b/installation/index.html @@ -1,8 +1,8 @@ - Installation - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Installation

Setup

(You can also look how it’s done in the examples)

Gradle Plugins

You need to add KSP and the Ktorfit Gradle plugin

plugins {
+ Installation - Ktorfit       
🤔 Documentation issue? Report or edit

Installation

Setup

(You can also look how it’s done in the examples)

Gradle Plugins

You need to add KSP and the Ktorfit Gradle plugin

plugins {
   id("com.google.devtools.ksp") version "CURRENT_KSP_VERSION"
-  id("de.jensklingenberg.ktorfit") version "2.0.1"
+  id("de.jensklingenberg.ktorfit") version "2.1.0"
 }
-

Ktorfit-lib

Add the Ktorfit-lib to your common module. You can find all available versions here

val ktorfitVersion = "2.0.1"
+

Ktorfit-lib

Add the Ktorfit-lib to your common module. You can find all available versions here

val ktorfitVersion = "2.1.0"
 
 sourceSets {
     val commonMain by getting{
@@ -10,4 +10,4 @@
             implementation("de.jensklingenberg.ktorfit:ktorfit-lib:$ktorfitVersion")
         }
     }
-

You can also use “de.jensklingenberg.ktorfit:ktorfit-lib-light” this will only add the Ktor client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as “ktorfit-lib”

Ktor

Ktorfit is based on Ktor clients 2.3.11. You don’t need to add an extra dependency for the default clients. When you want to use Ktor plugins for things like serialization, you need to add the dependencies, and they need to be compatible with 2.3.11


Last update: May 26, 2024
\ No newline at end of file +

You can also use “de.jensklingenberg.ktorfit:ktorfit-lib-light” this will only add the Ktor client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as “ktorfit-lib”

Ktor

Ktorfit is based on Ktor clients 2.3.12. You don’t need to add an extra dependency for the default clients. When you want to use Ktor plugins for things like serialization, you need to add the dependencies, and they need to be compatible with 2.3.12


Last update: May 26, 2024
\ No newline at end of file diff --git a/knownissues/index.html b/knownissues/index.html new file mode 100644 index 000000000..938bdfe79 --- /dev/null +++ b/knownissues/index.html @@ -0,0 +1 @@ + Known issues - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Known Issues

KMP project with single target

  • Unresolved reference for API class

When you have a KMP project with a single target, IntelliJ will find the generated “create” extension function (e.g. ktorfit.createExampleApi()) in your common module, but the compilation will fail because of an “Unresolved reference” error. In that case, you have to use ktorfit.create<ExampleApi>() to make it work, even though it’s already deprecated.

Kotlin handles the compilation of a KMP project with a single target differently than with multiple targets.

See:


Last update: September 1, 2024
\ No newline at end of file diff --git a/migration/index.html b/migration/index.html index 53bcd0f36..d662bed8d 100644 --- a/migration/index.html +++ b/migration/index.html @@ -1,4 +1,4 @@ - Migration - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Migration

Here is how to migrate from deprecated code:

From <2 to 2.0.0

  • You used Response?

Add this dependency:

You can find all available versions here

implementation("de.jensklingenberg.ktorfit:ktorfit-converters-response:$CONVERTER_VERSION")
+ Migration - Ktorfit       
🤔 Documentation issue? Report or edit

Migration

Here is how to migrate from deprecated code:

From <2 to 2.0.0

  • You used Response?

Add this dependency:

You can find all available versions here

implementation("de.jensklingenberg.ktorfit:ktorfit-converters-response:$CONVERTER_VERSION")
 

and add this converter to your Ktorfit instance:

.addConverterFactory(ResponseConverterFactory())
 

  • You used Call?

Add this dependency:

You can find all available versions here

implementation("de.jensklingenberg.ktorfit:ktorfit-converters-call:$CONVERTER_VERSION")
 

and add this converter to your Ktorfit instance:

.addConverterFactory(CallConverterFactory())
@@ -19,4 +19,4 @@
 

Redirect the deprecated function to the new function:

override suspend fun convert(response: HttpResponse): Any {
     return convert(KtorfitResult.Success(response))
 }
-

Last update: May 26, 2024
\ No newline at end of file +

Last update: May 26, 2024
\ No newline at end of file diff --git a/quick-start/index.html b/quick-start/index.html index e93f52db9..28ff0b281 100644 --- a/quick-start/index.html +++ b/quick-start/index.html @@ -1,4 +1,4 @@ - Quick start - Ktorfit
🤔 Documentation issue? Report or edit

Quick start

First do the Installation

Let’s say you want to make a GET Request to https://swapi.dev/api/people/1/

Create a new Kotlin interface:

interface ExampleApi {
+ Quick start - Ktorfit      
🤔 Documentation issue? Report or edit

Quick start

First do the Installation

Let’s say you want to make a GET Request to https://swapi.dev/api/people/1/

Create a new Kotlin interface:

interface ExampleApi {
     @GET("people/1/")
     suspend fun getPerson(): String
 }
@@ -6,4 +6,4 @@
 val exampleApi = ktorfit.createExampleApi()
 

Next we use the Ktorfit builder to create a Ktorfit instance, and set the base url. After compiling the project we can then use the generated extension function to receive an implementation of the wanted type.

val response = exampleApi.getPerson()
 println(response)
-

Now we can use exampleApi to make the request.


Last update: May 26, 2024
\ No newline at end of file +

Now we can use exampleApi to make the request.


Last update: May 26, 2024
\ No newline at end of file diff --git a/requests/index.html b/requests/index.html index 196360f50..6195ae366 100644 --- a/requests/index.html +++ b/requests/index.html @@ -1,4 +1,4 @@ - Requests - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Requests

HTTP Request

Ktorfit supports the following the HTTP method annotations:

  • @GET
  • @POST
  • @PUT
  • @DELETE
  • @HEAD
  • @OPTIONS
  • @PATCH

Or you can set your custom method to @HTTP

@GET("posts")
+ Requests - Ktorfit       
🤔 Documentation issue? Report or edit

Requests

HTTP Request

Ktorfit supports the following the HTTP method annotations:

  • @GET
  • @POST
  • @PUT
  • @DELETE
  • @HEAD
  • @OPTIONS
  • @PATCH

Or you can set your custom method to @HTTP

@GET("posts")
 fun getPosts(): List<Post>
 

The value of the HTTP annotation will be appended to the baseUrl that you set in the Ktorfit builder. If the value contains a url that starts with http or https, this url will be used for the request instead of the baseUrl.

@GET("https://example.com/posts")
 fun getPosts(): List<Post>
@@ -80,4 +80,4 @@
         println(bytesSentTotal)
     }
 }
-

Then you can use the extension function to set additional configuration. The RequestBuilder will be applied last after everything that is set by Ktorfit


Last update: October 21, 2023
\ No newline at end of file +

Then you can use the extension function to set additional configuration. The RequestBuilder will be applied last after everything that is set by Ktorfit


Last update: October 21, 2023
\ No newline at end of file diff --git a/responseconverter/index.html b/responseconverter/index.html index 0d4712923..bc1b1fbf3 100644 --- a/responseconverter/index.html +++ b/responseconverter/index.html @@ -1,4 +1,4 @@ - Responseconverter - Ktorfit
🤔 Documentation issue? Report or edit

Responseconverter

ResponseConverter is deprecated, use Converter.ResponseConverter instead

Let`s say you have a function that requests a list of comments

@GET("posts/{postId}/comments")
+ Responseconverter - Ktorfit      
🤔 Documentation issue? Report or edit

Responseconverter

ResponseConverter is deprecated, use Converter.ResponseConverter instead

Let`s say you have a function that requests a list of comments

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

But now you want to directly wrap your comment list in your data holder class e.g. “MyOwnResponse”

sealed class MyOwnResponse<T> {
     data class Success<T>(val data: T) : MyOwnResponse<T>()
@@ -31,4 +31,4 @@
 

You can then add the ResponseConverter on your Ktorfit object.

ktorfit.responseConverter(MyOwnResponseConverter())
 

Now add MyOwnResponse to your function

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


Last update: May 27, 2023
\ No newline at end of file +


Last update: May 27, 2023
\ No newline at end of file diff --git a/search/search_index.json b/search/search_index.json index 7e0de7900..64a153e9d 100644 --- a/search/search_index.json +++ b/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Overview","text":"Ktorfit"},{"location":"#introduction","title":"Introduction","text":"

Ktorfit is a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit

"},{"location":"#compatibility","title":"Compatibility","text":"Ktorfit-version Kotlin KSP Ktor 2.0.1 >=2.0.0 >=1.0.24 min 2.3.12 2.0.0 2.0.0 1.0.21 (min) 1.0.24 (max) 2.3.11"},{"location":"#installation","title":"Installation","text":"

Please see Installation

"},{"location":"#quick-start","title":"Quick start","text":"

Please see Quick start

"},{"location":"#requests","title":"Requests","text":"

See Requests

"},{"location":"#converters","title":"Converters","text":"

See documentation Here

"},{"location":"#changelog","title":"Changelog","text":"

See changelog

"},{"location":"#acknowledgments","title":"Acknowledgments","text":"

Some parts of this project are reusing ideas that are originally coming from Retrofit from Square. Thank you for Retrofit!

Thanks to JetBrains for Ktor and Kotlin!

"},{"location":"#contributions","title":"Contributions","text":"

When you find unexpected behaviour please write an issue

"},{"location":"CHANGELOG/","title":"Changelog","text":"

All important changes of this project must be documented in this file.

The format is based on Keep a Changelog and this project orients towards Semantic Versioning. Note: This project needs KSP to work and every new Ktorfit with an update of the KSP version is technically a breaking change. But there is no intent to bump the Ktorfit major version for every KSP update.

"},{"location":"CHANGELOG/#201","title":"2.0.1","text":""},{"location":"CHANGELOG/#201-2024-08-08","title":"2.0.1 - 2024-08-08","text":""},{"location":"CHANGELOG/#fixed","title":"Fixed","text":""},{"location":"CHANGELOG/#ktorfit-annotations","title":"ktorfit-annotations","text":""},{"location":"CHANGELOG/#compilerplugin","title":"compilerPlugin","text":""},{"location":"CHANGELOG/#ktorfit-ksp","title":"ktorfit-ksp","text":""},{"location":"CHANGELOG/#ktor3","title":"Ktor3","text":"

The \u201cnormal\u201d dependencies will stay on Ktor 2.x till 3.0 is stable. But here are versions that you can use when want to use Ktor3 and WasmJs

Project Version ktorfit-lib-light-ktor-3.0.0-beta-2 ktorfit-lib-ktor-3.0.0-beta-2 ktorfit-converters-flow-ktor-3.0.0-beta-2 ktorfit-converters-call-ktor-3.0.0-beta-2 ktorfit-converters-response-ktor-3.0.0-beta-2"},{"location":"CHANGELOG/#200","title":"2.0.0","text":""},{"location":"CHANGELOG/#ktorfit-ksp-200-1024-2024-08-06","title":"ktorfit-ksp-2.0.0-1.0.24 - 2024-08-06","text":""},{"location":"CHANGELOG/#ktorfit-ksp-200-1023-2024-07-14","title":"ktorfit-ksp-2.0.0-1.0.23 - 2024-07-14","text":""},{"location":"CHANGELOG/#-build-with-ksp-1023","title":"- Build with KSP 1.0.23","text":""},{"location":"CHANGELOG/#ktorfit-ksp-200-1022-2024-06-06","title":"ktorfit-ksp-2.0.0-1.0.22 - 2024-06-06","text":""},{"location":"CHANGELOG/#200-2024-05-27","title":"2.0.0 - 2024-05-27","text":""},{"location":"CHANGELOG/#changed","title":"Changed","text":"
plugins {\nid(\"com.google.devtools.ksp\") version \"CURRENT_KSP_VERSION\"\nid(\"de.jensklingenberg.ktorfit\") version \"2.0.0\"\n}\n

See the installation guide for more information: https://foso.github.io/Ktorfit/installation/

val api = ktorfit.create<ExampleApi>()\n

replace with:

val api = ktorfit.createExampleApi()\n
"},{"location":"CHANGELOG/#breaking-changes","title":"Breaking Changes","text":"

The deprecated code got removed. This will simplify the codebase and make it easier to maintain. When you haven\u2019t used the deprecated converters, there is not much you need to change. The dependencies for the converters that were previously auto added now need to be added manually. See the migration guide for more information: https://foso.github.io/Ktorfit/migration/#from-2-to-200

In the previous versions of Ktorfit, the qualifiedTypename was always generated in the code. This was used in the TypeData.createTypeData() function to provide a fully qualified type name for the data type being used.

val _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\nqualifiedTypename = \"de.jensklingenberg.ktorfit.Call<com.example.model.People>\"\n)\n

In the new version of Ktorfit, this behavior has been changed. Now, by default, Ktorfit will keep qualifiedTypename for TypeData in the generated code empty. This means that the qualifiedTypename will not be automatically generated.

val _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\n)\n

However, if you want to keep the old behavior and generate qualifiedTypename, you can set it in the Ktorfit config generateQualifiedTypeName to true in your build.gradle.kts file.

build.gradle.kts
ktorfit {\ngenerateQualifiedTypeName = true\n}\n

This change was made to provide more flexibility and control to the developers over the generated code. Please update your code accordingly if you were relying on the automatic generation of qualifiedTypename.

"},{"location":"CHANGELOG/#fixed_1","title":"Fixed","text":""},{"location":"CHANGELOG/#200-rc01-2024-05-19","title":"2.0.0-rc01 - 2024-05-19","text":""},{"location":"CHANGELOG/#200-beta1-2024-04-28","title":"2.0.0-beta1 - 2024-04-28","text":""},{"location":"CHANGELOG/#breaking-changes_1","title":"Breaking Changes","text":"

The deprecated code got removed. This will simplify the codebase and make it easier to maintain. When you haven\u2019t used the deprecated converters, there is not much you need to change. Some converters that were previously auto applied now need to be added manually. See the migration guide for more information: https://foso.github.io/Ktorfit/migration/#from-2-to-200

"},{"location":"CHANGELOG/#qualifiedtypename-in-ktorfit","title":"QualifiedTypeName in Ktorfit","text":"

In the previous versions of Ktorfit, the qualifiedTypename was always generated in the code. This was used in the TypeData.createTypeData() function to provide a fully qualified type name for the data type being used.

val _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\nqualifiedTypename = \"de.jensklingenberg.ktorfit.Call<com.example.model.People>\"\n)\n

In the new version of Ktorfit, this behavior has been changed. Now, by default, Ktorfit will keep qualifiedTypename for TypeData in the generated code empty. This means that the qualifiedTypename will not be automatically generated.

val _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\n)\n

However, if you want to keep the old behavior and generate qualifiedTypename, you can set a KSP argument Ktorfit_QualifiedTypeName to true in your build.gradle.kts file.

ksp {\narg(\"Ktorfit_QualifiedTypeName\", \"true\")\n}\n

This change was made to provide more flexibility and control to the developers over the generated code. Please update your code accordingly if you were relying on the automatic generation of qualifiedTypename.

"},{"location":"CHANGELOG/#1140-2024-04-15","title":"1.14.0 - 2024-04-15","text":""},{"location":"CHANGELOG/#1130-2024-04-14","title":"1.13.0 - 2024-04-14","text":""},{"location":"CHANGELOG/#1120-2024-01-16","title":"1.12.0 - 2024-01-16","text":""},{"location":"CHANGELOG/#1111-2023-12-21","title":"1.11.1 - 2023-12-21","text":""},{"location":"CHANGELOG/#1110-2023-12-06","title":"1.11.0 - 2023-12-06","text":"

Compatible with KSP 1.0.15 and Kotlin 1.9.21

"},{"location":"CHANGELOG/#changed_1","title":"Changed","text":""},{"location":"CHANGELOG/#1102-2023-11-29","title":"1.10.2 - 2023-11-29","text":""},{"location":"CHANGELOG/#fixed_2","title":"Fixed","text":""},{"location":"CHANGELOG/#1101-2023-11-15","title":"1.10.1 - 2023-11-15","text":""},{"location":"CHANGELOG/#fixed_3","title":"Fixed","text":""},{"location":"CHANGELOG/#1100-2023-11-06","title":"1.10.0 - 2023-11-06","text":"

Compatible with KSP 1.0.14 and Kotlin 1.9.20

"},{"location":"CHANGELOG/#added","title":"Added","text":""},{"location":"CHANGELOG/#changed_2","title":"Changed","text":""},{"location":"CHANGELOG/#191-2023-10-22","title":"1.9.1 - 2023-10-22","text":"

Compatible with KSP 1.0.13 and Kotlin 1.9.10/1.9.20-RC

When you are still using the Ktorfit Gradle plugin in version 1.0.0, please also update this to 1.9.1

"},{"location":"CHANGELOG/#added_1","title":"Added","text":""},{"location":"CHANGELOG/#changed_3","title":"Changed","text":""},{"location":"CHANGELOG/#181-2023-10-09","title":"1.8.1 - 2023-10-09","text":"

Compatible with KSP 1.0.13 and Kotlin 1.9.10/1.9.20-Beta2

"},{"location":"CHANGELOG/#added_2","title":"Added","text":""},{"location":"CHANGELOG/#changed_4","title":"Changed","text":""},{"location":"CHANGELOG/#deprecated","title":"Deprecated","text":"

! When you still use the deprecated converters because there are use cases which you can\u2019t do with the converter factories, please write a GitHub Issue. The deprecated converters will be removed in one of the upcoming versions

"},{"location":"CHANGELOG/#removed","title":"Removed","text":""},{"location":"CHANGELOG/#fixed_4","title":"Fixed","text":""},{"location":"CHANGELOG/#security","title":"Security","text":""},{"location":"CHANGELOG/#180-2023-10-09","title":"1.8.0 - 2023-10-09","text":"

Skip this version, it had an issue with Kotlin 1.9.20

"},{"location":"CHANGELOG/#170-1920-beta2-2023-09-16","title":"1.7.0-1.9.20-Beta2 - 2023-09-16","text":"

Version of 1.7.0 that is compatible with Kotlin 1.9.20-Beta2

"},{"location":"CHANGELOG/#170-2023-09-16","title":"1.7.0 - 2023-09-16","text":""},{"location":"CHANGELOG/#gradle-plugin","title":"Gradle plugin","text":"

From now on with every Ktorfit release there will also be a Gradle plugin with the same version. That means that drop you can drop the Gradle extension block where you previously set the version number and just bump the number of the Gradle plugin. https://foso.github.io/Ktorfit/installation/

plugins {\nid(\"de.jensklingenberg.ktorfit\") version \"1.7.0\"\n}\n
"},{"location":"CHANGELOG/#added_3","title":"Added","text":""},{"location":"CHANGELOG/#changed_5","title":"Changed","text":""},{"location":"CHANGELOG/#160-2023-08-24","title":"1.6.0 - 2023-08-24","text":""},{"location":"CHANGELOG/#changed_6","title":"Changed","text":""},{"location":"CHANGELOG/#150-2023-08-04","title":"1.5.0 - 2023-08-04","text":""},{"location":"CHANGELOG/#changed_7","title":"Changed","text":""},{"location":"CHANGELOG/#144-2023-07-26","title":"1.4.4 - 2023-07-26","text":""},{"location":"CHANGELOG/#changed_8","title":"Changed","text":""},{"location":"CHANGELOG/#143-2023-07-13","title":"1.4.3 - 2023-07-13","text":""},{"location":"CHANGELOG/#fixed_5","title":"Fixed","text":""},{"location":"CHANGELOG/#372-crash-with-xiaomi-on-create-ktorfitbuilder-by-princeparadoxes","title":"372 Crash with Xiaomi on create Ktorfit.Builder by @princeparadoxes","text":""},{"location":"CHANGELOG/#142-2023-06-25","title":"1.4.2 - 2023-06-25","text":""},{"location":"CHANGELOG/#fixed_6","title":"Fixed","text":""},{"location":"CHANGELOG/#323-code-generation-issue-for-multipart-formurlencoded-by-ph1ll1pp","title":"323 Code generation issue for @Multipart / @FormUrlEncoded by @Ph1ll1pp","text":""},{"location":"CHANGELOG/#141-2023-06-03","title":"1.4.1 - 2023-06-03","text":""},{"location":"CHANGELOG/#changed_9","title":"Changed","text":""},{"location":"CHANGELOG/#fixed_7","title":"Fixed","text":""},{"location":"CHANGELOG/#236-parsing-error-for-listarray","title":"236 Parsing error for list/array","text":""},{"location":"CHANGELOG/#140-2023-05-27","title":"1.4.0 - 2023-05-27","text":""},{"location":"CHANGELOG/#added_4","title":"Added","text":"

e.g.

interface ExampleApi{\nsuspend fun getUser(): Response<User>\n}\n\nval user = userKtorfit.create<ExampleApi>().getUser()\n\nif(user.isSuccessful){\nuser.body()\n}else{\nuser.errorBody()\n}\n
"},{"location":"CHANGELOG/#85-added-a-response-class-that-can-be-used-as-a-wrapper-around-the-api-response-the-converter-for-it-is-automatically-applied-thx-to-vovahost-datl4g","title":"85 Added a Response class that can be used as a wrapper around the API Response, the converter for it is automatically applied. thx to @vovahost, @DATL4G","text":""},{"location":"CHANGELOG/#changed_10","title":"Changed","text":""},{"location":"CHANGELOG/#deprecated_1","title":"Deprecated","text":""},{"location":"CHANGELOG/#130-2023-05-14","title":"1.3.0 - 2023-05-14","text":""},{"location":"CHANGELOG/#changed_11","title":"Changed","text":""},{"location":"CHANGELOG/#fixed_8","title":"Fixed","text":"

[Bug]: IllegalArgumentException with Custom Http Annotation #274

"},{"location":"CHANGELOG/#bumped","title":"Bumped","text":"

KSP version to 1.0.11

"},{"location":"CHANGELOG/#120-2023-05-05","title":"1.2.0 - 2023-05-05","text":""},{"location":"CHANGELOG/#bumped_1","title":"Bumped","text":"

Now based on Ktor 2.3.0

"},{"location":"CHANGELOG/#110-2023-04-15","title":"1.1.0 - 2023-04-15","text":""},{"location":"CHANGELOG/#added_5","title":"Added","text":"

From now on there are two versions of the ktorfit-lib.

\u201cde.jensklingenberg.ktorfit:ktorfit-lib\u201d will stay like before and add the platform client dependencies for the clients.

\u201cde.jensklingenberg.ktorfit:ktorfit-lib-light\u201d this will only add the client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as \u201cktorfit-lib\u201d

"},{"location":"CHANGELOG/#changed_12","title":"Changed","text":""},{"location":"CHANGELOG/#bumped_2","title":"Bumped","text":""},{"location":"CHANGELOG/#101-2023-03-20","title":"1.0.1 - 2023-03-20","text":""},{"location":"CHANGELOG/#added_6","title":"Added","text":"

From now on there are two versions of the ktorfit-lib.

\u201cde.jensklingenberg.ktorfit:ktorfit-lib\u201d will stay like before and add the platform client dependencies for the clients.

\u201cde.jensklingenberg.ktorfit:ktorfit-lib-light\u201d this will only add the client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as \u201cktorfit-lib\u201d

"},{"location":"CHANGELOG/#fixed_9","title":"Fixed","text":"

[Bug]: Post request body serialization doesn\u2019t work #202

"},{"location":"CHANGELOG/#100-2023-03-02","title":"1.0.0 - 2023-03-02","text":"

This project is now following semver

"},{"location":"CHANGELOG/#added_7","title":"Added","text":""},{"location":"CHANGELOG/#fixed_10","title":"Fixed","text":""},{"location":"CHANGELOG/#bumped_3","title":"Bumped","text":""},{"location":"CHANGELOG/#100-beta18-12-02-2023","title":"1.0.0-beta18 (12-02-2023)","text":"

NEW:

Ktorfit.Builder().baseUrl(testBaseUrl, checkUrl = false).build()\n

\ud83d\udc1b Bugs fixed

\u2b06\ufe0f Deps updates

"},{"location":"CHANGELOG/#100-beta17-21-01-2023","title":"1.0.0-beta17 (21-01-2023)","text":"

\ud83d\udca5 Breaking changes:

Add this plugin:

plugins {\nid \"de.jensklingenberg.ktorfit\" version \"1.0.0\"\n}\n

NEW:

\ud83d\udc1b Bugs fixed

\u2b06\ufe0f Deps updates

"},{"location":"CHANGELOG/#100-beta16-13-11-2022","title":"1.0.0-beta16 (13-11-2022)","text":"

NEW: - Field parameters can now be nullable, null values will be ignored in requests - Add option to turn of error checking

ksp { arg(\u201cKtorfit_Errors\u201d, \u201c1\u201d) }

You can set it in your build.gradle.kts file,

0: Turn off all Ktorfit related error checking

1: Check for errors

2: Turn errors into warnings

\u2b06\ufe0f Deps updates

\ud83d\udc1b Bugs fixed

"},{"location":"CHANGELOG/#whats-changed","title":"What\u2019s Changed","text":"

Full Changelog: https://github.com/Foso/Ktorfit/compare/v1.0.0-beta15...v1.0.0-beta16

"},{"location":"CHANGELOG/#100-beta15-05-10-2022","title":"1.0.0-beta15 (05-10-2022)","text":"

\u2b06\ufe0f Deps updates

\ud83d\udc1b Bugs fixed

\ud83d\udca5 Breaking changes

"},{"location":"CHANGELOG/#100-beta14-24-09-2022","title":"1.0.0-beta14 (24-09-2022)","text":"

NEW: - Query parameters can now be nullable, null values will be ignored in requests - Function return types can now be nullable

FIX: - Url annotation not resolved correctly #65

BREAKING CHANGES: - Changed naming of Converters:

"},{"location":"CHANGELOG/#100-beta13-10-09-2022","title":"1.0.0-beta13 (10-09-2022)","text":"

Fixed: - Url annotation not resolved correctly #52

"},{"location":"CHANGELOG/#100-beta12-31-08-2022","title":"1.0.0-beta12 (31-08-2022)","text":""},{"location":"CHANGELOG/#breaking-changes_2","title":"Breaking Changes:","text":"

wrapResponse from SuspendResponseConverter got renamed to wrapSuspendResponse. This add the possibility to have ResponseConverter and SuspendResponseConverter implemented in the same class.

"},{"location":"CHANGELOG/#changes","title":"Changes:","text":"

val ktorfit = ktorfit {\nbaseUrl(\"http://example.com/\")\n}.create<GithubService>()\n
or this

val ktorfit = ktorfit {\nbaseUrl(\"http://example.com/\")\n}.createGithubService()\n

By default, IntelliJ/Android Studio can\u2019t find the generated code, you need to add the KSP generated folder to the sourcesets like this: (See more here: https://kotlinlang.org/docs/ksp-quickstart.html#make-ide-aware-of-generated-code)

kotlin.srcDir(\"build/generated/ksp/jvm/jvmMain/\")\n
"},{"location":"CHANGELOG/#100-beta11-21-08-2022","title":"1.0.0-beta11 (21-08-2022)","text":""},{"location":"CHANGELOG/#100-beta10-18-08-2022","title":"1.0.0-beta10 (18-08-2022)","text":"

to this:

Ktorfit.Builder()\n.baseUrl(\"https://example.com/\")\n.httpClient(HttpClient {})\n.build()\n
"},{"location":"CHANGELOG/#breaking-changes_3","title":"Breaking Changes:","text":"

@Headers now requires a vararg of String instead of an Array e.g. you need to change from:

@Headers(\n[\"Authorization: token ghp_abcdefgh\",\n\"Content-Type: application/json\"]\n)\n

to this:

@Headers(\n\"Authorization: token ghp_abcdefgh\",\n\"Content-Type: application/json\"\n)\n

"},{"location":"CHANGELOG/#100-beta09","title":"1.0.0-beta09","text":""},{"location":"CHANGELOG/#15-fix-encoding-of-query-parameters","title":"15 fix encoding of query parameters","text":""},{"location":"CHANGELOG/#100-beta08","title":"1.0.0-beta08","text":""},{"location":"CHANGELOG/#100-beta07","title":"1.0.0-beta07","text":""},{"location":"CHANGELOG/#100-beta06","title":"1.0.0-beta06","text":""},{"location":"CHANGELOG/#100-beta05","title":"1.0.0-beta05","text":""},{"location":"CHANGELOG/#100-beta04","title":"1.0.0-beta04","text":"

initial release

"},{"location":"License/","title":"License","text":"
                             Apache License\n                       Version 2.0, January 2004\n                    http://www.apache.org/licenses/\n

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

  1. Definitions.

    \u201cLicense\u201d shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.

    \u201cLicensor\u201d shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.

    \u201cLegal Entity\u201d shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, \u201ccontrol\u201d means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.

    \u201cYou\u201d (or \u201cYour\u201d) shall mean an individual or Legal Entity exercising permissions granted by this License.

    \u201cSource\u201d form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.

    \u201cObject\u201d form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.

    \u201cWork\u201d shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).

    \u201cDerivative Works\u201d shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.

    \u201cContribution\u201d shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, \u201csubmitted\u201d means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as \u201cNot a Contribution.\u201d

    \u201cContributor\u201d shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.

  2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.

  3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.

  4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:

    (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and

    (b) You must cause any modified files to carry prominent notices stating that You changed the files; and

    \u00a9 You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and

    (d) If the Work includes a \u201cNOTICE\u201d text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.

    You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.

  5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.

  6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.

  7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an \u201cAS IS\u201d BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.

  8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.

  9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

  To apply the Apache License to your work, attach the following\n  boilerplate notice, with the fields enclosed by brackets \"[]\"\n  replaced with your own identifying information. (Don't include\n  the brackets!)  The text should be enclosed in the appropriate\n  comment syntax for the file format. We also recommend that a\n  file or class name and description of purpose be included on the\n  same \"printed page\" as the copyright notice for easier\n  identification within third-party archives.\n

Copyright 2022 Jens Klingenberg

Licensed under the Apache License, Version 2.0 (the \u201cLicense\u201d); 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\n

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \u201cAS IS\u201d 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.

"},{"location":"architecture/","title":"How Ktorfit works under the hood","text":"

Ktorfit consists of three main components KSP-Plugin, Compiler plugin and the Ktorfit lib

"},{"location":"architecture/#ksp-plugin","title":"KSP-Plugin","text":"

This will generate the code for the implementation of the interfaces

"},{"location":"architecture/#compiler-plugin","title":"Compiler plugin","text":"

This transforms the create() function from the Ktorfit lib

"},{"location":"architecture/#ktorfit-lib","title":"Ktorfit lib","text":"

A wrapper around Ktor to simplify code generation

"},{"location":"architecture/#example","title":"Example","text":"

package com.example\n\nimport com.example.model.People\nimport de.jensklingenberg.ktorfit.http.GET\n\ninterface ExampleApi  {\n@GET(\"/test\")\nsuspend fun exampleGet(): People\n}\n
Let`s say we have a interface like this.

At compile time Ktorfit/KSP checks for all functions that are annotated with Ktorfit annotations like @GET.

Then it looks at the parent interfaces of that functions and generates, the source code of a Kotlin class that implements the interface. The classes are named like the interfaces but with an underscore at the beginning and \u201cImpl\u201d at the end, and they have the same package as the interfaces. In this case a class named _ExampleApiImpl will be generated.

@OptIn(InternalKtorfitApi::class)\npublic class _ExampleApiImpl(\nprivate val _ktorfit: Ktorfit,\n) : ExampleApi {\npublic val _converter: KtorfitConverterHelper = KtorfitConverterHelper(_ktorfit)\n\noverride suspend fun exampleGet(): People {\nval _ext: HttpRequestBuilder.() -> Unit = {\nmethod = HttpMethod.parse(\"GET\")\nurl{\ntakeFrom(_ktorfit.baseUrl + \"/test\")\n}\n}\nval _typeData = TypeData.createTypeData(qualifiedTypename = \"com.example.model.People\",\ntypeInfo = typeInfo<People>())\n\nreturn _converter.suspendRequest<People, People>(_typeData,_ext)!!\n}\n}\n\npublic fun Ktorfit.createExampleApi(): ExampleApi = this.create(_ExampleApiImpl(this))\n

The next part is the compiler plugin which is added by the gradle plugin. It looks for the every usage of the create function from the Ktorfit-lib and adds an object of the wanted implementation class as an argument. Because of the naming convention of the generated classes we can deduce the name of the class from the name of type parameter.

val api = jvmKtorfit.create<ExampleApi>()\n

will be transformed to:

val api = jvmKtorfit.create<ExampleApi>(_ExampleApiImpl(jvmKtorfit))\n

The create() function is used, checks that the compiler plugin replaced the default value

public fun <T> create(data: T? = null): T {\nif (data == null) {\nthrow IllegalArgumentException(ENABLE_GRADLE_PLUGIN)\n}\nreturn data\n}\n
"},{"location":"configuration/","title":"Compile errors","text":"

By default, Ktorfit will throw compile error when it finds conditions under which it can\u2019t ensure that it will work correct. You can set it in the Ktorfit config to change this

ktorfit{\nerrorCheckingMode = ErrorCheckingMode.NONE\n}\n

You can set it in your build.gradle.kts file,

"},{"location":"configuration/#qualifiedtypename","title":"QualifiedTypeName","text":"

By default, Ktorfit will keep qualifiedTypename for TypeData in the generated code empty. You can set it in the Ktorfit config to change this:

ktorfit {\ngenerateQualifiedTypeName = true\n}\n
Default code generation
...\nval _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\n)\n...\n
With QualifiedTypeName true
...\nval _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\nqualifiedTypename = \"de.jensklingenberg.ktorfit.Call<com.example.model.People>\"\n)\n...\n
"},{"location":"configuration/#add-your-own-ktor-client","title":"Add your own Ktor client","text":"

You can set your Ktor client instance to the Ktorfit builder:

val myClient = HttpClient()\nval ktorfit = Ktorfit.Builder().httpClient(myClient).build()\n
"},{"location":"development/","title":"Development","text":""},{"location":"development/#update-ktorfit-for-new-kotlin-version","title":"Update Ktorfit for new Kotlin version","text":""},{"location":"installation/","title":"Installation","text":""},{"location":"installation/#setup","title":"Setup","text":"

(You can also look how it\u2019s done in the examples)

"},{"location":"installation/#gradle-plugins","title":"Gradle Plugins","text":"

You need to add KSP and the Ktorfit Gradle plugin

plugins {\nid(\"com.google.devtools.ksp\") version \"CURRENT_KSP_VERSION\"\nid(\"de.jensklingenberg.ktorfit\") version \"2.0.1\"\n}\n

"},{"location":"installation/#ktorfit-lib","title":"Ktorfit-lib","text":"

Add the Ktorfit-lib to your common module. You can find all available versions here

val ktorfitVersion = \"2.0.1\"\n\nsourceSets {\nval commonMain by getting{\ndependencies{\nimplementation(\"de.jensklingenberg.ktorfit:ktorfit-lib:$ktorfitVersion\")\n}\n}\n

You can also use \u201cde.jensklingenberg.ktorfit:ktorfit-lib-light\u201d this will only add the Ktor client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as \u201cktorfit-lib\u201d

"},{"location":"installation/#ktor","title":"Ktor","text":"

Ktorfit is based on Ktor clients 2.3.11. You don\u2019t need to add an extra dependency for the default clients. When you want to use Ktor plugins for things like serialization, you need to add the dependencies, and they need to be compatible with 2.3.11

"},{"location":"migration/","title":"Migration","text":"

Here is how to migrate from deprecated code:

"},{"location":"migration/#from-2-to-200","title":"From <2 to 2.0.0","text":"

Add this dependency:

You can find all available versions here

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-response:$CONVERTER_VERSION\")\n

and add this converter to your Ktorfit instance:

.addConverterFactory(ResponseConverterFactory())\n

Add this dependency:

You can find all available versions here

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-call:$CONVERTER_VERSION\")\n

and add this converter to your Ktorfit instance:

.addConverterFactory(CallConverterFactory())\n

Add this dependency:

You can find all available versions here

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-flow:$CONVERTER_VERSION\")\n

and add this converter to your Ktorfit instance:

.addConverterFactory(FlowConverterFactory())\n

"},{"location":"migration/#from-170-to-181","title":"From 1.7.0 to 1.8.1","text":""},{"location":"migration/#suspendresponseconverter","title":"SuspendResponseConverter","text":"

Implement override suspend fun convert(result: KtorfitResult)

public suspend fun convert(result: KtorfitResult): T {\nreturn when (result) {\nis KtorfitResult.Failure -> {\nthrow result.throwable // Or do something with the throwable\n}\n\nis KtorfitResult.Success -> {\nval response = result.response\n//Put the code that was in your other convert function here\n}\n}\n}\n

Redirect the deprecated function to the new function:

override suspend fun convert(response: HttpResponse): Any {\nreturn convert(KtorfitResult.Success(response))\n}\n
"},{"location":"quick-start/","title":"Quick start","text":"

First do the Installation

Let\u2019s say you want to make a GET Request to https://swapi.dev/api/people/1/

Create a new Kotlin interface:

interface ExampleApi {\n@GET(\"people/1/\")\nsuspend fun getPerson(): String\n}\n

Now we add a function that will be used to make our request. The @GET annotation will tell Ktorfit that this a GET request. The value of @GET is the relative URL path that will be appended to the base url which we set later.

An interface used for Ktorfit needs to have a HTTP method annotation on every function. Because Ktor relies on Coroutines by default your functions need to have the suspend modifier. Alternatively you can use #Flow or Call

Info

The return type String will return the response text. When you want directly parse the response into a class you need to add a JSON,XML, etc. converter to Ktor

val ktorfit = Ktorfit.Builder().baseUrl(\"https://swapi.dev/api/\").build()\nval exampleApi = ktorfit.createExampleApi()\n

Next we use the Ktorfit builder to create a Ktorfit instance, and set the base url. After compiling the project we can then use the generated extension function to receive an implementation of the wanted type.

val response = exampleApi.getPerson()\nprintln(response)\n

Now we can use exampleApi to make the request.

"},{"location":"requests/","title":"Requests","text":""},{"location":"requests/#http-request","title":"HTTP Request","text":"

Ktorfit supports the following the HTTP method annotations:

Or you can set your custom method to @HTTP

@GET(\"posts\")\nfun getPosts(): List<Post>\n

The value of the HTTP annotation will be appended to the baseUrl that you set in the Ktorfit builder. If the value contains a url that starts with http or https, this url will be used for the request instead of the baseUrl.

@GET(\"https://example.com/posts\")\nfun getPosts(): List<Post>\n

The value can only be empty when you also use @Url

"},{"location":"requests/#url","title":"Url","text":"

Can be used to set a URL dynamically as a function parameter.

@GET(\"\")\nsuspend fun getPosts(@Url url: String): List<Post>\n

"},{"location":"requests/#query","title":"Query","text":"
@GET(\"comments\")\nsuspend fun getCommentsById(\n@Query(\"postId\") postId: String,\n@QueryName queryName: String,\n@QueryMap headerMap : Map<String,String>\n): List<Comment>\n

You can use @Query, @QueryName or @QueryMap to set queries to your request.

Example:

@GET(\"comments\")\nsuspend fun getCommentsById(@Query(\"postId\") postId: String): List<Comment>\n

A request with getCommentsById(3) will result in the relative URL \u201ccomments?postId=3\u201d

"},{"location":"requests/#path","title":"Path","text":"

When you want to dynamically replace a part of the URL, you can use the @Path annotation.

interface ExampleApi {\n@GET(\"people/{peopleId}/\")\nsuspend fun getPerson(@Path(\"peopleId\") id: String): String\n}\n
Just write a part of your URL path in curly braces. Then you need to annotate a parameter with @Path. The value of @Path needs to match with one of the curly braces part in your URL path.

Example:

On a request with getPerson(1) , {peopleId} will be replaced with the argument 1 and the relative URL will become \u201cpeople/1/\u201d

"},{"location":"requests/#headers","title":"Headers","text":"
@Headers(\"Accept: application/json\")\n@GET(\"comments\")\nsuspend fun requestWithHeaders(\n@Header(\"Content-Type\") name: String,\n@HeaderMap headerMap : Map<String,String>\n): List<Comment>\n

You can use @Headers, @Header or @HeaderMap to configure headers to your request.

"},{"location":"requests/#body","title":"Body","text":"
interface ExampleService {\n@POST(\"upload\")\nsuspend fun upload(@Body data: String)\n}\n

@Body can be used as parameter to send data in a request body. It can only be used with Http Methods that have a request body

"},{"location":"requests/#formdata","title":"FormData","text":"
@POST(\"signup\")\n@FormUrlEncoded\nsuspend fun signup(\n@Field(\"username\") username: String, @Field(\"email\") email: String,\n@Field(\"password\") password: String, @Field(\"confirmation\") confirmation: String\n): String\n

To send FormData you can use @Field or @FieldMap. Your function needs to be annotated with @FormUrlEncoded.

"},{"location":"requests/#tag","title":"Tag","text":"

Tag can be used to add a tag to a request.

@GET(\"posts\")\nfun getPosts(@Tag(\"myTag\") tag: String): List<Post>\n

You can then access the tag from the attributes of a Ktor HttpClientCall

val myTag = response.call.attributes[AttributeKey(\"myTag\")] 
"},{"location":"requests/#multipart","title":"Multipart","text":"

To send Multipart data you have two options:

"},{"location":"requests/#1-body","title":"1) @Body","text":"
interface ExampleService {\n@POST(\"upload\")\nsuspend fun upload(@Body map: MultiPartFormDataContent)\n}\n

To upload MultiPartFormData you need to have a parameter of the type MultiPartFormDataContent that is annotated with @Body. The method needs to be annotated with @POST or @PUT

val multipart = MultiPartFormDataContent(formData {\nappend(\"description\", \"Ktor logo\")\nappend(\"image\", File(\"ktor_logo.png\").readBytes(), Headers.build {\nappend(HttpHeaders.ContentType, \"image/png\")\nappend(HttpHeaders.ContentDisposition, \"filename=ktor_logo.png\")\n})\n})\n\nexampleApi.upload(multipart)\n

Then you can use Ktor\u2019s formData Builder to create the MultiPartFormDataContent

"},{"location":"requests/#2-multipart","title":"2) @MultiPart","text":"
@Multipart\n@POST(\"upload\")\nsuspend fun uploadFile(@Part(\"description\") description: String, @Part(\"\") file: List<PartData>): String\n

You can annotate a function with @Multipart. Then you can annotate parameters with @Part

val multipart = formData {\nappend(\"image\", File(\"ktor_logo.png\").readBytes(), Headers.build {\nappend(HttpHeaders.ContentType, \"image/png\")\nappend(HttpHeaders.ContentDisposition, \"filename=ktor_logo.png\")\n})\n}\n\nexampleApi.upload(\"Ktor logo\",multipart)\n

All your parameters annotated with @Part wil be combined and send as MultiPartFormDataContent

"},{"location":"requests/#json","title":"JSON","text":"

Ktorfit doesn\u2019t parse JSON. You have to install the Json Feature to the Ktor Client that you add to Ktorfit.

See here Add your own Ktor client and here https://ktor.io/docs/serialization-client.html

val ktorClient = HttpClient() {\ninstall(ContentNegotiation) {\njson(Json { isLenient = true; ignoreUnknownKeys = true })\n}\n}\n
"},{"location":"requests/#streaming","title":"Streaming","text":"
@Streaming\n@GET(\"docs/response.html#streaming\")\nsuspend fun getPostsStreaming(): HttpStatement\n

To receive streaming data you need to annotate a function with @Streaming and the return type has to be HttpStatement.

exampleApi.getPostsStreaming().execute { response ->\n//Do something with response\n}\n

For more information check Ktor docs

"},{"location":"requests/#requestbuilder","title":"RequestBuilder","text":"
@GET(\"comments\")\nsuspend fun getCommentsById(\n@Query(\"postId\") name: String,\n@ReqBuilder ext: HttpRequestBuilder.() -> Unit\n): List<Comment>\n

You need to set extra configuration on your request? Add a parameter with \u201c@ReqBuilder ext: HttpRequestBuilder.() -> Unit\u201d to your function.

val result = secondApi.getCommentsById(\"3\") {\nonDownload { bytesSentTotal, contentLength ->\nprintln(bytesSentTotal)\n}\n}\n

Then you can use the extension function to set additional configuration. The RequestBuilder will be applied last after everything that is set by Ktorfit

"},{"location":"responseconverter/","title":"Responseconverter","text":"

ResponseConverter is deprecated, use Converter.ResponseConverter instead

Let`s say you have a function that requests a list of comments

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

But now you want to directly wrap your comment list in your data holder class e.g. \u201cMyOwnResponse\u201d

sealed class MyOwnResponse<T> {\ndata class Success<T>(val data: T) : MyOwnResponse<T>()\nclass Error(val ex:Throwable) : MyOwnResponse<Nothing>()\n\ncompanion object {\nfun <T> success(data: T) = Success(data)\nfun error(ex: Throwable) = Error(ex)\n}\n}\n

To enable that, you have to implement a ResponseConverter. This class will be used to wrap the Ktor response inside your wrapper class.

class MyOwnResponseConverter : ResponseConverter {\n\noverride suspend fun <RequestType> wrapResponse(\ntypeData: TypeData,\nrequestFunction: suspend () -> Pair<TypeInfo, HttpResponse>,\nktorfit: Ktorfit\n): Any {\nreturn try {\nval (info, response) = requestFunction()\nMyOwnResponse.success<Any>(response.body(info))\n} catch (ex: Throwable) {\nMyOwnResponse.error(ex)\n}\n}\n\noverride fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean {\nreturn typeData.qualifiedName == \"com.example.model.MyOwnResponse\"\n}\n}\n

You can then add the ResponseConverter on your Ktorfit object.

ktorfit.responseConverter(MyOwnResponseConverter())\n

Now add MyOwnResponse to your function

@GET(\"posts/{postId}/comments\")\nsuspend fun getCommentsByPostId(@Path(\"postId\") postId: Int): MyOwnResponse<List<Comment>>\n

"},{"location":"suspendresponseconverter/","title":"Suspendresponseconverter","text":"

SuspendResponseConverter is deprecated, use Converter.SuspendResponseConverter instead

Because Ktor relies on Coroutines by default your functions need to have the suspend modifier.

To change this, you need to use a SuspendResponseConverter, you add your own or use Flow or Call

You can add RequestConverter on your Ktorfit object.

ktorfit.responseConverter(FlowResponseConverter())\n
"},{"location":"suspendresponseconverter/#flow","title":"Flow","text":"

Ktorfit has support for Kotlin Flow. You need add the FlowResponseConverter() to your Ktorfit instance.

ktorfit.responseConverter(FlowResponseConverter())\n
@GET(\"comments\")\nfun getCommentsById(@Query(\"postId\") postId: String): Flow<List<Comment>>\n

Then you can drop the suspend modifier and wrap your return type with Flow<>

"},{"location":"suspendresponseconverter/#call","title":"Call","text":"

ktorfit.responseConverter(CallResponseConverter())\n
@GET(\"people/{id}/\")\nfun getPersonById(@Path(\"id\") peopleId: Int): Call<People>\n

exampleApi.getPersonById(3).onExecute(object : Callback<People>{\noverride fun onResponse(call: People, response: HttpResponse) {\n//Do something with Response\n}\n\noverride fun onError(exception: Exception) {\n//Do something with exception\n}\n})\n

You can use Call to receive the response in a Callback."},{"location":"suspendresponseconverter/#your-own","title":"Your own","text":"

You can also add your own Converter. You just need to implement RequestConverter. Inside the converter you need to handle the conversion from suspend to your async code.

class MyOwnResponseConverter : SuspendResponseConverter {\n...\n
"},{"location":"android/proguard/","title":"proguard","text":"

proguard-rules.pro

-keep class de.jensklingenberg.ktorfit.** { *; }\n-keepclassmembers class de.jensklingenberg.ktorfit.** { *; }\n

"},{"location":"converters/converters/","title":"Converters","text":"

Converters are used to convert the HTTPResponse or parameters.

They are added inside of a Converter.Factory which will then be added to the Ktorfit builder with the converterfactories() function.

"},{"location":"converters/converters/#converter-types","title":"Converter Types","text":""},{"location":"converters/converters/#existing-converter-factories","title":"Existing converter factories","text":"

Add this dependency:

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-call:$CONVERTER_VERSION\")\n

You can find all available versions here

Add this dependency:

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-flow:$CONVERTER_VERSION\")\n

You can find all available versions here

Add this dependency:

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-response:$CONVERTER_VERSION\")\n

You can find all available versions here

"},{"location":"converters/example1/","title":"Example1","text":"

Let\u2019s say you want to get an user from an API and the response you get looks like below:

API response
{\n\"success\": true,\n\"user\":\n{\n\"id\": 1,\n\"name\": \"Jens Klingenberg\"\n}\n}\n

But you are only interested in the \u201cuser\u201d object, and you want to look your interface function something like this:

Example function
@GET(\"/user\")\nsuspend fun getUser(): User\n

First you need the Kotlin classes to which your JSON data is mapped to:

This example assumes that you are Kotlin Serialization

@kotlinx.serialization.Serializable\ndata class Envelope(val success: Boolean, val user: User)\n\n@kotlinx.serialization.Serializable\ndata class User(val id: Int, val name: String)\n

Now you need a converter that can convert the HTTPResponse and return a user object. Create a class that extends Converter.Factory

class UserFactory : Converter.Factory {\n\n}\n

Because in this case User is the return type of a suspend function, you need to create a SuspendResponseConverter. Override suspendResponseConverter()

class UserFactory : Converter.Factory {\noverride fun suspendResponseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.SuspendResponseConverter<HttpResponse, *>? {\n\n}\n}\n

Inside suspendResponseConverter you can decide if you want to return a converter. In our case we a converter for the type User. We can check that case with the typeData that we get as a parameter.

override fun suspendResponseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.SuspendResponseConverter<HttpResponse, *>? {\nif (typeData.typeInfo.type == User::class) {\n...\n}\nreturn null\n}\n

Next we create the SuspendResponseConverter:

if (typeData.typeInfo.type == User::class) {\nreturn object : Converter.SuspendResponseConverter<HttpResponse, Any> {\noverride suspend fun convert(response: HttpResponse): Any {\n...\n}\n}\n}\n
Inside of convert we get the HttpResponse and we want to return a User object.

Now we could do the following:

When we know that this converter will always be used for a API that wraps the User inside an Envelope class, we can directly transform the body to an envelope object and just return the user object.

override suspend fun convert(response: HttpResponse): Any {\nval envelope = response.body<Envelope>()\nreturn envelope.user\n}\n

or we can create a TypeData of Envelope and use nextSuspendResponseConverter() to look up the next converter that can convert the response

 override suspend fun convert(response: HttpResponse): Any {\nval typeData = TypeData.createTypeData(\"com.example.model.Envelope\", typeInfo<Envelope>())\nval envelope = ktorfit.nextSuspendResponseConverter(null, typeData)?.convert(response) as? Envelope\nreturn envelope.user\n}\n

Finally, add your converter factory to the Ktorfit Builder

Ktorfit.Builder().converterFactories(UserFactory()).baseUrl(\"foo\").build()\n
"},{"location":"converters/migration/","title":"Migration","text":" SuspendResponseConverter
 override suspend fun <RequestType> wrapSuspendResponse(\ntypeData: TypeData,\nrequestFunction: suspend () -> Pair<TypeInfo, HttpResponse>,\nktorfit: Ktorfit\n): Any {\nreturn object : Call<RequestType> {\noverride fun onExecute(callBack: Callback<RequestType>) {\n\nktorfit.httpClient.launch {\nval deferredResponse = async { requestFunction() }\n\nval (data, response) = deferredResponse.await()\n\ntry {\nval res = response.call.body(data)\ncallBack.onResponse(res as RequestType, response)\n} catch (ex: Exception) {\ncallBack.onError(ex)\n} } } } }\n\noverride fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean {\nreturn typeData.qualifiedName == \"de.jensklingenberg.ktorfit.Call\"\n}\n
Equivalent with converter factory:
public class CallConverterFactory : Converter.Factory {\n\noverride fun suspendResponseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.SuspendResponseConverter<HttpResponse, *>? {\nif (typeData.typeInfo.type == Call::class) {\nreturn object: Converter.SuspendResponseConverter<HttpResponse, Call<Any?>> {\noverride suspend fun convert(response: HttpResponse): Call<Any?> {\n\nreturn object : Call<Any?> {\noverride fun onExecute(callBack: Callback<Any?>) {\nktorfit.httpClient.launch {\ntry {\nval data = response.call.body(typeData.typeArgs.first().typeInfo)\ncallBack.onResponse(data!!, response)\n} catch (ex: Exception) {\ncallBack.onError(ex)\n} } } } } } }\nreturn null\n}\n}\n
ResponseConverter
override fun <RequestType> wrapResponse(\ntypeData: TypeData,\nrequestFunction: suspend () -> Pair<TypeInfo, HttpResponse?>,\nktorfit: Ktorfit\n): Any {\nreturn object : Call<RequestType> {\noverride fun onExecute(callBack: Callback<RequestType>) {\n\nktorfit.httpClient.launch {\nval deferredResponse = async { requestFunction() }\n\ntry {\nval (info, response) = deferredResponse.await()\nval data = response!!.body(info) as RequestType\ncallBack.onResponse(data, response)\n} catch (ex: Exception) {\ncallBack.onError(ex)\n}\n\n}\n}\n\n}\n}\n\noverride fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean {\nreturn typeData.qualifiedName == \"de.jensklingenberg.ktorfit.Call\"\n}\n
Equivalent with converter factory:
public class CallConverterFactory : Converter.Factory {\noverride fun responseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.ResponseConverter<HttpResponse, *>? {\nif (typeData.typeInfo.type == Call::class) {\nreturn object : Converter.ResponseConverter<HttpResponse, Call<Any?>> {\n\noverride fun convert(getResponse: suspend () -> HttpResponse): Call<Any?> {\nreturn object : Call<Any?> {\noverride fun onExecute(callBack: Callback<Any?>) {\nktorfit.httpClient.launch {\ntry {\nval response = getResponse()\n\nval data = response.call.body(typeData.typeArgs.first().typeInfo)\n\ncallBack.onResponse(data, response)\n} catch (ex: Exception) {\nprintln(ex)\ncallBack.onError(ex)\n} } } } } } }\nreturn null\n}\n}\n
"},{"location":"converters/requestparameterconverter/","title":"RequestParameterConverter","text":"
@GET(\"posts/{postId}/comments\")\nsuspend fun getCommentsById(@RequestType(Int::class) @Path(\"postId\") postId: String): List<Comment>\n

You can set RequestType at a parameter with a type to which the parameter should be converted.

Then you need to implement a Converter factory with a RequestParameterConverter.

class StringToIntRequestConverterFactory : Converter.Factory {\noverride fun requestParameterConverter(\nparameterType: KClass<*>,\nrequestType: KClass<*>\n): Converter.RequestParameterConverter? {\nreturn object : Converter.RequestParameterConverter {\noverride fun convert(data: Any): Any {\n//convert the data\n}\n}\n}\n}\n
ktorfit.converterFactories(StringToIntRequestConverterFactory())\n
"},{"location":"converters/responseconverter/","title":"ResponseConverter","text":"

You only need ResponseConverters for cases where you can\u2019t use a suspend function in your interface. For every other case you want SuspendResponseConverter

Because Ktor relies on Coroutines by default your functions need to have the suspend modifier.

Let\u2019s say you have API endpoint to get a list of comments and you want to get them as Flow.

Ktorfit already has a converter for Flow, but it\u2019s used as an example

@GET(\"/user\")\nfun getUser(): Flow<List<Commment>>\n

Now you need a converter that can convert the HTTPResponse and return a Flow. Create a class that extends Converter.Factory

class FlowConverterFactory : Converter.Factory {\n}\n

Next you need to overwrite responseConverter()

override fun responseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.ResponseConverter<HttpResponse, *>? {\n

Inside responseConverter you can decide if you want to return a converter. In our case we want a converter for the type Flow. We can check that case with the typeData that we get as a parameter.

override fun suspendResponseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.SuspendResponseConverter<HttpResponse, *>? {\nif (typeData.typeInfo.type == Flow::class) {\n...\n}\nreturn null\n}\n

Next we create the ResponseConverter:

if (typeData.typeInfo.type == User::class) {\nval requestType = typeData.typeArgs.first()\n\nreturn object : Converter.ResponseConverter<HttpResponse, Flow<Any>> {\noverride fun convert(getResponse: suspend () -> HttpResponse): Flow<Any> {\nreturn flow {\ntry {\nval response = getResponse()\nif (requestType.typeInfo.type == HttpResponse::class) {\nemit(response)\n} else {\nval data = ktorfit.nextSuspendResponseConverter(this@FlowConverterFactory, requestType)\n?.convert(response)\nemit(data)\n}\n} catch (exception: Exception) {\nthrow exception\n}\n}\n}\n}\n}\n

Inside of convert we get the HttpResponse from getResponse(). We use nextSuspendResponseConverter to find the next converter that can convert the response. Then we put the converted response in the Flow and return it.

Finally, add your converter factory to the Ktorfit Builder

Ktorfit.Builder().converterFactories(FlowConverterFactory()).baseUrl(\"foo\").build()\n
"},{"location":"converters/responseconverter/#flow","title":"Flow","text":"

Add this dependency:

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-flow:$CONVERTER_VERSION\")\n

Ktorfit has support for Kotlin Flow. You need add the FlowConverterFactory() to your Ktorfit instance.

ktorfit.converterFactories(FlowConverterFactory())\n
@GET(\"comments\")\nfun getCommentsById(@Query(\"postId\") postId: String): Flow<List<Comment>>\n

Then you can drop the suspend modifier and wrap your return type with Flow<>

"},{"location":"converters/responseconverter/#call","title":"Call","text":"

Add this dependency:

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-call:$CONVERTER_VERSION\")\n

ktorfit.converterFactories(CallConverterFactory())\n
@GET(\"people/{id}/\")\nfun getPersonById(@Path(\"id\") peopleId: Int): Call<People>\n
exampleApi.getPersonById(3).onExecute(object : Callback<People> {\noverride fun onResponse(call: People, response: HttpResponse) {\n//Do something with Response\n}\n\noverride fun onError(exception: Exception) {\n//Do something with exception\n}\n})\n

You can use Call to receive the response in a Callback."},{"location":"converters/suspendresponseconverter/","title":"SuspendResponseConverter","text":"

Let`s say you have a function that requests a list of comments

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

But now you want to directly wrap your comment list in your data holder class e.g. \u201cMyOwnResponse\u201d

sealed class MyOwnResponse<T> {\ndata class Success<T>(val data: T) : MyOwnResponse<T>()\nclass Error(val ex:Throwable) : MyOwnResponse<Nothing>()\n\ncompanion object {\nfun <T> success(data: T) = Success(data)\nfun error(ex: Throwable) = Error(ex)\n}\n}\n

To enable that, you have to implement a SuspendResponseConverter. This class will be used to wrap the Ktor response inside your wrapper class.

class MyOwnResponseConverterFactory : Converter.Factory{\n\noverride fun suspendResponseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.SuspendResponseConverter<HttpResponse, *>? {\nif(typeData.typeInfo.type == MyOwnResponse::class) {\n\nreturn object : Converter.SuspendResponseConverter<HttpResponse, Any> {\noverride suspend fun convert(response: HttpResponse): Any {\nreturn try {\nMyOwnResponse.success(response.body(typeData.typeArgs.first().typeInfo))\n} catch (ex: Throwable) {\nMyOwnResponse.error(ex)\n}\n}\n}\n}\nreturn null\n}\n}\n

You can then add the ResponseConverter to your Ktorfit object.

ktorfit.converterFactories(MyOwnResponseConverterFactory())\n

Now add MyOwnResponse to your function

@GET(\"posts/{postId}/comments\")\nsuspend fun getCommentsByPostId(@Path(\"postId\") postId: Int): MyOwnResponse<List<Comment>>\n

"},{"location":"fundamentals/scope/","title":"Scope of Ktorfit","text":"

The goal of Ktorfit is to provide a similar developer experience like Retrofit for Kotlin Multiplatform projects. It`s not a 100% drop-in replacement for Retrofit. It uses Ktor clients because they are available on nearly every compile target of KMP. Every feature should be implemented so that it works on all platforms that Ktor supports. Before a new functionality is added to Ktorfit, it should be checked if there is already a Ktor plugin for it which solves the same problem.

"}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Overview","text":"Ktorfit"},{"location":"#introduction","title":"Introduction","text":"

Ktorfit is a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit

"},{"location":"#compatibility","title":"Compatibility","text":"Ktorfit-version 2.1.0 https://github.com/Foso/Ktorfit/blob/master/docs/CHANGELOG.md#210 2.0.1 https://github.com/Foso/Ktorfit/blob/master/docs/CHANGELOG.md#201 2.0.0 https://github.com/Foso/Ktorfit/blob/master/docs/CHANGELOG.md#200"},{"location":"#installation","title":"Installation","text":"

Please see Installation

"},{"location":"#quick-start","title":"Quick start","text":"

Please see Quick start

"},{"location":"#requests","title":"Requests","text":"

See Requests

"},{"location":"#converters","title":"Converters","text":"

See documentation Here

"},{"location":"#changelog","title":"Changelog","text":"

See changelog

"},{"location":"#acknowledgments","title":"Acknowledgments","text":"

Some parts of this project are reusing ideas that are originally coming from Retrofit from Square. Thank you for Retrofit!

Thanks to JetBrains for Ktor and Kotlin!

"},{"location":"#contributions","title":"Contributions","text":"

When you find unexpected behaviour please write an issue

"},{"location":"CHANGELOG/","title":"Changelog","text":"

All important changes of this project must be documented in this file.

The format is based on Keep a Changelog and this project orients towards Semantic Versioning. Note: This project needs KSP to work and every new Ktorfit with an update of the KSP version is technically a breaking change. But there is no intent to bump the Ktorfit major version for every KSP update.

"},{"location":"CHANGELOG/#210","title":"2.1.0","text":""},{"location":"CHANGELOG/#added","title":"Added","text":""},{"location":"CHANGELOG/#changed","title":"Changed","text":""},{"location":"CHANGELOG/#fixed","title":"Fixed","text":""},{"location":"CHANGELOG/#ktor3","title":"Ktor3","text":"

The \u201cnormal\u201d dependencies will stay on Ktor 2.x till 3.0 is stable. But here are versions that you can use when want to use Ktor3 and WasmJs

de.jensklingenberg.ktorfit:ktorfit-lib-light-ktor-3.0.0-beta-2:2.1.0

de.jensklingenberg.ktorfit:ktorfit-lib-ktor-3.0.0-beta-2:2.1.0

de.jensklingenberg.ktorfit:ktorfit-converters-flow-ktor-3.0.0-beta-2:2.1.0

de.jensklingenberg.ktorfit:ktorfit-converters-call-ktor-3.0.0-beta-2:2.1.0

de.jensklingenberg.ktorfit:ktorfit-converters-response-ktor-3.0.0-beta-2:2.1.0

"},{"location":"CHANGELOG/#201","title":"2.0.1","text":""},{"location":"CHANGELOG/#201-2024-08-08","title":"2.0.1 - 2024-08-08","text":""},{"location":"CHANGELOG/#fixed_1","title":"Fixed","text":""},{"location":"CHANGELOG/#ktorfit-annotations","title":"ktorfit-annotations","text":""},{"location":"CHANGELOG/#compilerplugin","title":"compilerPlugin","text":""},{"location":"CHANGELOG/#ktorfit-ksp","title":"ktorfit-ksp","text":""},{"location":"CHANGELOG/#ktor3_1","title":"Ktor3","text":"

The \u201cnormal\u201d dependencies will stay on Ktor 2.x till 3.0 is stable. But here are versions that you can use when want to use Ktor3 and WasmJs

Project Version ktorfit-lib-light-ktor-3.0.0-beta-2 ktorfit-lib-ktor-3.0.0-beta-2 ktorfit-converters-flow-ktor-3.0.0-beta-2 ktorfit-converters-call-ktor-3.0.0-beta-2 ktorfit-converters-response-ktor-3.0.0-beta-2"},{"location":"CHANGELOG/#200","title":"2.0.0","text":""},{"location":"CHANGELOG/#ktorfit-ksp-200-1024-2024-08-06","title":"ktorfit-ksp-2.0.0-1.0.24 - 2024-08-06","text":""},{"location":"CHANGELOG/#ktorfit-ksp-200-1023-2024-07-14","title":"ktorfit-ksp-2.0.0-1.0.23 - 2024-07-14","text":""},{"location":"CHANGELOG/#-build-with-ksp-1023","title":"- Build with KSP 1.0.23","text":""},{"location":"CHANGELOG/#ktorfit-ksp-200-1022-2024-06-06","title":"ktorfit-ksp-2.0.0-1.0.22 - 2024-06-06","text":""},{"location":"CHANGELOG/#200-2024-05-27","title":"2.0.0 - 2024-05-27","text":""},{"location":"CHANGELOG/#changed_1","title":"Changed","text":"
plugins {\nid(\"com.google.devtools.ksp\") version \"CURRENT_KSP_VERSION\"\nid(\"de.jensklingenberg.ktorfit\") version \"2.0.0\"\n}\n

See the installation guide for more information: https://foso.github.io/Ktorfit/installation/

val api = ktorfit.create<ExampleApi>()\n

replace with:

val api = ktorfit.createExampleApi()\n
"},{"location":"CHANGELOG/#breaking-changes","title":"Breaking Changes","text":"

The deprecated code got removed. This will simplify the codebase and make it easier to maintain. When you haven\u2019t used the deprecated converters, there is not much you need to change. The dependencies for the converters that were previously auto added now need to be added manually. See the migration guide for more information: https://foso.github.io/Ktorfit/migration/#from-2-to-200

In the previous versions of Ktorfit, the qualifiedTypename was always generated in the code. This was used in the TypeData.createTypeData() function to provide a fully qualified type name for the data type being used.

val _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\nqualifiedTypename = \"de.jensklingenberg.ktorfit.Call<com.example.model.People>\"\n)\n

In the new version of Ktorfit, this behavior has been changed. Now, by default, Ktorfit will keep qualifiedTypename for TypeData in the generated code empty. This means that the qualifiedTypename will not be automatically generated.

val _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\n)\n

However, if you want to keep the old behavior and generate qualifiedTypename, you can set it in the Ktorfit config generateQualifiedTypeName to true in your build.gradle.kts file.

build.gradle.kts
ktorfit {\ngenerateQualifiedTypeName = true\n}\n

This change was made to provide more flexibility and control to the developers over the generated code. Please update your code accordingly if you were relying on the automatic generation of qualifiedTypename.

"},{"location":"CHANGELOG/#fixed_2","title":"Fixed","text":""},{"location":"CHANGELOG/#200-rc01-2024-05-19","title":"2.0.0-rc01 - 2024-05-19","text":""},{"location":"CHANGELOG/#200-beta1-2024-04-28","title":"2.0.0-beta1 - 2024-04-28","text":""},{"location":"CHANGELOG/#breaking-changes_1","title":"Breaking Changes","text":"

The deprecated code got removed. This will simplify the codebase and make it easier to maintain. When you haven\u2019t used the deprecated converters, there is not much you need to change. Some converters that were previously auto applied now need to be added manually. See the migration guide for more information: https://foso.github.io/Ktorfit/migration/#from-2-to-200

"},{"location":"CHANGELOG/#qualifiedtypename-in-ktorfit","title":"QualifiedTypeName in Ktorfit","text":"

In the previous versions of Ktorfit, the qualifiedTypename was always generated in the code. This was used in the TypeData.createTypeData() function to provide a fully qualified type name for the data type being used.

val _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\nqualifiedTypename = \"de.jensklingenberg.ktorfit.Call<com.example.model.People>\"\n)\n

In the new version of Ktorfit, this behavior has been changed. Now, by default, Ktorfit will keep qualifiedTypename for TypeData in the generated code empty. This means that the qualifiedTypename will not be automatically generated.

val _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\n)\n

However, if you want to keep the old behavior and generate qualifiedTypename, you can set a KSP argument Ktorfit_QualifiedTypeName to true in your build.gradle.kts file.

ksp {\narg(\"Ktorfit_QualifiedTypeName\", \"true\")\n}\n

This change was made to provide more flexibility and control to the developers over the generated code. Please update your code accordingly if you were relying on the automatic generation of qualifiedTypename.

"},{"location":"CHANGELOG/#1140-2024-04-15","title":"1.14.0 - 2024-04-15","text":""},{"location":"CHANGELOG/#1130-2024-04-14","title":"1.13.0 - 2024-04-14","text":""},{"location":"CHANGELOG/#1120-2024-01-16","title":"1.12.0 - 2024-01-16","text":""},{"location":"CHANGELOG/#1111-2023-12-21","title":"1.11.1 - 2023-12-21","text":""},{"location":"CHANGELOG/#1110-2023-12-06","title":"1.11.0 - 2023-12-06","text":"

Compatible with KSP 1.0.15 and Kotlin 1.9.21

"},{"location":"CHANGELOG/#changed_2","title":"Changed","text":""},{"location":"CHANGELOG/#1102-2023-11-29","title":"1.10.2 - 2023-11-29","text":""},{"location":"CHANGELOG/#fixed_3","title":"Fixed","text":""},{"location":"CHANGELOG/#1101-2023-11-15","title":"1.10.1 - 2023-11-15","text":""},{"location":"CHANGELOG/#fixed_4","title":"Fixed","text":""},{"location":"CHANGELOG/#1100-2023-11-06","title":"1.10.0 - 2023-11-06","text":"

Compatible with KSP 1.0.14 and Kotlin 1.9.20

"},{"location":"CHANGELOG/#added_1","title":"Added","text":""},{"location":"CHANGELOG/#changed_3","title":"Changed","text":""},{"location":"CHANGELOG/#191-2023-10-22","title":"1.9.1 - 2023-10-22","text":"

Compatible with KSP 1.0.13 and Kotlin 1.9.10/1.9.20-RC

When you are still using the Ktorfit Gradle plugin in version 1.0.0, please also update this to 1.9.1

"},{"location":"CHANGELOG/#added_2","title":"Added","text":""},{"location":"CHANGELOG/#changed_4","title":"Changed","text":""},{"location":"CHANGELOG/#181-2023-10-09","title":"1.8.1 - 2023-10-09","text":"

Compatible with KSP 1.0.13 and Kotlin 1.9.10/1.9.20-Beta2

"},{"location":"CHANGELOG/#added_3","title":"Added","text":""},{"location":"CHANGELOG/#changed_5","title":"Changed","text":""},{"location":"CHANGELOG/#deprecated","title":"Deprecated","text":"

! When you still use the deprecated converters because there are use cases which you can\u2019t do with the converter factories, please write a GitHub Issue. The deprecated converters will be removed in one of the upcoming versions

"},{"location":"CHANGELOG/#removed","title":"Removed","text":""},{"location":"CHANGELOG/#fixed_5","title":"Fixed","text":""},{"location":"CHANGELOG/#security","title":"Security","text":""},{"location":"CHANGELOG/#180-2023-10-09","title":"1.8.0 - 2023-10-09","text":"

Skip this version, it had an issue with Kotlin 1.9.20

"},{"location":"CHANGELOG/#170-1920-beta2-2023-09-16","title":"1.7.0-1.9.20-Beta2 - 2023-09-16","text":"

Version of 1.7.0 that is compatible with Kotlin 1.9.20-Beta2

"},{"location":"CHANGELOG/#170-2023-09-16","title":"1.7.0 - 2023-09-16","text":""},{"location":"CHANGELOG/#gradle-plugin","title":"Gradle plugin","text":"

From now on with every Ktorfit release there will also be a Gradle plugin with the same version. That means that drop you can drop the Gradle extension block where you previously set the version number and just bump the number of the Gradle plugin. https://foso.github.io/Ktorfit/installation/

plugins {\nid(\"de.jensklingenberg.ktorfit\") version \"1.7.0\"\n}\n
"},{"location":"CHANGELOG/#added_4","title":"Added","text":""},{"location":"CHANGELOG/#changed_6","title":"Changed","text":""},{"location":"CHANGELOG/#160-2023-08-24","title":"1.6.0 - 2023-08-24","text":""},{"location":"CHANGELOG/#changed_7","title":"Changed","text":""},{"location":"CHANGELOG/#150-2023-08-04","title":"1.5.0 - 2023-08-04","text":""},{"location":"CHANGELOG/#changed_8","title":"Changed","text":""},{"location":"CHANGELOG/#144-2023-07-26","title":"1.4.4 - 2023-07-26","text":""},{"location":"CHANGELOG/#changed_9","title":"Changed","text":""},{"location":"CHANGELOG/#143-2023-07-13","title":"1.4.3 - 2023-07-13","text":""},{"location":"CHANGELOG/#fixed_6","title":"Fixed","text":""},{"location":"CHANGELOG/#372-crash-with-xiaomi-on-create-ktorfitbuilder-by-princeparadoxes","title":"372 Crash with Xiaomi on create Ktorfit.Builder by @princeparadoxes","text":""},{"location":"CHANGELOG/#142-2023-06-25","title":"1.4.2 - 2023-06-25","text":""},{"location":"CHANGELOG/#fixed_7","title":"Fixed","text":""},{"location":"CHANGELOG/#323-code-generation-issue-for-multipart-formurlencoded-by-ph1ll1pp","title":"323 Code generation issue for @Multipart / @FormUrlEncoded by @Ph1ll1pp","text":""},{"location":"CHANGELOG/#141-2023-06-03","title":"1.4.1 - 2023-06-03","text":""},{"location":"CHANGELOG/#changed_10","title":"Changed","text":""},{"location":"CHANGELOG/#fixed_8","title":"Fixed","text":""},{"location":"CHANGELOG/#236-parsing-error-for-listarray","title":"236 Parsing error for list/array","text":""},{"location":"CHANGELOG/#140-2023-05-27","title":"1.4.0 - 2023-05-27","text":""},{"location":"CHANGELOG/#added_5","title":"Added","text":"

e.g.

interface ExampleApi{\nsuspend fun getUser(): Response<User>\n}\n\nval user = userKtorfit.create<ExampleApi>().getUser()\n\nif(user.isSuccessful){\nuser.body()\n}else{\nuser.errorBody()\n}\n
"},{"location":"CHANGELOG/#85-added-a-response-class-that-can-be-used-as-a-wrapper-around-the-api-response-the-converter-for-it-is-automatically-applied-thx-to-vovahost-datl4g","title":"85 Added a Response class that can be used as a wrapper around the API Response, the converter for it is automatically applied. thx to @vovahost, @DATL4G","text":""},{"location":"CHANGELOG/#changed_11","title":"Changed","text":""},{"location":"CHANGELOG/#deprecated_1","title":"Deprecated","text":""},{"location":"CHANGELOG/#130-2023-05-14","title":"1.3.0 - 2023-05-14","text":""},{"location":"CHANGELOG/#changed_12","title":"Changed","text":""},{"location":"CHANGELOG/#fixed_9","title":"Fixed","text":"

[Bug]: IllegalArgumentException with Custom Http Annotation #274

"},{"location":"CHANGELOG/#bumped","title":"Bumped","text":"

KSP version to 1.0.11

"},{"location":"CHANGELOG/#120-2023-05-05","title":"1.2.0 - 2023-05-05","text":""},{"location":"CHANGELOG/#bumped_1","title":"Bumped","text":"

Now based on Ktor 2.3.0

"},{"location":"CHANGELOG/#110-2023-04-15","title":"1.1.0 - 2023-04-15","text":""},{"location":"CHANGELOG/#added_6","title":"Added","text":"

From now on there are two versions of the ktorfit-lib.

\u201cde.jensklingenberg.ktorfit:ktorfit-lib\u201d will stay like before and add the platform client dependencies for the clients.

\u201cde.jensklingenberg.ktorfit:ktorfit-lib-light\u201d this will only add the client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as \u201cktorfit-lib\u201d

"},{"location":"CHANGELOG/#changed_13","title":"Changed","text":""},{"location":"CHANGELOG/#bumped_2","title":"Bumped","text":""},{"location":"CHANGELOG/#101-2023-03-20","title":"1.0.1 - 2023-03-20","text":""},{"location":"CHANGELOG/#added_7","title":"Added","text":"

From now on there are two versions of the ktorfit-lib.

\u201cde.jensklingenberg.ktorfit:ktorfit-lib\u201d will stay like before and add the platform client dependencies for the clients.

\u201cde.jensklingenberg.ktorfit:ktorfit-lib-light\u201d this will only add the client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as \u201cktorfit-lib\u201d

"},{"location":"CHANGELOG/#fixed_10","title":"Fixed","text":"

[Bug]: Post request body serialization doesn\u2019t work #202

"},{"location":"CHANGELOG/#100-2023-03-02","title":"1.0.0 - 2023-03-02","text":"

This project is now following semver

"},{"location":"CHANGELOG/#added_8","title":"Added","text":""},{"location":"CHANGELOG/#fixed_11","title":"Fixed","text":""},{"location":"CHANGELOG/#bumped_3","title":"Bumped","text":""},{"location":"CHANGELOG/#100-beta18-12-02-2023","title":"1.0.0-beta18 (12-02-2023)","text":"

NEW:

Ktorfit.Builder().baseUrl(testBaseUrl, checkUrl = false).build()\n

\ud83d\udc1b Bugs fixed

\u2b06\ufe0f Deps updates

"},{"location":"CHANGELOG/#100-beta17-21-01-2023","title":"1.0.0-beta17 (21-01-2023)","text":"

\ud83d\udca5 Breaking changes:

Add this plugin:

plugins {\nid \"de.jensklingenberg.ktorfit\" version \"1.0.0\"\n}\n

NEW:

\ud83d\udc1b Bugs fixed

\u2b06\ufe0f Deps updates

"},{"location":"CHANGELOG/#100-beta16-13-11-2022","title":"1.0.0-beta16 (13-11-2022)","text":"

NEW: - Field parameters can now be nullable, null values will be ignored in requests - Add option to turn of error checking

ksp { arg(\u201cKtorfit_Errors\u201d, \u201c1\u201d) }

You can set it in your build.gradle.kts file,

0: Turn off all Ktorfit related error checking

1: Check for errors

2: Turn errors into warnings

\u2b06\ufe0f Deps updates

\ud83d\udc1b Bugs fixed

"},{"location":"CHANGELOG/#whats-changed","title":"What\u2019s Changed","text":"

Full Changelog: https://github.com/Foso/Ktorfit/compare/v1.0.0-beta15...v1.0.0-beta16

"},{"location":"CHANGELOG/#100-beta15-05-10-2022","title":"1.0.0-beta15 (05-10-2022)","text":"

\u2b06\ufe0f Deps updates

\ud83d\udc1b Bugs fixed

\ud83d\udca5 Breaking changes

"},{"location":"CHANGELOG/#100-beta14-24-09-2022","title":"1.0.0-beta14 (24-09-2022)","text":"

NEW: - Query parameters can now be nullable, null values will be ignored in requests - Function return types can now be nullable

FIX: - Url annotation not resolved correctly #65

BREAKING CHANGES: - Changed naming of Converters:

"},{"location":"CHANGELOG/#100-beta13-10-09-2022","title":"1.0.0-beta13 (10-09-2022)","text":"

Fixed: - Url annotation not resolved correctly #52

"},{"location":"CHANGELOG/#100-beta12-31-08-2022","title":"1.0.0-beta12 (31-08-2022)","text":""},{"location":"CHANGELOG/#breaking-changes_2","title":"Breaking Changes:","text":"

wrapResponse from SuspendResponseConverter got renamed to wrapSuspendResponse. This add the possibility to have ResponseConverter and SuspendResponseConverter implemented in the same class.

"},{"location":"CHANGELOG/#changes","title":"Changes:","text":"

val ktorfit = ktorfit {\nbaseUrl(\"http://example.com/\")\n}.create<GithubService>()\n
or this

val ktorfit = ktorfit {\nbaseUrl(\"http://example.com/\")\n}.createGithubService()\n

By default, IntelliJ/Android Studio can\u2019t find the generated code, you need to add the KSP generated folder to the sourcesets like this: (See more here: https://kotlinlang.org/docs/ksp-quickstart.html#make-ide-aware-of-generated-code)

kotlin.srcDir(\"build/generated/ksp/jvm/jvmMain/\")\n
"},{"location":"CHANGELOG/#100-beta11-21-08-2022","title":"1.0.0-beta11 (21-08-2022)","text":""},{"location":"CHANGELOG/#100-beta10-18-08-2022","title":"1.0.0-beta10 (18-08-2022)","text":"

to this:

Ktorfit.Builder()\n.baseUrl(\"https://example.com/\")\n.httpClient(HttpClient {})\n.build()\n
"},{"location":"CHANGELOG/#breaking-changes_3","title":"Breaking Changes:","text":"

@Headers now requires a vararg of String instead of an Array e.g. you need to change from:

@Headers(\n[\"Authorization: token ghp_abcdefgh\",\n\"Content-Type: application/json\"]\n)\n

to this:

@Headers(\n\"Authorization: token ghp_abcdefgh\",\n\"Content-Type: application/json\"\n)\n

"},{"location":"CHANGELOG/#100-beta09","title":"1.0.0-beta09","text":""},{"location":"CHANGELOG/#15-fix-encoding-of-query-parameters","title":"15 fix encoding of query parameters","text":""},{"location":"CHANGELOG/#100-beta08","title":"1.0.0-beta08","text":""},{"location":"CHANGELOG/#100-beta07","title":"1.0.0-beta07","text":""},{"location":"CHANGELOG/#100-beta06","title":"1.0.0-beta06","text":""},{"location":"CHANGELOG/#100-beta05","title":"1.0.0-beta05","text":""},{"location":"CHANGELOG/#100-beta04","title":"1.0.0-beta04","text":"

initial release

"},{"location":"License/","title":"License","text":"
                             Apache License\n                       Version 2.0, January 2004\n                    http://www.apache.org/licenses/\n

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

  1. Definitions.

    \u201cLicense\u201d shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.

    \u201cLicensor\u201d shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.

    \u201cLegal Entity\u201d shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, \u201ccontrol\u201d means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.

    \u201cYou\u201d (or \u201cYour\u201d) shall mean an individual or Legal Entity exercising permissions granted by this License.

    \u201cSource\u201d form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.

    \u201cObject\u201d form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.

    \u201cWork\u201d shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).

    \u201cDerivative Works\u201d shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.

    \u201cContribution\u201d shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, \u201csubmitted\u201d means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as \u201cNot a Contribution.\u201d

    \u201cContributor\u201d shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.

  2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.

  3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.

  4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:

    (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and

    (b) You must cause any modified files to carry prominent notices stating that You changed the files; and

    \u00a9 You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and

    (d) If the Work includes a \u201cNOTICE\u201d text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.

    You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.

  5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.

  6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.

  7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an \u201cAS IS\u201d BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.

  8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.

  9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

  To apply the Apache License to your work, attach the following\n  boilerplate notice, with the fields enclosed by brackets \"[]\"\n  replaced with your own identifying information. (Don't include\n  the brackets!)  The text should be enclosed in the appropriate\n  comment syntax for the file format. We also recommend that a\n  file or class name and description of purpose be included on the\n  same \"printed page\" as the copyright notice for easier\n  identification within third-party archives.\n

Copyright 2022 Jens Klingenberg

Licensed under the Apache License, Version 2.0 (the \u201cLicense\u201d); 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\n

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \u201cAS IS\u201d 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.

"},{"location":"architecture/","title":"How Ktorfit works under the hood","text":"

Ktorfit consists of three main components KSP-Plugin, Compiler plugin and the Ktorfit lib

"},{"location":"architecture/#ksp-plugin","title":"KSP-Plugin","text":"

This will generate the code for the implementation of the interfaces

"},{"location":"architecture/#compiler-plugin","title":"Compiler plugin","text":"

This transforms the create() function from the Ktorfit lib

"},{"location":"architecture/#ktorfit-lib","title":"Ktorfit lib","text":"

A wrapper around Ktor to simplify code generation

"},{"location":"architecture/#example","title":"Example","text":"

package com.example\n\nimport com.example.model.People\nimport de.jensklingenberg.ktorfit.http.GET\n\ninterface ExampleApi  {\n@GET(\"/test\")\nsuspend fun exampleGet(): People\n}\n
Let`s say we have a interface like this.

At compile time Ktorfit/KSP checks for all functions that are annotated with Ktorfit annotations like @GET.

Then it looks at the parent interfaces of that functions and generates, the source code of a Kotlin class that implements the interface. The classes are named like the interfaces but with an underscore at the beginning and \u201cImpl\u201d at the end, and they have the same package as the interfaces. In this case a class named _ExampleApiImpl will be generated.

@OptIn(InternalKtorfitApi::class)\npublic class _ExampleApiImpl(\nprivate val _ktorfit: Ktorfit,\n) : ExampleApi {\nprivate val _helper: KtorfitConverterHelper = KtorfitConverterHelper(_ktorfit)\n\noverride suspend fun exampleGet(): People {\nval _ext: HttpRequestBuilder.() -> Unit = {\nmethod = HttpMethod.parse(\"GET\")\nurl{\ntakeFrom(_ktorfit.baseUrl + \"/test\")\n}\n}\nval _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<People>(),\n)\nreturn _helper.suspendRequest(_typeData,_ext)!!\n}\n}\n\npublic class _ExampleApiProvider : ClassProvider<ExampleApi> {\noverride fun create(_ktorfit: Ktorfit): ExampleApi = _ExampleApiImpl(_ktorfit)\n}\n\npublic fun Ktorfit.createExampleApi(): ExampleApi = _ExampleApiImpl(this)\n

The next part is the compiler plugin which is added by the gradle plugin. It looks for the every usage of the create function from the Ktorfit-lib and adds an object of the wanted implementation class as an argument. Because of the naming convention of the generated classes we can deduce the name of the class from the name of type parameter.

val api = jvmKtorfit.create<ExampleApi>()\n

will be transformed to:

val api = jvmKtorfit.create<ExampleApi>(_ExampleApiImpl(jvmKtorfit))\n

The create() function is used, checks that the compiler plugin replaced the default value

public fun <T> create(data: T? = null): T {\nif (data == null) {\nthrow IllegalArgumentException(ENABLE_GRADLE_PLUGIN)\n}\nreturn data\n}\n
"},{"location":"configuration/","title":"Gradle","text":""},{"location":"configuration/#compile-errors","title":"Compile errors","text":"

By default, Ktorfit will throw compile error when it finds conditions under which it can\u2019t ensure that it will work correct. You can set it in the Ktorfit config to change this

ktorfit{\nerrorCheckingMode = ErrorCheckingMode.NONE\n}\n

You can set it in your build.gradle.kts file,

"},{"location":"configuration/#qualifiedtypename","title":"QualifiedTypeName","text":"

By default, Ktorfit will keep qualifiedTypename for TypeData in the generated code empty. You can set it in the Ktorfit config to change this:

ktorfit {\ngenerateQualifiedTypeName = true\n}\n
Default code generation
...\nval _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\n)\n...\n
With QualifiedTypeName true
...\nval _typeData = TypeData.createTypeData(\ntypeInfo = typeInfo<Call<People>>(),\nqualifiedTypename = \"de.jensklingenberg.ktorfit.Call<com.example.model.People>\"\n)\n...\n
"},{"location":"configuration/#ktorfit-builder","title":"Ktorfit Builder","text":""},{"location":"configuration/#add-your-own-ktor-client","title":"Add your own Ktor client","text":"

You can set your Ktor client instance to the Ktorfit builder:

val myClient = HttpClient()\nval ktorfit = Ktorfit.Builder().httpClient(myClient).build()\n
"},{"location":"development/","title":"Development","text":""},{"location":"development/#update-ktorfit-for-new-kotlin-version","title":"Update Ktorfit for new Kotlin version","text":""},{"location":"development/#project-structure","title":"\ud83d\udc77 Project Structure","text":""},{"location":"installation/","title":"Installation","text":""},{"location":"installation/#setup","title":"Setup","text":"

(You can also look how it\u2019s done in the examples)

"},{"location":"installation/#gradle-plugins","title":"Gradle Plugins","text":"

You need to add KSP and the Ktorfit Gradle plugin

plugins {\nid(\"com.google.devtools.ksp\") version \"CURRENT_KSP_VERSION\"\nid(\"de.jensklingenberg.ktorfit\") version \"2.1.0\"\n}\n

"},{"location":"installation/#ktorfit-lib","title":"Ktorfit-lib","text":"

Add the Ktorfit-lib to your common module. You can find all available versions here

val ktorfitVersion = \"2.1.0\"\n\nsourceSets {\nval commonMain by getting{\ndependencies{\nimplementation(\"de.jensklingenberg.ktorfit:ktorfit-lib:$ktorfitVersion\")\n}\n}\n

You can also use \u201cde.jensklingenberg.ktorfit:ktorfit-lib-light\u201d this will only add the Ktor client core dependency and not the platform dependencies for the clients. This will give you more control over the used clients, but you have to add them yourself. https://ktor.io/docs/http-client-engines.html Everything else is the same as \u201cktorfit-lib\u201d

"},{"location":"installation/#ktor","title":"Ktor","text":"

Ktorfit is based on Ktor clients 2.3.12. You don\u2019t need to add an extra dependency for the default clients. When you want to use Ktor plugins for things like serialization, you need to add the dependencies, and they need to be compatible with 2.3.12

"},{"location":"knownissues/","title":"Known Issues","text":""},{"location":"knownissues/#kmp-project-with-single-target","title":"KMP project with single target","text":"

When you have a KMP project with a single target, IntelliJ will find the generated \u201ccreate\u201d extension function (e.g. ktorfit.createExampleApi()) in your common module, but the compilation will fail because of an \u201cUnresolved reference\u201d error. In that case, you have to use ktorfit.create<ExampleApi>() to make it work, even though it\u2019s already deprecated.

Kotlin handles the compilation of a KMP project with a single target differently than with multiple targets.

See:

"},{"location":"migration/","title":"Migration","text":"

Here is how to migrate from deprecated code:

"},{"location":"migration/#from-2-to-200","title":"From <2 to 2.0.0","text":"

Add this dependency:

You can find all available versions here

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-response:$CONVERTER_VERSION\")\n

and add this converter to your Ktorfit instance:

.addConverterFactory(ResponseConverterFactory())\n

Add this dependency:

You can find all available versions here

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-call:$CONVERTER_VERSION\")\n

and add this converter to your Ktorfit instance:

.addConverterFactory(CallConverterFactory())\n

Add this dependency:

You can find all available versions here

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-flow:$CONVERTER_VERSION\")\n

and add this converter to your Ktorfit instance:

.addConverterFactory(FlowConverterFactory())\n

"},{"location":"migration/#from-170-to-181","title":"From 1.7.0 to 1.8.1","text":""},{"location":"migration/#suspendresponseconverter","title":"SuspendResponseConverter","text":"

Implement override suspend fun convert(result: KtorfitResult)

public suspend fun convert(result: KtorfitResult): T {\nreturn when (result) {\nis KtorfitResult.Failure -> {\nthrow result.throwable // Or do something with the throwable\n}\n\nis KtorfitResult.Success -> {\nval response = result.response\n//Put the code that was in your other convert function here\n}\n}\n}\n

Redirect the deprecated function to the new function:

override suspend fun convert(response: HttpResponse): Any {\nreturn convert(KtorfitResult.Success(response))\n}\n
"},{"location":"quick-start/","title":"Quick start","text":"

First do the Installation

Let\u2019s say you want to make a GET Request to https://swapi.dev/api/people/1/

Create a new Kotlin interface:

interface ExampleApi {\n@GET(\"people/1/\")\nsuspend fun getPerson(): String\n}\n

Now we add a function that will be used to make our request. The @GET annotation will tell Ktorfit that this a GET request. The value of @GET is the relative URL path that will be appended to the base url which we set later.

An interface used for Ktorfit needs to have a HTTP method annotation on every function. Because Ktor relies on Coroutines by default your functions need to have the suspend modifier. Alternatively you can use #Flow or Call

Info

The return type String will return the response text. When you want directly parse the response into a class you need to add a JSON,XML, etc. converter to Ktor

val ktorfit = Ktorfit.Builder().baseUrl(\"https://swapi.dev/api/\").build()\nval exampleApi = ktorfit.createExampleApi()\n

Next we use the Ktorfit builder to create a Ktorfit instance, and set the base url. After compiling the project we can then use the generated extension function to receive an implementation of the wanted type.

val response = exampleApi.getPerson()\nprintln(response)\n

Now we can use exampleApi to make the request.

"},{"location":"requests/","title":"Requests","text":""},{"location":"requests/#http-request","title":"HTTP Request","text":"

Ktorfit supports the following the HTTP method annotations:

Or you can set your custom method to @HTTP

@GET(\"posts\")\nfun getPosts(): List<Post>\n

The value of the HTTP annotation will be appended to the baseUrl that you set in the Ktorfit builder. If the value contains a url that starts with http or https, this url will be used for the request instead of the baseUrl.

@GET(\"https://example.com/posts\")\nfun getPosts(): List<Post>\n

The value can only be empty when you also use @Url

"},{"location":"requests/#url","title":"Url","text":"

Can be used to set a URL dynamically as a function parameter.

@GET(\"\")\nsuspend fun getPosts(@Url url: String): List<Post>\n

"},{"location":"requests/#query","title":"Query","text":"
@GET(\"comments\")\nsuspend fun getCommentsById(\n@Query(\"postId\") postId: String,\n@QueryName queryName: String,\n@QueryMap headerMap : Map<String,String>\n): List<Comment>\n

You can use @Query, @QueryName or @QueryMap to set queries to your request.

Example:

@GET(\"comments\")\nsuspend fun getCommentsById(@Query(\"postId\") postId: String): List<Comment>\n

A request with getCommentsById(3) will result in the relative URL \u201ccomments?postId=3\u201d

"},{"location":"requests/#path","title":"Path","text":"

When you want to dynamically replace a part of the URL, you can use the @Path annotation.

interface ExampleApi {\n@GET(\"people/{peopleId}/\")\nsuspend fun getPerson(@Path(\"peopleId\") id: String): String\n}\n
Just write a part of your URL path in curly braces. Then you need to annotate a parameter with @Path. The value of @Path needs to match with one of the curly braces part in your URL path.

Example:

On a request with getPerson(1) , {peopleId} will be replaced with the argument 1 and the relative URL will become \u201cpeople/1/\u201d

"},{"location":"requests/#headers","title":"Headers","text":"
@Headers(\"Accept: application/json\")\n@GET(\"comments\")\nsuspend fun requestWithHeaders(\n@Header(\"Content-Type\") name: String,\n@HeaderMap headerMap : Map<String,String>\n): List<Comment>\n

You can use @Headers, @Header or @HeaderMap to configure headers to your request.

"},{"location":"requests/#body","title":"Body","text":"
interface ExampleService {\n@POST(\"upload\")\nsuspend fun upload(@Body data: String)\n}\n

@Body can be used as parameter to send data in a request body. It can only be used with Http Methods that have a request body

"},{"location":"requests/#formdata","title":"FormData","text":"
@POST(\"signup\")\n@FormUrlEncoded\nsuspend fun signup(\n@Field(\"username\") username: String, @Field(\"email\") email: String,\n@Field(\"password\") password: String, @Field(\"confirmation\") confirmation: String\n): String\n

To send FormData you can use @Field or @FieldMap. Your function needs to be annotated with @FormUrlEncoded.

"},{"location":"requests/#tag","title":"Tag","text":"

Tag can be used to add a tag to a request.

@GET(\"posts\")\nfun getPosts(@Tag(\"myTag\") tag: String): List<Post>\n

You can then access the tag from the attributes of a Ktor HttpClientCall

val myTag = response.call.attributes[AttributeKey(\"myTag\")] 
"},{"location":"requests/#multipart","title":"Multipart","text":"

To send Multipart data you have two options:

"},{"location":"requests/#1-body","title":"1) @Body","text":"
interface ExampleService {\n@POST(\"upload\")\nsuspend fun upload(@Body map: MultiPartFormDataContent)\n}\n

To upload MultiPartFormData you need to have a parameter of the type MultiPartFormDataContent that is annotated with @Body. The method needs to be annotated with @POST or @PUT

val multipart = MultiPartFormDataContent(formData {\nappend(\"description\", \"Ktor logo\")\nappend(\"image\", File(\"ktor_logo.png\").readBytes(), Headers.build {\nappend(HttpHeaders.ContentType, \"image/png\")\nappend(HttpHeaders.ContentDisposition, \"filename=ktor_logo.png\")\n})\n})\n\nexampleApi.upload(multipart)\n

Then you can use Ktor\u2019s formData Builder to create the MultiPartFormDataContent

"},{"location":"requests/#2-multipart","title":"2) @MultiPart","text":"
@Multipart\n@POST(\"upload\")\nsuspend fun uploadFile(@Part(\"description\") description: String, @Part(\"\") file: List<PartData>): String\n

You can annotate a function with @Multipart. Then you can annotate parameters with @Part

val multipart = formData {\nappend(\"image\", File(\"ktor_logo.png\").readBytes(), Headers.build {\nappend(HttpHeaders.ContentType, \"image/png\")\nappend(HttpHeaders.ContentDisposition, \"filename=ktor_logo.png\")\n})\n}\n\nexampleApi.upload(\"Ktor logo\",multipart)\n

All your parameters annotated with @Part wil be combined and send as MultiPartFormDataContent

"},{"location":"requests/#json","title":"JSON","text":"

Ktorfit doesn\u2019t parse JSON. You have to install the Json Feature to the Ktor Client that you add to Ktorfit.

See here Add your own Ktor client and here https://ktor.io/docs/serialization-client.html

val ktorClient = HttpClient() {\ninstall(ContentNegotiation) {\njson(Json { isLenient = true; ignoreUnknownKeys = true })\n}\n}\n
"},{"location":"requests/#streaming","title":"Streaming","text":"
@Streaming\n@GET(\"docs/response.html#streaming\")\nsuspend fun getPostsStreaming(): HttpStatement\n

To receive streaming data you need to annotate a function with @Streaming and the return type has to be HttpStatement.

exampleApi.getPostsStreaming().execute { response ->\n//Do something with response\n}\n

For more information check Ktor docs

"},{"location":"requests/#requestbuilder","title":"RequestBuilder","text":"
@GET(\"comments\")\nsuspend fun getCommentsById(\n@Query(\"postId\") name: String,\n@ReqBuilder ext: HttpRequestBuilder.() -> Unit\n): List<Comment>\n

You need to set extra configuration on your request? Add a parameter with \u201c@ReqBuilder ext: HttpRequestBuilder.() -> Unit\u201d to your function.

val result = secondApi.getCommentsById(\"3\") {\nonDownload { bytesSentTotal, contentLength ->\nprintln(bytesSentTotal)\n}\n}\n

Then you can use the extension function to set additional configuration. The RequestBuilder will be applied last after everything that is set by Ktorfit

"},{"location":"responseconverter/","title":"Responseconverter","text":"

ResponseConverter is deprecated, use Converter.ResponseConverter instead

Let`s say you have a function that requests a list of comments

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

But now you want to directly wrap your comment list in your data holder class e.g. \u201cMyOwnResponse\u201d

sealed class MyOwnResponse<T> {\ndata class Success<T>(val data: T) : MyOwnResponse<T>()\nclass Error(val ex:Throwable) : MyOwnResponse<Nothing>()\n\ncompanion object {\nfun <T> success(data: T) = Success(data)\nfun error(ex: Throwable) = Error(ex)\n}\n}\n

To enable that, you have to implement a ResponseConverter. This class will be used to wrap the Ktor response inside your wrapper class.

class MyOwnResponseConverter : ResponseConverter {\n\noverride suspend fun <RequestType> wrapResponse(\ntypeData: TypeData,\nrequestFunction: suspend () -> Pair<TypeInfo, HttpResponse>,\nktorfit: Ktorfit\n): Any {\nreturn try {\nval (info, response) = requestFunction()\nMyOwnResponse.success<Any>(response.body(info))\n} catch (ex: Throwable) {\nMyOwnResponse.error(ex)\n}\n}\n\noverride fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean {\nreturn typeData.qualifiedName == \"com.example.model.MyOwnResponse\"\n}\n}\n

You can then add the ResponseConverter on your Ktorfit object.

ktorfit.responseConverter(MyOwnResponseConverter())\n

Now add MyOwnResponse to your function

@GET(\"posts/{postId}/comments\")\nsuspend fun getCommentsByPostId(@Path(\"postId\") postId: Int): MyOwnResponse<List<Comment>>\n

"},{"location":"suspendresponseconverter/","title":"Suspendresponseconverter","text":"

SuspendResponseConverter is deprecated, use Converter.SuspendResponseConverter instead

Because Ktor relies on Coroutines by default your functions need to have the suspend modifier.

To change this, you need to use a SuspendResponseConverter, you add your own or use Flow or Call

You can add RequestConverter on your Ktorfit object.

ktorfit.responseConverter(FlowResponseConverter())\n
"},{"location":"suspendresponseconverter/#flow","title":"Flow","text":"

Ktorfit has support for Kotlin Flow. You need add the FlowResponseConverter() to your Ktorfit instance.

ktorfit.responseConverter(FlowResponseConverter())\n
@GET(\"comments\")\nfun getCommentsById(@Query(\"postId\") postId: String): Flow<List<Comment>>\n

Then you can drop the suspend modifier and wrap your return type with Flow<>

"},{"location":"suspendresponseconverter/#call","title":"Call","text":"

ktorfit.responseConverter(CallResponseConverter())\n
@GET(\"people/{id}/\")\nfun getPersonById(@Path(\"id\") peopleId: Int): Call<People>\n

exampleApi.getPersonById(3).onExecute(object : Callback<People>{\noverride fun onResponse(call: People, response: HttpResponse) {\n//Do something with Response\n}\n\noverride fun onError(exception: Exception) {\n//Do something with exception\n}\n})\n

You can use Call to receive the response in a Callback."},{"location":"suspendresponseconverter/#your-own","title":"Your own","text":"

You can also add your own Converter. You just need to implement RequestConverter. Inside the converter you need to handle the conversion from suspend to your async code.

class MyOwnResponseConverter : SuspendResponseConverter {\n...\n
"},{"location":"android/proguard/","title":"proguard","text":"

proguard-rules.pro

-keep class de.jensklingenberg.ktorfit.** { *; }\n-keepclassmembers class de.jensklingenberg.ktorfit.** { *; }\n

"},{"location":"converters/converters/","title":"Converters","text":"

Converters are used to convert the HTTPResponse or parameters.

They are added inside of a Converter.Factory which will then be added to the Ktorfit builder with the converterfactories() function.

"},{"location":"converters/converters/#converter-types","title":"Converter Types","text":""},{"location":"converters/converters/#existing-converter-factories","title":"Existing converter factories","text":"

Add this dependency:

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-call:$CONVERTER_VERSION\")\n

You can find all available versions here

Add this dependency:

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-flow:$CONVERTER_VERSION\")\n

You can find all available versions here

Add this dependency:

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-response:$CONVERTER_VERSION\")\n

You can find all available versions here

"},{"location":"converters/example1/","title":"Example1","text":"

Let\u2019s say you want to get an user from an API and the response you get looks like below:

API response
{\n\"success\": true,\n\"user\":\n{\n\"id\": 1,\n\"name\": \"Jens Klingenberg\"\n}\n}\n

But you are only interested in the \u201cuser\u201d object, and you want to look your interface function something like this:

Example function
@GET(\"/user\")\nsuspend fun getUser(): User\n

First you need the Kotlin classes to which your JSON data is mapped to:

This example assumes that you are Kotlin Serialization

@kotlinx.serialization.Serializable\ndata class Envelope(val success: Boolean, val user: User)\n\n@kotlinx.serialization.Serializable\ndata class User(val id: Int, val name: String)\n

Now you need a converter that can convert the HTTPResponse and return a user object. Create a class that extends Converter.Factory

class UserFactory : Converter.Factory {\n\n}\n

Because in this case User is the return type of a suspend function, you need to create a SuspendResponseConverter. Override suspendResponseConverter()

class UserFactory : Converter.Factory {\noverride fun suspendResponseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.SuspendResponseConverter<HttpResponse, *>? {\n\n}\n}\n

Inside suspendResponseConverter you can decide if you want to return a converter. In our case we a converter for the type User. We can check that case with the typeData that we get as a parameter.

override fun suspendResponseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.SuspendResponseConverter<HttpResponse, *>? {\nif (typeData.typeInfo.type == User::class) {\n...\n}\nreturn null\n}\n

Next we create the SuspendResponseConverter:

if (typeData.typeInfo.type == User::class) {\nreturn object : Converter.SuspendResponseConverter<HttpResponse, Any> {\noverride suspend fun convert(response: HttpResponse): Any {\n...\n}\n}\n}\n
Inside of convert we get the HttpResponse and we want to return a User object.

Now we could do the following:

When we know that this converter will always be used for a API that wraps the User inside an Envelope class, we can directly transform the body to an envelope object and just return the user object.

override suspend fun convert(response: HttpResponse): Any {\nval envelope = response.body<Envelope>()\nreturn envelope.user\n}\n

or we can create a TypeData of Envelope and use nextSuspendResponseConverter() to look up the next converter that can convert the response

 override suspend fun convert(response: HttpResponse): Any {\nval typeData = TypeData.createTypeData(\"com.example.model.Envelope\", typeInfo<Envelope>())\nval envelope = ktorfit.nextSuspendResponseConverter(null, typeData)?.convert(response) as? Envelope\nreturn envelope.user\n}\n

Finally, add your converter factory to the Ktorfit Builder

Ktorfit.Builder().converterFactories(UserFactory()).baseUrl(\"foo\").build()\n
"},{"location":"converters/migration/","title":"Migration","text":" SuspendResponseConverter
 override suspend fun <RequestType> wrapSuspendResponse(\ntypeData: TypeData,\nrequestFunction: suspend () -> Pair<TypeInfo, HttpResponse>,\nktorfit: Ktorfit\n): Any {\nreturn object : Call<RequestType> {\noverride fun onExecute(callBack: Callback<RequestType>) {\n\nktorfit.httpClient.launch {\nval deferredResponse = async { requestFunction() }\n\nval (data, response) = deferredResponse.await()\n\ntry {\nval res = response.call.body(data)\ncallBack.onResponse(res as RequestType, response)\n} catch (ex: Exception) {\ncallBack.onError(ex)\n} } } } }\n\noverride fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean {\nreturn typeData.qualifiedName == \"de.jensklingenberg.ktorfit.Call\"\n}\n
Equivalent with converter factory:
public class CallConverterFactory : Converter.Factory {\n\noverride fun suspendResponseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.SuspendResponseConverter<HttpResponse, *>? {\nif (typeData.typeInfo.type == Call::class) {\nreturn object: Converter.SuspendResponseConverter<HttpResponse, Call<Any?>> {\noverride suspend fun convert(response: HttpResponse): Call<Any?> {\n\nreturn object : Call<Any?> {\noverride fun onExecute(callBack: Callback<Any?>) {\nktorfit.httpClient.launch {\ntry {\nval data = response.call.body(typeData.typeArgs.first().typeInfo)\ncallBack.onResponse(data!!, response)\n} catch (ex: Exception) {\ncallBack.onError(ex)\n} } } } } } }\nreturn null\n}\n}\n
ResponseConverter
override fun <RequestType> wrapResponse(\ntypeData: TypeData,\nrequestFunction: suspend () -> Pair<TypeInfo, HttpResponse?>,\nktorfit: Ktorfit\n): Any {\nreturn object : Call<RequestType> {\noverride fun onExecute(callBack: Callback<RequestType>) {\n\nktorfit.httpClient.launch {\nval deferredResponse = async { requestFunction() }\n\ntry {\nval (info, response) = deferredResponse.await()\nval data = response!!.body(info) as RequestType\ncallBack.onResponse(data, response)\n} catch (ex: Exception) {\ncallBack.onError(ex)\n}\n\n}\n}\n\n}\n}\n\noverride fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean {\nreturn typeData.qualifiedName == \"de.jensklingenberg.ktorfit.Call\"\n}\n
Equivalent with converter factory:
public class CallConverterFactory : Converter.Factory {\noverride fun responseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.ResponseConverter<HttpResponse, *>? {\nif (typeData.typeInfo.type == Call::class) {\nreturn object : Converter.ResponseConverter<HttpResponse, Call<Any?>> {\n\noverride fun convert(getResponse: suspend () -> HttpResponse): Call<Any?> {\nreturn object : Call<Any?> {\noverride fun onExecute(callBack: Callback<Any?>) {\nktorfit.httpClient.launch {\ntry {\nval response = getResponse()\n\nval data = response.call.body(typeData.typeArgs.first().typeInfo)\n\ncallBack.onResponse(data, response)\n} catch (ex: Exception) {\nprintln(ex)\ncallBack.onError(ex)\n} } } } } } }\nreturn null\n}\n}\n
"},{"location":"converters/requestparameterconverter/","title":"RequestParameterConverter","text":"
@GET(\"posts/{postId}/comments\")\nsuspend fun getCommentsById(@RequestType(Int::class) @Path(\"postId\") postId: String): List<Comment>\n

You can set RequestType at a parameter with a type to which the parameter should be converted.

Then you need to implement a Converter factory with a RequestParameterConverter.

class StringToIntRequestConverterFactory : Converter.Factory {\noverride fun requestParameterConverter(\nparameterType: KClass<*>,\nrequestType: KClass<*>\n): Converter.RequestParameterConverter? {\nreturn object : Converter.RequestParameterConverter {\noverride fun convert(data: Any): Any {\n//convert the data\n}\n}\n}\n}\n
ktorfit.converterFactories(StringToIntRequestConverterFactory())\n
"},{"location":"converters/responseconverter/","title":"ResponseConverter","text":"

You only need ResponseConverters for cases where you can\u2019t use a suspend function in your interface. For every other case you want SuspendResponseConverter

Because Ktor relies on Coroutines by default your functions need to have the suspend modifier.

Let\u2019s say you have API endpoint to get a list of comments and you want to get them as Flow.

Ktorfit already has a converter for Flow, but it\u2019s used as an example

@GET(\"/user\")\nfun getUser(): Flow<List<Commment>>\n

Now you need a converter that can convert the HTTPResponse and return a Flow. Create a class that extends Converter.Factory

class FlowConverterFactory : Converter.Factory {\n}\n

Next you need to overwrite responseConverter()

override fun responseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.ResponseConverter<HttpResponse, *>? {\n

Inside responseConverter you can decide if you want to return a converter. In our case we want a converter for the type Flow. We can check that case with the typeData that we get as a parameter.

override fun suspendResponseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.SuspendResponseConverter<HttpResponse, *>? {\nif (typeData.typeInfo.type == Flow::class) {\n...\n}\nreturn null\n}\n

Next we create the ResponseConverter:

if (typeData.typeInfo.type == User::class) {\nval requestType = typeData.typeArgs.first()\n\nreturn object : Converter.ResponseConverter<HttpResponse, Flow<Any>> {\noverride fun convert(getResponse: suspend () -> HttpResponse): Flow<Any> {\nreturn flow {\ntry {\nval response = getResponse()\nif (requestType.typeInfo.type == HttpResponse::class) {\nemit(response)\n} else {\nval data = ktorfit.nextSuspendResponseConverter(this@FlowConverterFactory, requestType)\n?.convert(response)\nemit(data)\n}\n} catch (exception: Exception) {\nthrow exception\n}\n}\n}\n}\n}\n

Inside of convert we get the HttpResponse from getResponse(). We use nextSuspendResponseConverter to find the next converter that can convert the response. Then we put the converted response in the Flow and return it.

Finally, add your converter factory to the Ktorfit Builder

Ktorfit.Builder().converterFactories(FlowConverterFactory()).baseUrl(\"foo\").build()\n
"},{"location":"converters/responseconverter/#flow","title":"Flow","text":"

Add this dependency:

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-flow:$CONVERTER_VERSION\")\n

Ktorfit has support for Kotlin Flow. You need add the FlowConverterFactory() to your Ktorfit instance.

ktorfit.converterFactories(FlowConverterFactory())\n
@GET(\"comments\")\nfun getCommentsById(@Query(\"postId\") postId: String): Flow<List<Comment>>\n

Then you can drop the suspend modifier and wrap your return type with Flow<>

"},{"location":"converters/responseconverter/#call","title":"Call","text":"

Add this dependency:

implementation(\"de.jensklingenberg.ktorfit:ktorfit-converters-call:$CONVERTER_VERSION\")\n

ktorfit.converterFactories(CallConverterFactory())\n
@GET(\"people/{id}/\")\nfun getPersonById(@Path(\"id\") peopleId: Int): Call<People>\n
exampleApi.getPersonById(3).onExecute(object : Callback<People> {\noverride fun onResponse(call: People, response: HttpResponse) {\n//Do something with Response\n}\n\noverride fun onError(exception: Exception) {\n//Do something with exception\n}\n})\n

You can use Call to receive the response in a Callback."},{"location":"converters/suspendresponseconverter/","title":"SuspendResponseConverter","text":"

Let`s say you have a function that requests a list of comments

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

But now you want to directly wrap your comment list in your data holder class e.g. \u201cMyOwnResponse\u201d

sealed class MyOwnResponse<T> {\ndata class Success<T>(val data: T) : MyOwnResponse<T>()\nclass Error(val ex:Throwable) : MyOwnResponse<Nothing>()\n\ncompanion object {\nfun <T> success(data: T) = Success(data)\nfun error(ex: Throwable) = Error(ex)\n}\n}\n

To enable that, you have to implement a SuspendResponseConverter. This class will be used to wrap the Ktor response inside your wrapper class.

class MyOwnResponseConverterFactory : Converter.Factory{\n\noverride fun suspendResponseConverter(\ntypeData: TypeData,\nktorfit: Ktorfit\n): Converter.SuspendResponseConverter<HttpResponse, *>? {\nif(typeData.typeInfo.type == MyOwnResponse::class) {\n\nreturn object : Converter.SuspendResponseConverter<HttpResponse, Any> {\noverride suspend fun convert(response: HttpResponse): Any {\nreturn try {\nMyOwnResponse.success(response.body(typeData.typeArgs.first().typeInfo))\n} catch (ex: Throwable) {\nMyOwnResponse.error(ex)\n}\n}\n}\n}\nreturn null\n}\n}\n

You can then add the ResponseConverter to your Ktorfit object.

ktorfit.converterFactories(MyOwnResponseConverterFactory())\n

Now add MyOwnResponse to your function

@GET(\"posts/{postId}/comments\")\nsuspend fun getCommentsByPostId(@Path(\"postId\") postId: Int): MyOwnResponse<List<Comment>>\n

"},{"location":"fundamentals/scope/","title":"Scope of Ktorfit","text":"

The goal of Ktorfit is to provide a similar developer experience like Retrofit for Kotlin Multiplatform projects. It`s not a 100% drop-in replacement for Retrofit. It uses Ktor clients because they are available on nearly every compile target of KMP. Every feature should be implemented so that it works on all platforms that Ktor supports. Before a new functionality is added to Ktorfit, it should be checked if there is already a Ktor plugin for it which solves the same problem.

"}]} \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml index a4ef28d60..18fb13452 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,102 +2,107 @@ https://github.com/Foso/Ktorfit/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/CHANGELOG/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/License/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/architecture/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/configuration/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/development/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/installation/ - 2024-08-26 + 2024-09-01 + daily + + + https://github.com/Foso/Ktorfit/knownissues/ + 2024-09-01 daily https://github.com/Foso/Ktorfit/migration/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/quick-start/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/requests/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/responseconverter/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/suspendresponseconverter/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/android/proguard/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/converters/converters/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/converters/example1/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/converters/migration/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/converters/requestparameterconverter/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/converters/responseconverter/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/converters/suspendresponseconverter/ - 2024-08-26 + 2024-09-01 daily https://github.com/Foso/Ktorfit/fundamentals/scope/ - 2024-08-26 + 2024-09-01 daily \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz index 382e7370c..8ad6dabed 100644 Binary files a/sitemap.xml.gz and b/sitemap.xml.gz differ diff --git a/suspendresponseconverter/index.html b/suspendresponseconverter/index.html index 400520c46..4983b27de 100644 --- a/suspendresponseconverter/index.html +++ b/suspendresponseconverter/index.html @@ -1,4 +1,4 @@ - Suspendresponseconverter - Ktorfit
Skip to content
🤔 Documentation issue? Report or edit

Suspendresponseconverter

SuspendResponseConverter is deprecated, use Converter.SuspendResponseConverter instead

Because Ktor relies on Coroutines by default your functions need to have the suspend modifier.

To change this, you need to use a SuspendResponseConverter, you add your own or use Flow or Call

You can add RequestConverter on your Ktorfit object.

ktorfit.responseConverter(FlowResponseConverter())
+ Suspendresponseconverter - Ktorfit       
🤔 Documentation issue? Report or edit

Suspendresponseconverter

SuspendResponseConverter is deprecated, use Converter.SuspendResponseConverter instead

Because Ktor relies on Coroutines by default your functions need to have the suspend modifier.

To change this, you need to use a SuspendResponseConverter, you add your own or use Flow or Call

You can add RequestConverter on your Ktorfit object.

ktorfit.responseConverter(FlowResponseConverter())
 

Flow

Ktorfit has support for Kotlin Flow. You need add the FlowResponseConverter() to your Ktorfit instance.

ktorfit.responseConverter(FlowResponseConverter())
 
@GET("comments")
 fun getCommentsById(@Query("postId") postId: String): Flow<List<Comment>>
@@ -16,4 +16,4 @@
 })
 

You can use Call to receive the response in a Callback.

Your own

You can also add your own Converter. You just need to implement RequestConverter. Inside the converter you need to handle the conversion from suspend to your async code.

class MyOwnResponseConverter : SuspendResponseConverter {
    ...
-

Last update: May 27, 2023
\ No newline at end of file +

Last update: May 27, 2023
\ No newline at end of file