package solutions.poole.questlog.api.v0.models import com.fasterxml.jackson.core.type.TypeReference import com.jakewharton.retrofit2.adapter.rxjava2.HttpException import io.apibuilder.app.ApiNetworkCallResponse import io.apibuilder.app.CommonNetworkErrors import io.apibuilder.app.CommonNetworkErrors.UnknownNetworkError import io.apibuilder.app.EitherCallOrCommonNetworkError import io.apibuilder.app.EitherCallOrCommonNetworkError.CallError import io.apibuilder.app.EitherCallOrCommonNetworkError.CommonNetworkError import io.reactivex.Single import java.util.UUID import kotlin.String import kotlin.Throwable import kotlin.collections.List import retrofit2.Response import retrofit2.http.GET import retrofit2.http.POST import retrofit2.http.Path import retrofit2.http.Query /** * This code was generated by [models.generator.kotlin.KotlinGenerator] */ interface AdventuresClient { @POST(value="adventures") fun postAdventures(@Query(value="adventure") adventure: Adventure): Single> @GET(value="adventures") fun getAdventures(@Query(value="guid") guid: UUID?, @Query(value="name") name: String?): Single>> @GET(value="adventures/{guid}") fun getAdventuresGuid(@Path(value="guid") guid: UUID): Single> object PostAdventuresCall { val toError: (Throwable) -> EitherCallOrCommonNetworkError = { t -> when (t) { is HttpException -> { val body: String? = t.response().errorBody()?.string() when (t.code()) { 409 -> body?.let { CallError(ErrorResponses.Error409(solutions.poole.questlog.api.v0.models.JacksonObjectMapperFactory.create().readValue(body, ErrorListTypeReference ))) } ?: CommonNetworkError(UnknownNetworkError(409, "No Body")) else -> CommonNetworkError(CommonNetworkErrors.processCommonNetworkError(t)) } } else -> CommonNetworkError(CommonNetworkErrors.processCommonNetworkError(t)) } } fun getCallAndErrorLambda(client: AdventuresClient, adventure: Adventure) = ApiNetworkCallResponse( client.postAdventures(adventure) .map{ response -> if(response.isSuccessful) response.body()?.let { body -> Pair(body, response.code())} else throw HttpException(response)} .map{it} , toError) object ErrorListTypeReference : TypeReference>() sealed class ErrorResponses { data class Error409( val data: List ) : ErrorResponses() } } }