ApiService.kt 2.07 KB
Newer Older
1 2 3 4 5 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
package com.example.ayudapy

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
        val redditClient by lazy { ApiService.invoke("https://ayudapy.org/api/v1/donationcenters/") }
        val stackClient by lazy { ApiService.invoke("https://ayudapy.org/api/v1/helprequestsgeo/") }
        val pedidoDetalle by lazy { ApiService.invoke("https://ayudapy.org/api/v1/helprequests/") }

        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)
        }
    }



}