-
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.
- Loading branch information
Matthias Reumann
committed
Aug 7, 2021
1 parent
dd5eb4b
commit 5da834c
Showing
16 changed files
with
200 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package googlebooksapi | ||
|
||
import googlebooksapi.data.bookshelf.Bookshelf | ||
import googlebooksapi.data.bookshelf.BookshelfItem | ||
import googlebooksapi.data.volume.Volume | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
|
||
class BookshelfClient : Client() { | ||
suspend fun getAll(url: String): Bookshelf { | ||
val response: HttpResponse = httpClient.get(url) {} | ||
return response.receive() | ||
} | ||
|
||
suspend fun get(url: String): BookshelfItem { | ||
val response: HttpResponse = httpClient.get(url) {} | ||
return response.receive() | ||
} | ||
} |
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,65 @@ | ||
package googlebooksapi | ||
|
||
import googlebooksapi.data.bookshelf.Bookshelf | ||
import googlebooksapi.data.bookshelf.BookshelfItem | ||
import googlebooksapi.data.volume.Volume | ||
import googlebooksapi.exceptions.HelperException | ||
import googlebooksapi.options.FilterOption | ||
import googlebooksapi.options.PrintTypeOption | ||
import googlebooksapi.options.ProjectionOption | ||
import googlebooksapi.options.SortOption | ||
import io.ktor.client.features.* | ||
|
||
class BookshelfHelper(apikey: String) { | ||
private val urlBuilder: BookshelfURLBuilder | ||
private var userID: String | ||
|
||
init { | ||
urlBuilder = BookshelfURLBuilder(apikey) | ||
userID = StringUtils.EMPTY_STRING | ||
} | ||
|
||
fun userID(id: String) { | ||
userID = id | ||
} | ||
|
||
suspend fun getAll(): Bookshelf { | ||
val url = urlBuilder.getURL(userID) | ||
val client = BookshelfClient() | ||
val bookshelf: Bookshelf | ||
|
||
try { | ||
bookshelf = client.getAll(url) | ||
} catch (redirectException: RedirectResponseException) { | ||
throw HelperException(redirectException.message ?: "3xx received") | ||
} catch (clientException: ClientRequestException) { | ||
throw HelperException(clientException.message) | ||
} catch (serverException: ServerResponseException) { | ||
throw HelperException(serverException.message ?: "5xx received") | ||
} finally { | ||
client.close() | ||
} | ||
|
||
return bookshelf | ||
} | ||
|
||
suspend fun get(bookshelfID: String): BookshelfItem { | ||
val url = urlBuilder.getURLByBookshelfID(userID, bookshelfID) | ||
val client = BookshelfClient() | ||
val item: BookshelfItem | ||
|
||
try { | ||
item = client.get(url) | ||
} catch (redirectException: RedirectResponseException) { | ||
throw HelperException(redirectException.message ?: "3xx received") | ||
} catch (clientException: ClientRequestException) { | ||
throw HelperException(clientException.message) | ||
} catch (serverException: ServerResponseException) { | ||
throw HelperException(serverException.message ?: "5xx received") | ||
} finally { | ||
client.close() | ||
} | ||
|
||
return item | ||
} | ||
} |
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,23 @@ | ||
package googlebooksapi | ||
|
||
import googlebooksapi.exceptions.InvalidUserIdException | ||
import java.lang.StringBuilder | ||
|
||
class BookshelfURLBuilder(private var key: String = StringUtils.EMPTY_STRING) : URLBuilder() { | ||
private val url: String | ||
|
||
init { | ||
url = baseURL + "/users" | ||
} | ||
|
||
fun getURL(userID: String): String { | ||
if (userID.isEmpty()) { | ||
throw InvalidUserIdException("UserID is empty") | ||
} | ||
return "$url/$userID/bookshelves?key=$key" | ||
} | ||
|
||
fun getURLByBookshelfID(userID: String, bookshelfID: String): String { | ||
return "$url/$userID/bookshelves/$bookshelfID?key=$key" | ||
} | ||
} |
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 |
---|---|---|
|
@@ -16,6 +16,4 @@ abstract class Client { | |
fun close(){ | ||
httpClient.close() | ||
} | ||
|
||
abstract suspend fun get(url: String): Volume | ||
} |
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
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,6 @@ | ||
package googlebooksapi.data.bookshelf | ||
|
||
data class Bookshelf( | ||
val kind: String, | ||
val bookshelfItems: List<BookshelfItem> | ||
) |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/googlebooksapi/data/bookshelf/BookshelfItem.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,17 @@ | ||
package googlebooksapi.data.bookshelf | ||
|
||
import java.net.URI | ||
import java.time.Instant | ||
|
||
data class BookshelfItem( | ||
val kind: String, | ||
val id: Int, | ||
val selfLink: URI, | ||
val title: String, | ||
val description: String, | ||
val access: String, | ||
val updated: Instant, | ||
val created: Instant, | ||
val volumeCount: Int, | ||
val volumesLastUpdated: Instant | ||
) |
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/googlebooksapi/exceptions/InvalidUserIdException.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,4 @@ | ||
package googlebooksapi.exceptions | ||
|
||
class InvalidUserIdException(message: String) : Exception(message) { | ||
} |
2 changes: 1 addition & 1 deletion
2
...ain/kotlin/googlebooksapi/FilterOption.kt → ...in/googlebooksapi/options/FilterOption.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
2 changes: 1 addition & 1 deletion
2
.../kotlin/googlebooksapi/PrintTypeOption.kt → ...googlebooksapi/options/PrintTypeOption.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
2 changes: 1 addition & 1 deletion
2
...kotlin/googlebooksapi/ProjectionOption.kt → ...ooglebooksapi/options/ProjectionOption.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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/googlebooksapi/SortOption.kt → ...tlin/googlebooksapi/options/SortOption.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
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,37 @@ | ||
package googlebooksapi | ||
|
||
import googlebooksapi.exceptions.InvalidUserIdException | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
internal class BookshelfURLBuilderTest { | ||
companion object { | ||
val EMPTY_STRING: String = "" | ||
|
||
val KEY: String = "xxxx" | ||
val USERID: String = "11111" | ||
val BOOKSHELFID: String = "3" | ||
} | ||
|
||
@Test(InvalidUserIdException::class) | ||
fun testGetURLEmptyUserID() { | ||
val urlBuilder = BookshelfURLBuilder(KEY) | ||
urlBuilder.getURL(userID = EMPTY_STRING) | ||
} | ||
|
||
@Test | ||
fun testGetURLValid() { | ||
val urlBuilder = BookshelfURLBuilder(KEY) | ||
val actual = urlBuilder.getURL(userID = USERID) | ||
val expected = "https://www.googleapis.com/books/v1/users/$USERID/bookshelves?key=$KEY" | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun testGetURLByBookshelfID(){ | ||
val urlBuilder = BookshelfURLBuilder(KEY) | ||
val actual = urlBuilder.getURLByBookshelfID(USERID, BOOKSHELFID) | ||
val expected = "https://www.googleapis.com/books/v1/users/$USERID/bookshelves/$BOOKSHELFID?key=$KEY" | ||
assertEquals(expected, actual) | ||
} | ||
} |
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