https://kotlinlang.org logo
#ktor
Title
# ktor
m

Marc Knaup

06/23/2019, 2:35 PM
The client's
HttpResponsePipeline
should allow passing additional info - not just a
TypeInfo
. I'd like to pass the
KSerializer
which should be used for example since it's for a generic type. The feature or json serializer could then use the additional info to correctly deserialize the response.
e

e5l

06/24/2019, 6:34 AM
Hi @Marc Knaup, could you describe your use case? Maybe you can use
attributes
instead.
m

Marc Knaup

06/24/2019, 10:11 AM
Hey @e5l, I've written a small API client for Airtable. In Airtable, records always have the same structure:
Copy code
@Serializable
data class AirtableRecord<Fields : Any>(
	val createdTime: Instant,
	val fields: Fields,
	val id: String
)
Fields
depends on the actual table you're accessing. Like this for example:
Copy code
@Serializable
data class TimeTrackingFields(

	@SerialName("Description")
	val description: String,

	@SerialName("Employee ID")
	val employeeId: String,

	@SerialName("End Time")
	val endTime: Instant,

	@SerialName("Planday ID")
	val plandayId: String,

	@SerialName("Start Time")
	val startTime: Instant,

	@SerialName("Sync Time")
	val syncTime: Instant
)
So I can make the API calls generic and type-safe:
Copy code
suspend inline fun <reified Fields : Any> createRecord(tableName: String, fields: Fields): AirtableRecord<Fields> = …
suspend inline fun <reified Fields : Any> updateRecord(tableName: String, recordId: String, fields: Fields): AirtableRecord<Fields> = …
But in that case the API response (
AirtableRecord<Fields>
) would require a serializer for a generic type. With
TypeInfo
I'm not able to get the concrete type of
Fields
. I only get the
TypeVariable
. My solution for now is to avoid
JsonFeature
and do the parsing & serializing directly in my client.
4 Views