ApiService.kt 2.32 KB
Newer Older
1
package com.example.ayudapy.api
2

3 4 5
import com.example.ayudapy.model.CentroAyuda
import com.example.ayudapy.model.PedidosAyuda
import com.example.ayudapy.model.Resultado
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
import java.util.concurrent.TimeUnit


interface ApiService {

    @GET("?in_bbox")
    fun getListaCentro(
        @Query("limit") limit : Int,
        @Query("offset") offset : Int
    ): Call<CentroAyuda>


  /*  @GET("helprequestsgeo/")
    fun getHelpRequests(@Query("in_bbox") boundaryBox: String): Call<CentroAyuda>*/

    @GET("?in_bbox")
    fun getListaPedidos(
        @Query("limit") limit : Int,
        @Query("offset") offset : Int
    ): Call<PedidosAyuda>

    @GET("{id}")
    fun getPedidoDetalle(
        @Path("id") id : Int
    ): Call<Resultado>


   companion object {
        // init Retrofit base server instance
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        val redditClient by lazy {
            invoke(
                "https://ayudapy.org/api/v1/donationcenters/"
            )
        }
        val stackClient by lazy {
            invoke(
                "https://ayudapy.org/api/v1/helprequestsgeo/"
            )
        }
        val pedidoDetalle by lazy {
            invoke(
                "https://ayudapy.org/api/v1/helprequests/"
            )
        }
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

        private val loggingInterceptor = HttpLoggingInterceptor().apply {
            this.level = HttpLoggingInterceptor.Level.BODY
        }

        operator fun invoke(baseUrl: String): ApiService {
            val client = OkHttpClient.Builder().apply {
                /**addNetworkInterceptor(StethoInterceptor()) */
                addNetworkInterceptor(loggingInterceptor)
                connectTimeout(10, TimeUnit.MINUTES)
                readTimeout(10, TimeUnit.MINUTES)
                writeTimeout(10, TimeUnit.MINUTES)
            }.build()

            return Retrofit.Builder()
                .client(client)
                .baseUrl(baseUrl)
//                .addCallAdapterFactory(CoroutineCallAdapterFactory())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ApiService::class.java)
        }
    }



}