add base network logic
This commit is contained in:
@@ -37,6 +37,8 @@ dependencies {
|
|||||||
implementation(libs.androidx.core.ktx)
|
implementation(libs.androidx.core.ktx)
|
||||||
implementation(libs.androidx.appcompat)
|
implementation(libs.androidx.appcompat)
|
||||||
implementation(libs.material)
|
implementation(libs.material)
|
||||||
|
implementation(libs.androidx.activity)
|
||||||
|
implementation(libs.androidx.constraintlayout)
|
||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
androidTestImplementation(libs.androidx.junit)
|
androidTestImplementation(libs.androidx.junit)
|
||||||
androidTestImplementation(libs.androidx.espresso.core)
|
androidTestImplementation(libs.androidx.espresso.core)
|
||||||
|
|||||||
@@ -33,8 +33,9 @@ android {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation(libs.androidx.core.ktx)
|
implementation(libs.androidx.core.ktx)
|
||||||
implementation(libs.androidx.appcompat)
|
implementation(libs.androidx.appcompat)
|
||||||
implementation(libs.material)
|
|
||||||
testImplementation(libs.junit)
|
implementation("com.squareup.okhttp3:okhttp:5.3.2")
|
||||||
androidTestImplementation(libs.androidx.junit)
|
implementation("com.squareup.okhttp3:logging-interceptor:5.3.2")
|
||||||
androidTestImplementation(libs.androidx.espresso.core)
|
implementation("com.squareup.retrofit2:retrofit:3.0.0")
|
||||||
|
implementation("com.squareup.retrofit2:converter-simplexml:3.0.0")
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package ru.fincode.tsudesk.core.network
|
||||||
|
|
||||||
|
object HttpClientProvider {
|
||||||
|
|
||||||
|
fun provide(): OkHttpClient =
|
||||||
|
OkHttpClient.Builder()
|
||||||
|
.connectTimeout(15, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(15, TimeUnit.SECONDS)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package ru.fincode.tsudesk.core.network
|
||||||
|
|
||||||
|
object NetworkConstants {
|
||||||
|
const val BASE_URL = "https://api.tsu.tula.ru/"
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package ru.fincode.tsudesk.core.network
|
||||||
|
|
||||||
|
sealed class NetworkError {
|
||||||
|
object NoInternet : NetworkError()
|
||||||
|
object Timeout : NetworkError()
|
||||||
|
data class Http(val code: Int) : NetworkError()
|
||||||
|
data class Unknown(val throwable: Throwable) : NetworkError()
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package ru.fincode.tsudesk.core.network
|
||||||
|
|
||||||
|
sealed class NetworkResult<out T> {
|
||||||
|
data class Success<T>(val data: T) : NetworkResult<T>()
|
||||||
|
data class Error(val error: NetworkError) : NetworkResult<Nothing>()
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package ru.fincode.tsudesk.core.network
|
||||||
|
|
||||||
|
object RetrofitProvider {
|
||||||
|
|
||||||
|
fun provide(baseUrl: String, client: OkHttpClient): Retrofit =
|
||||||
|
Retrofit.Builder()
|
||||||
|
.baseUrl(baseUrl)
|
||||||
|
.client(client)
|
||||||
|
.addConverterFactory(SimpleXmlConverterFactory.create())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package ru.fincode.tsudesk.core.network.di
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import retrofit2.Retrofit
|
||||||
|
import ru.fincode.tsudesk.core.network.HttpClientProvider
|
||||||
|
import ru.fincode.tsudesk.core.network.NetworkConstants
|
||||||
|
import ru.fincode.tsudesk.core.network.RetrofitProvider
|
||||||
|
|
||||||
|
@Module
|
||||||
|
@InstallIn(SingletonComponent::class)
|
||||||
|
object NetworkModule {
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideOkHttpClient(): OkHttpClient =
|
||||||
|
HttpClientProvider.provide()
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideRetrofit(client: OkHttpClient): Retrofit =
|
||||||
|
RetrofitProvider.provide(
|
||||||
|
baseUrl = NetworkConstants.BASE_URL,
|
||||||
|
client = client
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user