-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
activity/app/src/main/java/com/openclassrooms/icerush/data/network/WeatherClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.openclassrooms.icerush.data.network | ||
|
||
import com.openclassrooms.icerush.data.response.OpenWeatherForecastsResponse | ||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
import retrofit2.http.Query | ||
|
||
|
||
interface WeatherClient { | ||
@GET("/data/2.5/forecast") | ||
suspend fun getWeatherByPosition( | ||
@Query(value = "lat") latitude: Double, | ||
@Query(value = "lon") longitude: Double, | ||
@Query(value = "appid") apiKey: String | ||
): Response<OpenWeatherForecastsResponse> | ||
} |
52 changes: 52 additions & 0 deletions
52
activity/app/src/main/java/com/openclassrooms/icerush/di/NetworkModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.openclassrooms.icerush.di | ||
|
||
import com.openclassrooms.icerush.data.network.WeatherClient | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.moshi.MoshiConverterFactory | ||
import javax.inject.Singleton | ||
|
||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { | ||
|
||
|
||
// Provides a singleton instance of Retrofit for network communication | ||
@Singleton | ||
@Provides | ||
fun provideRetrofit(): Retrofit { | ||
return Retrofit.Builder() | ||
.baseUrl("https://api.openweathermap.org") | ||
.addConverterFactory( | ||
MoshiConverterFactory.create( | ||
Moshi.Builder().add(KotlinJsonAdapterFactory()).build() | ||
) | ||
) | ||
.client(provideOkHttpClient()) // Uses a separate function for OkHttpClient configuration | ||
.build() | ||
} | ||
|
||
|
||
// Provides a singleton instance of WeatherClient using Retrofit | ||
@Singleton | ||
@Provides | ||
fun provideWeatherClient(retrofit: Retrofit): WeatherClient { | ||
return retrofit.create(WeatherClient::class.java) | ||
} | ||
|
||
|
||
// Private function to configure OkHttpClient with an interceptor for logging | ||
private fun provideOkHttpClient(): OkHttpClient { | ||
return OkHttpClient.Builder().apply { | ||
addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) | ||
}.build() | ||
} | ||
} |