Skip to content

Commit

Permalink
activity : P1C4
Browse files Browse the repository at this point in the history
  • Loading branch information
JustJerem committed Feb 19, 2024
1 parent 6fc119c commit d73e00f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activity/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".SnowForecastApp"
android:allowBackup="true"
Expand Down
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>
}
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()
}
}

0 comments on commit d73e00f

Please sign in to comment.