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

cn.lnkdoc.sdk.uia.instance.google.converter.UserInfoConverter.kt Maven / Gradle / Ivy

The newest version!
package cn.lnkdoc.sdk.uia.instance.google.converter

import cn.lnkdoc.sdk.uia.common.converter.IUiaConverter
import cn.lnkdoc.sdk.uia.common.exception.UiaException
import cn.lnkdoc.sdk.uia.instance.google.domain.UserInfo
import cn.lnkdoc.sdk.uia.instance.google.property.GoogleProperty
import cn.lnkdoc.sdk.uia.instance.google.request.UserInfoRequest
import com.alibaba.fastjson2.into
import io.vavr.Tuple2

/**
 * @author langkye
 * @since 1.0.0.RELEASE
 */
@Suppress("unused", "Unchecked_cast", "Duplicated_code_fragment")
open class UserInfoConverter : IUiaConverter {
    /**
     * convert body
     *
     * @param body body
     * @return response converted result
     */
    override fun  convertRequest(body: R): T {
        val tuple = body as Tuple2
        val accessTokenRequest = tuple._1

        val builder = okhttp3.FormBody.Builder()
        builder.addEncoded("access_token", accessTokenRequest.body())

        return builder.build() as T
    }

    /**
     * build headers
     *
     * @param body body
     * @param  type
     * @param  type
     * @return response converted result
     */
    override fun  buildHeaders(body: R): T {
        val builder = okhttp3.Headers.Builder()
        return builder
            .add("Accept", "application/json")
            .build() as T
    }

    /**
     * convert response
     *
     * @param body response
     * @return response converted result
     */
    override fun  convertResponse(body: R): T {
        @Suppress("Duplicated_code_fragment")
        val tuple = body as Tuple2
        // convert json
        @Suppress("Duplicated_code_fragment")
        val string = tuple._1
        
        val data = string.into()
        data.raw = string

        if (!data.success) {
            throw UiaException(data.message)
        }

        return data as T
    }
    /**
     * converter name
     *
     * @return converter name
     */
    override fun name(): String {
        return UserInfoRequest::class.java.getName()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy