Skip to content

Commit 0a65c48

Browse files
committed
feat: Implement user data fetching with Retrofit
This commit introduces the ability to fetch user data from a remote API using Retrofit. It includes setting up the network layer, defining data classes, and creating a repository to handle the data retrieval. Changes: - Created `UserApi` interface to define the API endpoint for fetching users using `@GET("/users")`. - Created `RetrofitInstance` to provide a configured Retrofit instance and `UserApi`. It includes: - A `provideRetrofit` function that configures Retrofit with a base URL, an `OkHttpClient` with a logging interceptor, and a `GsonConverterFactory`. - A `provideUserApi` function that creates and returns an instance of the `UserApi`. - Added `UserRepository` to manage fetching user data. It uses a flow to emit results and handles network errors. - Added `User`, `Address`, `Geo`, and `Company` data classes to represent the user data structure. - `UserRepository` now handles API responses, emits either a list of users or an error Result, and shifts network operations to `Dispatchers.IO` using `flowOn`. - Added dependencies `okhttp3`, `logging-interceptor`, `retrofit2`, `converter-gson`
1 parent 77003df commit 0a65c48

4 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.techexactly.model.dataclass
2+
3+
data class User(
4+
val id: Long,
5+
val name: String,
6+
val username: String,
7+
val email: String,
8+
val address: Address,
9+
val phone: String,
10+
val website: String,
11+
val company: Company,
12+
)
13+
14+
data class Address(
15+
val street: String,
16+
val suite: String,
17+
val city: String,
18+
val zipcode: String,
19+
val geo: Geo,
20+
)
21+
22+
data class Geo(
23+
val lat: String,
24+
val lng: String,
25+
)
26+
27+
data class Company(
28+
val name: String,
29+
val catchPhrase: String,
30+
val bs: String,
31+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.techexactly.model.network
2+
3+
import com.google.gson.GsonBuilder
4+
import okhttp3.OkHttpClient
5+
import okhttp3.logging.HttpLoggingInterceptor
6+
import retrofit2.Retrofit
7+
import retrofit2.converter.gson.GsonConverterFactory
8+
9+
private const val BASE_URL = "https://jsonplaceholder.typicode.com/"
10+
11+
fun provideRetrofit(): Retrofit {
12+
val loggingInterceptor = HttpLoggingInterceptor().apply {
13+
level = HttpLoggingInterceptor.Level.BODY
14+
}
15+
val okHttpClient = OkHttpClient.Builder().addInterceptor(loggingInterceptor).build()
16+
val gson = GsonBuilder().setLenient().create()
17+
18+
return Retrofit.Builder()
19+
.baseUrl(BASE_URL)
20+
.client(okHttpClient)
21+
.addConverterFactory(GsonConverterFactory.create(gson))
22+
.build()
23+
}
24+
25+
fun provideUserApi(retrofit: Retrofit): UserApi {
26+
return retrofit.create(UserApi::class.java)
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.techexactly.model.network
2+
3+
import com.example.techexactly.model.dataclass.User
4+
import retrofit2.Response
5+
import retrofit2.http.GET
6+
7+
interface UserApi {
8+
@GET("/users")
9+
suspend fun getUsers(): Response<List<User>>
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.techexactly.model.repository
2+
3+
import com.example.techexactly.model.dataclass.User
4+
import com.example.techexactly.model.network.UserApi
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.flow.Flow
7+
import kotlinx.coroutines.flow.flow
8+
import kotlinx.coroutines.flow.flowOn
9+
10+
class UserRepository(private val userApi: UserApi) {
11+
fun getUsers(): Flow<Result<List<User>>> = flow {
12+
try {
13+
// Switch to Dispatchers.IO for the network call.
14+
val response = userApi.getUsers()
15+
if (response.isSuccessful) {
16+
// Handle success case.
17+
val users = response.body() ?: emptyList()
18+
emit(Result.success(users))
19+
} else {
20+
// Handle HTTP error cases.
21+
emit(Result.failure(Exception("Error: ${response.code()} ${response.message()}")))
22+
}
23+
} catch (e: Exception) {
24+
// Handle any exception during the network call.
25+
emit(Result.failure(Exception("Error fetching users: ${e.message}")))
26+
}
27+
}.flowOn(Dispatchers.IO) // Use flowOn to change the context of the flow to Dispatchers.IO
28+
}

0 commit comments

Comments
 (0)