parent
56658b6b73
commit
1fdca22aa4
@ -0,0 +1,15 @@
|
||||
package fr.iut.cinecool.API
|
||||
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
|
||||
object ApiClient {
|
||||
private const val BASE_URL = "https://api.themoviedb.org/3/"
|
||||
|
||||
private val retrofit: Retrofit = Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
|
||||
val apiService: ApiService = retrofit.create(ApiService::class.java)
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package fr.iut.cinecool.API
|
||||
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Query
|
||||
|
||||
interface ApiService {
|
||||
|
||||
@GET("movie/popular")
|
||||
suspend fun getPopularMovies(
|
||||
@Query("api_key") apiKey: String,
|
||||
@Query("page") page: Int
|
||||
): MovieResponse
|
||||
|
||||
@GET("search/movie")
|
||||
suspend fun searchMovies(
|
||||
@Query("api_key") apiKey: String,
|
||||
@Query("query") query: String,
|
||||
@Query("page") page: Int
|
||||
): MovieResponse
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package fr.iut.cinecool.API
|
||||
|
||||
data class MovieResponse(
|
||||
val page: Int,
|
||||
val results: List<Movie>,
|
||||
val total_pages: Int,
|
||||
val total_results: Int
|
||||
)
|
||||
|
||||
data class Movie(
|
||||
val id: Int,
|
||||
val title: String,
|
||||
val poster_path: String?,
|
||||
val overview: String
|
||||
)
|
@ -0,0 +1,13 @@
|
||||
package fr.iut.cinecool.API
|
||||
|
||||
class Repository {
|
||||
private val apiService = ApiClient.apiService
|
||||
|
||||
suspend fun getPopularMovies(apiKey: String, page: Int): MovieResponse {
|
||||
return apiService.getPopularMovies(apiKey, page)
|
||||
}
|
||||
|
||||
suspend fun searchMovies(apiKey: String, query: String, page: Int): MovieResponse {
|
||||
return apiService.searchMovies(apiKey, query, page)
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package fr.iut.cinecool
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.launch
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import fr.iut.cinecool.API.Movie
|
||||
import fr.iut.cinecool.API.Repository
|
||||
|
||||
class MovieViewModel : ViewModel() {
|
||||
private val repository = Repository()
|
||||
|
||||
val popularMovies = MutableLiveData<List<Movie>>()
|
||||
val searchResults = MutableLiveData<List<Movie>>()
|
||||
|
||||
fun getPopularMovies(apiKey: String, page: Int) {
|
||||
viewModelScope.launch {
|
||||
val movies = repository.getPopularMovies(apiKey, page)
|
||||
popularMovies.postValue(movies.results)
|
||||
}
|
||||
}
|
||||
|
||||
fun searchMovies(apiKey: String, query: String, page: Int) {
|
||||
viewModelScope.launch {
|
||||
val movies = repository.searchMovies(apiKey, query, page)
|
||||
searchResults.postValue(movies.results)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package fr.iut.cinecool
|
||||
|
||||
import fr.iut.cinecool.services.TheMovieDbApiClient
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
object RetrofitClient {
|
||||
private const val BASE_URL = "https://api.themoviedb.org/3/"
|
||||
|
||||
private val okHttpClient = OkHttpClient.Builder()
|
||||
.connectTimeout(60, TimeUnit.SECONDS)
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.writeTimeout(60, TimeUnit.SECONDS)
|
||||
.build()
|
||||
|
||||
private val retrofit = Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.client(okHttpClient)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
|
||||
val api: TheMovieDbApiClient = retrofit.create(TheMovieDbApiClient::class.java)
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package fr.iut.cinecool.interfaces
|
||||
|
||||
interface ITheMovieDbApiCallback<T> {
|
||||
fun onSuccess(result: T)
|
||||
fun onError(error: Throwable)
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package fr.iut.cinecool.interfaces
|
||||
|
||||
import fr.iut.cinecool.model.Movie
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Path
|
||||
import retrofit2.http.Query
|
||||
|
||||
interface ITheMovieDbService {
|
||||
@GET("movie/{movieId}")
|
||||
fun getMovieDetails(@Path("movieId") movieId: Int, @Query("api_key") apiKey: String): Call<Movie>
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package fr.iut.cinecool.services
|
||||
|
||||
import fr.iut.cinecool.interfaces.ITheMovieDbApiCallback
|
||||
import fr.iut.cinecool.interfaces.ITheMovieDbService
|
||||
import fr.iut.cinecool.model.MovieAPI
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
|
||||
class TheMovieDbApiClient(apiKey: String) {
|
||||
private val service: ITheMovieDbService = Retrofit.Builder()
|
||||
.baseUrl("https://api.themoviedb.org/3/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
.create(ITheMovieDbService::class.java)
|
||||
|
||||
private val apiKeyInterceptor = Interceptor { chain ->
|
||||
val url = chain.request().url.newBuilder()
|
||||
.addQueryParameter("tmdb_api_key", apiKey)
|
||||
.build()
|
||||
|
||||
val request = chain.request().newBuilder()
|
||||
.url(url)
|
||||
.build()
|
||||
|
||||
chain.proceed(request)
|
||||
}
|
||||
|
||||
private val client: OkHttpClient = OkHttpClient.Builder()
|
||||
.addInterceptor(apiKeyInterceptor)
|
||||
.build()
|
||||
|
||||
/*fun getMovieDetails(movieId: Int, callback: ITheMovieDbApiCallback<MovieAPI>) {
|
||||
service.getMovieDetails(movieId).enqueue(object : Callback<MovieAPI> {
|
||||
override fun onResponse(call: Call<MovieAPI>, response: Response<MovieAPI>) {
|
||||
if (response.isSuccessful) {
|
||||
val result = response.body()
|
||||
if (result != null) {
|
||||
callback.onSuccess(result)
|
||||
} else {
|
||||
callback.onError(Throwable("Empty response body"))
|
||||
}
|
||||
} else {
|
||||
callback.onError(Throwable("Error ${response.code()}: ${response.message()}"))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<MovieAPI>, t: Throwable) {
|
||||
callback.onError(t)
|
||||
}
|
||||
})
|
||||
}*/
|
||||
}
|
Loading…
Reference in new issue