Skip to content

Commit

Permalink
Merge pull request #2 from MoviebaseApp/release/0.3.0
Browse files Browse the repository at this point in the history
release/0.3.0
  • Loading branch information
ChrisKruegerDev authored Apr 6, 2021
2 parents f8fea18 + 918c76c commit 2e6f08a
Show file tree
Hide file tree
Showing 24 changed files with 488 additions and 85 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,16 @@ val showPageResult = tmdb.search.findShows(
You can build an image URL via the poster file path and size key. More information on the [TMDb images site](https://developers.themoviedb.org/3/getting-started/images).

```kotlin
val url = TmdbImageUrlBuilder.build("w154", "nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg")
val url = TmdbImageUrlBuilder.build( "nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg", "w154")
```

Or create the URL by an image class and the best matched width/height.

```kotlin
val url = TmdbImageUrlBuilder.build(image = movie.posterImage, width = 200,height = 300)
val url = TmdbImageUrlBuilder.build(image = movie.posterImage, width = 200, height = 300)
```

<br/>
---

*This library uses the TMDb but is not endorsed or certified by TMDb. These services are licensed under [CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0).*
8 changes: 6 additions & 2 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,19 @@ object Libs {
val glideRecyclerView = "com.github.bumptech.glide:recyclerview-integration:4.12.0"
}

object Util {
val kodein = "org.kodein.di:kodein-di:${Versions.kodein}"
object Data {
val tmdbApi = "app.moviebase:tmdb-api:0.2.0"

val ktorCore = "io.ktor:ktor-client-core:${Versions.ktor}"
val ktorJson = "io.ktor:ktor-client-json:${Versions.ktor}"
val ktorLogging = "io.ktor:ktor-client-logging:${Versions.ktor}"
val ktorSerialization = "io.ktor:ktor-client-serialization:${Versions.ktor}"
val ktorAndroid = "io.ktor:ktor-client-android:${Versions.ktor}"
val ktorIos = "io.ktor:ktor-client-ios:${Versions.ktor}"
}

object Util {
val kodein = "org.kodein.di:kodein-di:${Versions.kodein}"
val timber = "com.jakewharton.timber:timber:4.7.1"
val dagger = "com.google.dagger:dagger-android-support:${Versions.dagger}"
val daggerProcessor = "com.google.dagger:dagger-android-processor:${Versions.dagger}"
Expand Down
12 changes: 6 additions & 6 deletions tmdb-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ kotlin {
implementation(Libs.Kotlin.kotlinxDateTime)
api(Libs.Kotlin.kotlinIo)

implementation(Libs.Util.ktorCore)
implementation(Libs.Util.ktorJson)
implementation(Libs.Util.ktorLogging)
implementation(Libs.Util.ktorSerialization)
implementation(Libs.Data.ktorCore)
implementation(Libs.Data.ktorJson)
implementation(Libs.Data.ktorLogging)
implementation(Libs.Data.ktorSerialization)
}
}
val commonTest by getting {
Expand All @@ -69,7 +69,7 @@ kotlin {
}
val androidMain by getting {
dependencies {
implementation(Libs.Util.ktorAndroid)
implementation(Libs.Data.ktorAndroid)
}
}
val androidTest by getting {
Expand All @@ -96,7 +96,7 @@ kotlin {
kotlin.srcDir("src/iosMain/kotlin")

dependencies {
implementation(Libs.Util.ktorIos)
implementation(Libs.Data.ktorIos)
}
}
val iosArm64Main by getting {
Expand Down
12 changes: 11 additions & 1 deletion tmdb-api/src/commonMain/kotlin/app/moviebase/tmdb/Tmdb4.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ import io.ktor.client.request.*

class Tmdb4(tmdbApiKey: String) {

var authenticationToken: String? = null

private val client = buildHttpClient {
it.header(TmdbUrlParameter.API_KEY, tmdbApiKey)
it.header(TmdbUrlParameter.ACCESS_TOKEN, tmdbApiKey)
}

private val authClient = buildHttpClient {
it.header(TmdbUrlParameter.API_KEY, tmdbApiKey)
it.header(TmdbUrlParameter.ACCESS_TOKEN, tmdbApiKey)
it.header("Authorization", "Bearer $authenticationToken")

}

val account = Tmdb4AccountApi(client)
val auth = Tmdb4AuthenticationApi(client)
val auth = Tmdb4AuthenticationApi(authClient)
val list = Tmdb4ListApi(client)

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ object TmdbWebConfig {
const val BASE_URL_TMDB = "https://www.themoviedb.org"
const val VERSION_PATH_V3 = "3"
const val VERSION_PATH_V4 = "4"
const val BASE_URL_TMDB_IMAGE = "http://image.tmdb.org/t/p/"
const val BASE_URL_YOUTUBE_IMAGE = "http://img.youtube.com/vi"
const val BASE_URL_TMDB_IMAGE = "https://image.tmdb.org/t/p/"
const val BASE_URL_YOUTUBE_IMAGE = "https://img.youtube.com/vi"
const val LOGO_FILTER = "_filter(negate,000,666)"

}

object TmdbUrlParameter {
const val API_KEY = "api_key"
const val ACCESS_TOKEN = "access_token"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import io.ktor.client.request.*

class Tmdb4AccountApi(private val client: HttpClient) {

/**
* Get all of the lists you've created.
*/
suspend fun getLists(accountId: String, page: Int): TmdbPageResult<Tmdb4ListMeta> = client.get {
endPointAccount(accountId, "lists")
parameterPage(page)
}

/**
* Get the list of movies you have marked as a favorite.
*
Expand All @@ -30,21 +38,76 @@ class Tmdb4AccountApi(private val client: HttpClient) {
sortOrder: TmdbSortOrder = TmdbSortOrder.DESC,
): TmdbPageResult<TmdbShow> = getFavorites(TmdbRequestMediaType.TV, accountId, page, sortBy, sortOrder)

suspend fun <T : TmdbAnyMedia> getFavorites(
suspend fun <T : TmdbMediaListItem> getFavorites(
mediaType: TmdbRequestMediaType,
accountId: String,
page: Int,
sortBy: TmdbListSortBy? = null,
sortOrder: TmdbSortOrder = TmdbSortOrder.DESC,
): TmdbPageResult<T> = client.get {
endPointAccount(accountId, mediaType.value, "favorites")
sortBy?.let { parameterSortBy(it, sortOrder) }
parameterPage(page)
}

suspend fun getMovieRecommendation(
accountId: String,
page: Int,
sortBy: TmdbListSortBy? = null,
sortOrder: TmdbSortOrder = TmdbSortOrder.DESC,
): TmdbPageResult<TmdbMovie> = getRecommendations(TmdbRequestMediaType.MOVIE, accountId, page, sortBy, sortOrder)

suspend fun getShowRecommendation(
accountId: String,
page: Int,
sortBy: TmdbListSortBy? = null,
sortOrder: TmdbSortOrder = TmdbSortOrder.DESC,
): TmdbPageResult<TmdbShow> = getRecommendations(TmdbRequestMediaType.TV, accountId, page, sortBy, sortOrder)

suspend fun <T : TmdbMediaListItem> getRecommendations(
mediaType: TmdbRequestMediaType,
accountId: String,
page: Int,
sortBy: TmdbListSortBy? = null,
sortOrder: TmdbSortOrder = TmdbSortOrder.DESC,
): TmdbPageResult<T> = client.get {
endPointV4("account", accountId, mediaType.value, "favorites")
endPointAccount(accountId, mediaType.value, "recommendations")
sortBy?.let { parameterSortBy(it, sortOrder) }
parameterPage(page)
}

suspend fun getMovieWatchlist(
accountId: String,
page: Int,
sortBy: TmdbListSortBy? = null,
sortOrder: TmdbSortOrder = TmdbSortOrder.DESC,
): TmdbPageResult<TmdbMovie> = getWatchlist(TmdbRequestMediaType.MOVIE, accountId, page, sortBy, sortOrder)

suspend fun getShowWatchlist(
accountId: String,
page: Int,
sortBy: TmdbListSortBy? = null,
sortOrder: TmdbSortOrder = TmdbSortOrder.DESC,
): TmdbPageResult<TmdbShow> = getWatchlist(TmdbRequestMediaType.TV, accountId, page, sortBy, sortOrder)

suspend fun <T : TmdbMediaListItem> getWatchlist(
mediaType: TmdbRequestMediaType,
accountId: String,
page: Int,
sortBy: TmdbListSortBy? = null,
sortOrder: TmdbSortOrder = TmdbSortOrder.DESC,
): TmdbPageResult<T> = client.get {
endPointAccount(accountId, mediaType.value, "watchlist")
sortBy?.let { parameterSortBy(it, sortOrder) }
parameterPage(page)
}

private fun HttpRequestBuilder.parameterSortBy(sortBy: TmdbListSortBy, sortOrder: TmdbSortOrder) {
parameter("sort_by", sortBy.value + sortOrder.value)
}

private fun HttpRequestBuilder.endPointAccount(accountId: String, vararg paths: String) {
endPointV4("account", accountId, *paths)
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
package app.moviebase.tmdb.api

import app.moviebase.tmdb.model.Tmdb4ListMeta
import app.moviebase.tmdb.model.Tmdb4RedirectToBodyAuth
import app.moviebase.tmdb.model.Tmdb4RequestTokenBody
import app.moviebase.tmdb.model.TmdbPageResult
import io.ktor.client.*
import io.ktor.client.request.*

class Tmdb4AuthenticationApi(private val client: HttpClient) {

suspend fun requestToken(auth: Tmdb4RedirectToBodyAuth): TmdbPageResult<Tmdb4ListMeta> = client.post {
endPointV4("auth", "request_token")
json()
body = auth
}

suspend fun accessToken(requestToken: Tmdb4RequestTokenBody): TmdbPageResult<Tmdb4ListMeta> = client.post {
endPointV4("auth", "access_token")
json()
body = requestToken
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,101 @@
package app.moviebase.tmdb.api

import app.moviebase.tmdb.model.*
import io.ktor.client.*
import io.ktor.client.request.*

class Tmdb4ListApi(private val client: HttpClient) {

/**
* This method will retrieve a list by id.
* Private lists can only be accessed by their owners and therefore require a valid user access token.
*/
suspend fun getList(listId: String): Tmdb4List = client.get {
endPointList(listId)
}

/**
* This method will create a new list.
* You will need to have valid user access token in order to create a new list.
*/
suspend fun create(request: Tmdb4CreateListRequest): TmdbStatusResult = client.post {
endPointV4("list")
json()
body = request
}

/**
* This method will let you update the details of a list.
* You must be the owner of the list and therefore have a valid user access token in order to edit it.
*/
suspend fun update(listId: String, request: Tmdb4UpdateListRequest): TmdbStatusResult = client.put {
endPointList(listId)
json()
body = request
}

/**
* This method lets you clear all of the items from a list in a single request. This action cannot be reversed so use it with caution.
* You must be the owner of the list and therefore have a valid user access token in order to clear a list.
*/
suspend fun clear(listId: String): TmdbStatusResult = client.get {
endPointList(listId, "clear")
}

/**
* This method will delete a list by id. This action is not reversible so take care when issuing it.
* You must be the owner of the list and therefore have a valid user access token in order to delete it.
*/
suspend fun delete(listId: String): TmdbStatusResult = client.delete {
endPointList(listId)
}

/**
* This method will let you add items to a list. We support essentially an unlimited number of items to be posted at a time. Both movie and TV series are support.
* The results of this query will return a results array. Each result includes a success field. If a result is false this will usually indicate that the item already exists on the list. It may also indicate that the item could not be found.
* You must be the owner of the list and therefore have a valid user access token in order to add items to a list.
*/
suspend fun addItems(listId: String, items: Tmdb4ItemsRequest): TmdbStatusResult = client.post {
endPointList(listId, "items")

json()
body = items
}

/**
* This method will let you update an individual item on a list. Currently, only adding a comment is suported.
* You must be the owner of the list and therefore have a valid user access token in order to edit items.
*/
suspend fun updateItems(listId: String, items: Tmdb4UpdateItemsRequest): TmdbStatusResult = client.put {
endPointList(listId, "items")

json()
body = items
}

/**
* This method will let you remove items from a list. You can remove multiple items at a time.
* You must be the owner of the list and therefore have a valid user access token in order to delete items from it.
*/
suspend fun removeItems(listId: String, items: Tmdb4ItemsRequest): TmdbStatusResult = client.delete {
endPointList(listId, "items")

json()
body = items
}

/**
* This method lets you quickly check if the item is already added to the list.
* You must be the owner of the list and therefore have a valid user access token in order to check an item status.
*/
suspend fun checkItemStatus(listId: String, mediaId: Int, mediaType: TmdbMediaType): Tmdb4ItemStatus = client.get {
endPointList(listId, "item_status")
parameter("media_id", mediaId.toString())
parameter("media_type", mediaType.value)
}

private fun HttpRequestBuilder.endPointList(listId: String, vararg paths: String) {
endPointV4("list", listId, *paths)
}

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package app.moviebase.tmdb.api

import app.moviebase.tmdb.model.AccountDetails
import app.moviebase.tmdb.model.TmdbAccountDetails
import app.moviebase.tmdb.model.TmdbFavoriteRequestBody
import app.moviebase.tmdb.model.TmdbWatchlistRequestBody
import io.ktor.client.*
import io.ktor.client.request.*

class TmdbAccountApi(private val client: HttpClient) {

suspend fun getDetails(sessionId: String): AccountDetails = client.get {
suspend fun getDetails(sessionId: String): TmdbAccountDetails = client.get {
endPointV3("account")
parameter("session_id", sessionId)
}

suspend fun markFavorite(accountId: String, requestBody: TmdbFavoriteRequestBody): AccountDetails = client.post {
endPointV3("account", accountId, "favorite")
suspend fun markFavorite(accountId: String, requestBody: TmdbFavoriteRequestBody): TmdbAccountDetails = client.post {
endPointAccount(accountId, "favorite")
parameter("account_id", accountId)
json()

body = requestBody
}

suspend fun markWatchlist(accountId: String, requestBody: TmdbWatchlistRequestBody): AccountDetails = client.post {
endPointV3("account", accountId, "favorite")
suspend fun markWatchlist(accountId: String, requestBody: TmdbWatchlistRequestBody): TmdbAccountDetails = client.post {
endPointAccount(accountId, "favorite")
parameter("account_id", accountId)
json()

body = requestBody
}

private fun HttpRequestBuilder.endPointAccount(accountId: String, vararg paths: String) {
endPointV3("account", accountId, *paths)
}

}
Loading

0 comments on commit 2e6f08a

Please sign in to comment.