parent
77452aff27
commit
745ecb427c
@ -0,0 +1,66 @@
|
|||||||
|
package fr.iut.cinecool
|
||||||
|
|
||||||
|
import retrofit2.Call
|
||||||
|
import com.google.android.gms.common.api.Response
|
||||||
|
import com.google.gson.GsonBuilder
|
||||||
|
import fr.iut.cinecool.interfaces.ApiService
|
||||||
|
import fr.iut.cinecool.model.MovieResponse
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
|
import retrofit2.Retrofit
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
|
import javax.security.auth.callback.Callback
|
||||||
|
|
||||||
|
class ApiClient {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
private const val BASE_URL = "https://api.themoviedb.org/3/"
|
||||||
|
|
||||||
|
fun create(): ApiService {
|
||||||
|
|
||||||
|
val gson = GsonBuilder()
|
||||||
|
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
|
||||||
|
.create()
|
||||||
|
|
||||||
|
val logger = HttpLoggingInterceptor()
|
||||||
|
logger.level = HttpLoggingInterceptor.Level.BASIC
|
||||||
|
|
||||||
|
val client = OkHttpClient.Builder()
|
||||||
|
.addInterceptor(logger)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val retrofit = Retrofit.Builder()
|
||||||
|
.baseUrl(BASE_URL)
|
||||||
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
||||||
|
.client(client)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
return retrofit.create(ApiService::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
|
val retrofit = Retrofit.Builder()
|
||||||
|
.baseUrl("https://api.themoviedb.org/3/")
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val movieApiService = retrofit.create(ApiService::class.java)
|
||||||
|
|
||||||
|
val apiKey = "a97243d7813d31446f6c43284e6854d5"
|
||||||
|
val call = movieApiService.getPopularMovies(apiKey)
|
||||||
|
|
||||||
|
// TO DO
|
||||||
|
/*call.enqueue(object : Callback<MovieResponse> {
|
||||||
|
override fun onResponse(call: Call<MovieResponse>, response: Response<MovieResponse>) {
|
||||||
|
// Code à exécuter lorsque la réponse est reçue
|
||||||
|
val movies = response.body()?.results
|
||||||
|
// Traitez les films renvoyés ici
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailure(call: Call<MovieResponse>, t: Throwable) {
|
||||||
|
// Code à exécuter en cas d'échec de la requête
|
||||||
|
}
|
||||||
|
})*/
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package fr.iut.cinecool.interfaces
|
||||||
|
|
||||||
|
import fr.iut.cinecool.model.Movie
|
||||||
|
import fr.iut.cinecool.model.MovieResponse
|
||||||
|
import retrofit2.Call
|
||||||
|
import retrofit2.http.GET
|
||||||
|
import retrofit2.http.Query
|
||||||
|
|
||||||
|
interface ApiService {
|
||||||
|
|
||||||
|
@GET("movie/popular")
|
||||||
|
fun getPopularMovies(@Query("api_key") apiKey: String): Call<MovieResponse>
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,14 @@
|
|||||||
package fr.iut.cinecool.model
|
package fr.iut.cinecool.model
|
||||||
|
|
||||||
import android.graphics.drawable.Drawable
|
import android.graphics.drawable.Drawable
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
data class Movie(val id:Int, val name:String, var mark:Int, val realisator:String, var duration: Double, val icon:Int)
|
data class Movie(val id:Int, val name:String, var mark:Int, val realisator:String, var duration: Double, val icon:Int)
|
||||||
|
|
||||||
|
// D'après l'API (TheMovieDB)
|
||||||
|
data class MovieAPI(
|
||||||
|
@SerializedName("id") val id: Int,
|
||||||
|
@SerializedName("title") val title: String,
|
||||||
|
@SerializedName("overview") val overview: String,
|
||||||
|
@SerializedName("poster_path") val posterPath: String?,
|
||||||
|
@SerializedName("vote_average") val voteAverage: Float
|
||||||
|
)
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut.cinecool.model
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
// MovieResponse pour stocker la réponse JSON de l'API :
|
||||||
|
data class MovieResponse(
|
||||||
|
@SerializedName("results") val movies: List<Movie>
|
||||||
|
)
|
Loading…
Reference in new issue