package io.apibuilder.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.Long import kotlin.String import kotlin.Throwable import kotlin.Unit import kotlin.collections.List import retrofit2.Response import retrofit2.http.Body import retrofit2.http.DELETE 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 AttributesClient { /** * Search all attributes. Results are always paginated. */ @GET(value="attributes") fun getAttributes( @Query(value="guid") guid: UUID?, @Query(value="name") name: String?, @Query(value="limit") limit: Long, @Query(value="offset") offset: Long ): Single>> /** * Returns the attribute with this name. */ @GET(value="attributes/{name}") fun getAttributesName(@Path(value="name") name: String): Single> /** * Create a new attribute. */ @POST(value="attributes") fun postAttributes(@Body attributeForm: AttributeForm): Single> /** * Deletes the attribute with this name. Only the user who created an attribute can delete it. */ @DELETE(value="attributes/{name}") fun deleteAttributesName(@Path(value="name") name: String): Single> object GetAttributesNameCall { val toError: (Throwable) -> EitherCallOrCommonNetworkError = { t -> when (t) { is HttpException -> { val body: String? = t.response().errorBody()?.string() when (t.code()) { 404 -> CallError (ErrorResponses.Error404) else -> CommonNetworkError(CommonNetworkErrors.processCommonNetworkError(t)) } } else -> CommonNetworkError(CommonNetworkErrors.processCommonNetworkError(t)) } } fun getCallAndErrorLambda(client: AttributesClient, name: String) = ApiNetworkCallResponse( client.getAttributesName(name) .map{ response -> if(response.isSuccessful) response.body()?.let { body -> Pair(body, response.code())} else throw HttpException(response)} .map{it} , toError) /** * Error Responses for getAttributesName - Returns the attribute with this name. */ sealed class ErrorResponses { object Error404 : ErrorResponses() } } object PostAttributesCall { val toError: (Throwable) -> EitherCallOrCommonNetworkError = { t -> when (t) { is HttpException -> { val body: String? = t.response().errorBody()?.string() when (t.code()) { 401 -> CallError (ErrorResponses.Error401) 409 -> body?.let { CallError(ErrorResponses.Error409(io.apibuilder.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: AttributesClient, attributeForm: AttributeForm) = ApiNetworkCallResponse( client.postAttributes(attributeForm) .map{ response -> if(response.isSuccessful) response.body()?.let { body -> Pair(body, response.code())} else throw HttpException(response)} .map{it} , toError) object ErrorListTypeReference : TypeReference>() /** * Error Responses for postAttributes - Create a new attribute. */ sealed class ErrorResponses { object Error401 : ErrorResponses() data class Error409( val data: List ) : ErrorResponses() } } object DeleteAttributesNameCall { val toError: (Throwable) -> EitherCallOrCommonNetworkError = { t -> when (t) { is HttpException -> { val body: String? = t.response().errorBody()?.string() when (t.code()) { 401 -> CallError (ErrorResponses.Error401) 404 -> CallError (ErrorResponses.Error404) else -> CommonNetworkError(CommonNetworkErrors.processCommonNetworkError(t)) } } else -> CommonNetworkError(CommonNetworkErrors.processCommonNetworkError(t)) } } fun getCallAndErrorLambda(client: AttributesClient, name: String) = ApiNetworkCallResponse( client.deleteAttributesName(name) .map{ response -> if(response.isSuccessful) Pair(Unit, response.code()) else throw HttpException(response)} .map{it} , toError) /** * Error Responses for deleteAttributesName - Deletes the attribute with this name. Only the * user who created an attribute can delete it. */ sealed class ErrorResponses { object Error401 : ErrorResponses() object Error404 : ErrorResponses() } } }