-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from rubenquadros/artists-api
Adding the artist api.
- Loading branch information
Showing
33 changed files
with
9,828 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
kovibes/src/commonMain/kotlin/io/github/rubenquadros/kovibes/api/artist/ArtistApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.github.rubenquadros.kovibes.api.artist | ||
|
||
import io.github.rubenquadros.kovibes.api.ApiResponse | ||
import io.github.rubenquadros.kovibes.api.artist.models.GetArtistTopTracksResponse | ||
import io.github.rubenquadros.kovibes.api.models.AlbumResponse | ||
import io.github.rubenquadros.kovibes.api.models.ArtistInfo | ||
import io.github.rubenquadros.kovibes.api.response.ErrorBody | ||
|
||
/** | ||
* Artist API interface provides methods using which one can get information about the artist. | ||
* You can get artists' top tracks, albums and also other related artists. | ||
* | ||
* Each API returns [ApiResponse]. | ||
*/ | ||
internal interface ArtistApi { | ||
|
||
/** | ||
* Get artist API returns the artist information from the given ID. | ||
* | ||
* @param id | ||
* @return [ArtistInfo] when success and [ErrorBody] when error. | ||
*/ | ||
suspend fun getArtist(id: String): ApiResponse<ArtistInfo, ErrorBody> | ||
|
||
/** | ||
* Get artist albums API returns the albums, single, features and compilations of the artist. | ||
* | ||
* @param id | ||
* @param includeGroups | ||
* @param market | ||
* @param limit | ||
* @param offset | ||
* @return [AlbumResponse] when success and [ErrorBody] when error. | ||
*/ | ||
suspend fun getArtistAlbums( | ||
id: String, | ||
includeGroups: List<String>, | ||
market: String?, | ||
limit: Int, | ||
offset: Int | ||
): ApiResponse<AlbumResponse, ErrorBody> | ||
|
||
/** | ||
* Get artist top tracks API returns the top tracks of the artist. | ||
* | ||
* @param id | ||
* @param market | ||
* @return [GetArtistTopTracksResponse] when success and [ErrorBody] when error. | ||
*/ | ||
suspend fun getArtistTopTracks( | ||
id: String, | ||
market: String? | ||
): ApiResponse<GetArtistTopTracksResponse, ErrorBody> | ||
} |
87 changes: 87 additions & 0 deletions
87
kovibes/src/commonMain/kotlin/io/github/rubenquadros/kovibes/api/artist/ArtistApiImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.github.rubenquadros.kovibes.api.artist | ||
|
||
import io.github.rubenquadros.kovibes.api.ApiResponse | ||
import io.github.rubenquadros.kovibes.api.KtorService | ||
import io.github.rubenquadros.kovibes.api.artist.models.GetArtistTopTracksResponse | ||
import io.github.rubenquadros.kovibes.api.getParsedHttpResponse | ||
import io.github.rubenquadros.kovibes.api.models.AlbumResponse | ||
import io.github.rubenquadros.kovibes.api.models.ArtistInfo | ||
import io.github.rubenquadros.kovibes.api.response.ErrorBody | ||
import io.ktor.client.request.get | ||
import io.ktor.http.path | ||
import io.ktor.util.StringValues | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* @suppress | ||
* ArtistApiImpl is the implementation of [ArtistApi]. | ||
* | ||
* @property ktorService | ||
* @property dispatcher | ||
*/ | ||
internal class ArtistApiImpl( | ||
private val ktorService: KtorService, | ||
private val dispatcher: CoroutineDispatcher = Dispatchers.IO | ||
) : ArtistApi { | ||
override suspend fun getArtist(id: String): ApiResponse<ArtistInfo, ErrorBody> { | ||
val response = withContext(dispatcher) { | ||
ktorService.client.get { | ||
url { | ||
path("v1/artists/$id") | ||
} | ||
} | ||
} | ||
|
||
return response.getParsedHttpResponse() | ||
} | ||
|
||
override suspend fun getArtistAlbums( | ||
id: String, | ||
includeGroups: List<String>, | ||
market: String?, | ||
limit: Int, | ||
offset: Int | ||
): ApiResponse<AlbumResponse, ErrorBody> { | ||
val response = withContext(dispatcher) { | ||
ktorService.client.get { | ||
url { | ||
path("v1/artists/$id/albums") | ||
|
||
parameters.appendAll( | ||
StringValues.build { | ||
market?.let { this["market"] = market } | ||
this["include_groups"] = includeGroups.joinToString { it } | ||
this["limit"] = limit.toString() | ||
this["offset"] = offset.toString() | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
return response.getParsedHttpResponse() | ||
} | ||
|
||
override suspend fun getArtistTopTracks( | ||
id: String, | ||
market: String? | ||
): ApiResponse<GetArtistTopTracksResponse, ErrorBody> { | ||
val response = withContext(dispatcher) { | ||
ktorService.client.get { | ||
url { | ||
path("v1/artists/$id/top-tracks") | ||
|
||
parameters.appendAll( | ||
StringValues.build { | ||
market?.let { this["market"] = market } | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
return response.getParsedHttpResponse() | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
kovibes/src/commonMain/kotlin/io/github/rubenquadros/kovibes/api/artist/ArtistApiMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.github.rubenquadros.kovibes.api.artist | ||
|
||
import io.github.rubenquadros.kovibes.api.artist.models.GetArtistTopTracksResponse | ||
import io.github.rubenquadros.kovibes.api.mapper.toTrack | ||
import io.github.rubenquadros.kovibes.api.response.ArtistTopTracks | ||
|
||
/** | ||
* @suppress | ||
* Map [GetArtistTopTracksResponse] to [ArtistTopTracks]. | ||
*/ | ||
internal fun GetArtistTopTracksResponse.toArtistTopTracks(): ArtistTopTracks { | ||
return ArtistTopTracks( | ||
tracks = tracks.map { it.toTrack() } | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
...ain/kotlin/io/github/rubenquadros/kovibes/api/artist/models/GetArtistTopTracksResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.github.rubenquadros.kovibes.api.artist.models | ||
|
||
import io.github.rubenquadros.kovibes.api.ExcludeFromCoverage | ||
import io.github.rubenquadros.kovibes.api.models.TrackInfo | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@ExcludeFromCoverage | ||
@Serializable | ||
internal data class GetArtistTopTracksResponse( | ||
@SerialName("tracks") | ||
val tracks: List<TrackInfo> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.