how can I add base url and api_key header correcty in koin module retrofit correctly?

Viewed 329

I am developing new android app where I want to add baseurl and api_key in koin module retrofit but I am confused

Ending Point: https://api-aws-eu-qa-1.auto1-test.com/v1/car-types/manufacturer?wa_key=coding-puzzle-client-449cc9d&page=0&pageSize=15

API Key: coding-puzzle-client-449cc9d

Base Url: https://api-aws-eu-qa-1.auto1-test.com/

I want to add base_url and api_key correctly in my modules.kt file correctly so that later I can fetch data correctly from server if you check my modules.kt first I am calling api_key then base url. and setup my interface where I am call get method in interface following way

interface ApiInterface {

    @GET("v1/car-types/manufacturer?")
    suspend  fun getCarResponse(): Call<CarManufactureResponse>


}

below CarManifacturerResponse

data class CarManufactureResponse(
    @SerializedName("page")
    val page: Int,
    @SerializedName("pageSize")
    val pageSize: Int,
    @SerializedName("totalPageCount")
    val totalPageCount: Int,
    @SerializedName("mkda")
    val mkda: ManufacturerId
)

below ManifacturerId

data class ManufacturerId(
    @SerializedName("020")
    val x020: String,
    @SerializedName("040")
    val x040: String,
    @SerializedName("042")
    val x042: String,
    @SerializedName("043")
    val x043: String,
    @SerializedName("057")
    val x057: String,
    @SerializedName("060")
    val x060: String,
    @SerializedName("095")
    val x095: String,
    @SerializedName("107")
    val x107: String,
    @SerializedName("125")
    val x125: String,
    @SerializedName("130")
    val x130: String,
    @SerializedName("141")
    val x141: String,
    @SerializedName("145")
    val x145: String,
    @SerializedName("150")
    val x150: String,
    @SerializedName("157")
    val x157: String,
    @SerializedName("160")
    val x160: String
)

below my Modules.kt koin setup module where I want to pass baseurl and api_key

val viewModels = module {
    //viewModel { CarViewModel(get()) }
}




    val apiModule = module {
        single {
    
            val tokenInterceptor = Interceptor { chain ->
                val request =
                    chain
                        .request()
                        .newBuilder()
                        .addHeader(
                            "API_KEY",Constants.API_KEY
                        )
                        .build()
    
                chain.proceed(request)
            }
    
            val logInterceptor: HttpLoggingInterceptor = HttpLoggingInterceptor().apply {
                this.level = HttpLoggingInterceptor.Level.BODY
            }
    
            val okHttpClient =
                OkHttpClient.Builder()
                    .addInterceptor(tokenInterceptor)
                    .addInterceptor(logInterceptor)
                    .build()
    
            val retrofit =
                Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(Constants.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(CoroutineCallAdapterFactory())
                    .build()
    
            retrofit.create(ApiInterface::class.java)
        }
    }

below Constants.kt

object Constants {

const val API_KEY = "wa_key=coding-puzzle-client-449cc9d"
const val BASE_URL = "https://api-aws-eu-qa-1.auto1-test.com/"

}

how can I pass correctly baseurl and apiKey in koin module so that I can implement retrofit logic correctly

1 Answers

Use the code below to get your desired url:

@GET("v1/car-types/manufacturer?{apiKey}")
suspend  fun getCarResponse(  @Path("apiKey") type: String,@Query("page") page:String,@Query("pageSize") pageSize:String): Call<CarManufactureResponse>
Related