Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update converters/example1.md #724

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ See https://foso.github.io/Ktorfit/generation/#nodelegation

OptIn annotations on interfaces and functions will now be propagated to the generated classes.

- Fixed documentation for converters to match the current version.

# [2.1.0]()

* Supported Kotlin version: 2.0.0; 2.0.10; 2.0.20, 2.1.0-Beta1; 2.0.21-RC, 2.0.21
Expand Down
12 changes: 6 additions & 6 deletions docs/converters/example1.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,32 @@ Next we create the SuspendResponseConverter:
```kotlin
if (typeData.typeInfo.type == User::class) {
return object : Converter.SuspendResponseConverter<HttpResponse, Any> {
override suspend fun convert(response: HttpResponse): Any {
override suspend fun convert(result: KtorfitResult): Any {
...
}
}
}

```
Inside of **convert** we get the HttpResponse and we want to return a User object.
Inside of **convert** we get the KtorfitResult 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.

```kotlin
override suspend fun convert(response: HttpResponse): Any {
val envelope = response.body<Envelope>()
override suspend fun convert(result: KtorfitResult): Any {
val envelope = result.response.body<Envelope>()
return envelope.user
}
```

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

```kotlin
override suspend fun convert(response: HttpResponse): Any {
override suspend fun convert(result: KtorfitResult): Any {
val typeData = TypeData.createTypeData("com.example.model.Envelope", typeInfo<Envelope>())
val envelope = ktorfit.nextSuspendResponseConverter(null, typeData)?.convert(response) as? Envelope
val envelope = ktorfit.nextSuspendResponseConverter(null, typeData)?.convert(result) as? Envelope
return envelope.user
}
```
Expand Down