All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avito.filestorage.ToStringConverterFactory.kt Maven / Gradle / Ivy

package com.avito.filestorage

import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type

internal class ToStringConverterFactory : Converter.Factory() {

    private val uriRegex = Regex("uri\":.*\"(.+)\"")
    private val mediaType = "text/html".toMediaType()

    override fun responseBodyConverter(
        type: Type,
        annotations: Array,
        retrofit: Retrofit
    ): Converter? {
        return if (String::class.java == type) {
            Converter { value -> uriRegex.find(value.string())?.groupValues?.getOrNull(1) }
        } else {
            null
        }
    }

    override fun requestBodyConverter(
        type: Type,
        parameterAnnotations: Array,
        methodAnnotations: Array,
        retrofit: Retrofit
    ): Converter<*, RequestBody>? {
        return if (String::class.java == type) {
            Converter { value -> value.toRequestBody(mediaType) }
        } else {
            null
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy