๐ ์๋ผ๋ Open API ๋งค๋ด์ผ: https://docs.google.com/document/d/1mX-WxuoGs8Hy-QalhHcvuV17n50uGI2Sg_GHofgiePE/edit
๐ API ํค ๋ฐ๊ธ: https://www.aladin.co.kr/ttb/wblog_manage.aspx
์ฐ๋ ์ค๋น
1. ์คํ API๋ฅผ ์ฌ์ฉํ URL ์ ๋ ฅ ํ ์ถ๊ฐ ๋ฒํผ ํด๋ฆญ (๋ธ๋ก๊ทธ ์ฃผ์ ์ ๋ ฅํด๋ ๋จ)
2. TTB ํค ๋ฐ๊ธ๋จ
์ฝ๋
DTO
- ๊ฒ์ ๊ฒฐ๊ณผ ์ ์ฒด ์ ์ฅ
data class BookListDTO(
@SerializedName("item") val books: List<Book>
)
- ๊ฒ์ ๊ฒฐ๊ณผ ์ค ํ๋
data class Book(
@SerializedName("title") val title: String,
@SerializedName("author") val author: String,
@SerializedName("description") val description: String,
@SerializedName("isbn13") val isbn: String,
@SerializedName("cover") val cover: String,
@SerializedName("categoryName") val categoryName: String,
@SerializedName("itemPage") val itemPage: String,
)
์ธํฐํ์ด์ค
์ด ๋ ๊ณตํต url ์ฃผ์๋ http://www.aladin.co.kr/ttb/api/ ==> Retrofit ๊ฐ์ฒด ์์ฑ ์ ์์ฑ
์์ฒญ ๋ณ์๋ ์๋ผ๋ API ๋ฌธ์ ํ์ธํด ๋ณธ์ธํํ ํ์ํ ๋ณ์ ์ ๋ ฅํ๋ฉด ๋จ (ํ์ ์์ฒญ ๋ณ์๋ ๋ฐ๋์ ์ ๋ ฅ)
โ API ๋ฒ์ ์ 20131101๋ก ํด์ผ ์ํ ๋ฆฌ์คํธ API๊ฐ ์ํํ๊ฒ ์๋ํ๋ค. (๊ธฐ๋ณธ๊ฐ: 20070901)
๋ชจ๋ฅด๊ณ ์์ ํ๋ค๊ฐ ์ฑ ๋ชฉ๋ก์ด ์ ๋๋ก ์ ๋ถ๋ฌ์์ ธ์ ์ ๋จน์์
interface BookService {
// ์๋ผ๋ ์ฑ
๊ฒ์ api
@GET("ItemSearch.aspx")
suspend fun getBookSearch(
@Query("TTBKey") ttbkey: String, // ๋ฐ๊ธ ๋ฐ์ ์๋ผ๋ api key ์
๋ ฅ
@Query("Query") query: String, // ๊ฒ์์ด
@Query("Output") output: String, // ์ถ๋ ฅ ํ์
@Query("Version") version: String = "20131101" // api ๋ฒ์
): Response<BookListDTO>
// ์๋ผ๋ ์ฑ
๋ชฉ๋ก api
@GET("ItemList.aspx")
suspend fun getBookList(
@Query("TTBKey") ttbkey: String,
@Query("QueryType") querytype: String,
@Query("SearchTarget") searchtarget: String,
@Query("Output") output: String,
@Query("Version") version: String = "20131101"
): Response<BookListDTO>
// ์๋ผ๋ ์ฑ
์์ธ api
@GET("ItemLookUp.aspx")
suspend fun getBookDetail(
@Query("TTBKey") ttbkey: String,
@Query("ItemId") itemid: String,
@Query("itemIdType") itemidtype: String,
@Query("Output") output: String,
@Query("Version") version: String = "20131101"
): Response<BookListDTO>
}
Retrofit ๊ฐ์ฒด ์์ฑ
private val gson = GsonBuilder().setLenient().create()
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("http://www.aladin.co.kr/ttb/api/")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
val bookService = retrofit.create(BookService::class.java) // ์์์ ์์ฑํ ์ธํฐํ์ด์ค
ํจ์ ์์ฑ
์ธํฐํ์ด์ค ์์ฑ ์ ๋ฐํ๊ฐ์ Response๋ก ์ค์ ํ๊ธฐ ๋๋ฌธ์ suspend ์ฌ์ฉ (Call๋ก ๋ฐํ ์ ์ฌ์ฉํ ํ์ X)
suspend fun getBookSearch(query: String): List<Book> {
val bookSearchList = bookService
.getBookSearch(TTBKEY, query, OUTPUT, VERSION)
return bookSearchList.body()!!.books
}
suspend fun getBookList(): List<Book> {
val bookList = bookService
.getBookList(TTBKEY, QUERY_TYPE, SEARCH_TARGET, OUTPUT, VERSION)
return bookList.body()!!.books
}
suspend fun getBookDetail(itemid: String): Book {
val bookDetail = bookService
.getBookDetail(TTBKEY, itemid, ITEM_ID_TYPE, OUTPUT, VERSION)
return bookDetail.body()!!.books[0]
}
private companion object{
private const val TTBKEY = "๋ฐ๊ธ๋ฐ์ TTB ํค"
private const val QUERY_TYPE = "ItemNewSpecial"
private const val SEARCH_TARGET = "Book"
private const val ITEM_ID_TYPE = "ISBN13"
private const val OUTPUT = "JS"
private const val VERSION = "20131101"
}
ํจ์ ์ฌ์ฉ ์์ ์ฝ๋ฃจํด ์ฌ์ฉ (suspend ํจ์๊ธฐ ๋๋ฌธ)
๋ฐํ ๊ฐ์ด Response๊ธฐ ๋๋ฌธ์ Call๊ณผ ๋ฌ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ ๋ฐ๋ก ์ฌ์ฉ ๊ฐ๋ฅ
lifecycleScope.launch {
val bookList = getBookList()
binding.bookTitle.text = bookList[0].title
binding.bookAuthor.text = bookList[0].author
}
'Kotlin' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android/Kotlin] ๊ตฌ๊ธ ๋ก๊ทธ์ธ ๊ตฌํ (0) | 2024.02.12 |
---|---|
[Android/Kotlin] ๋ค์ด๋ฒ ์ง๋ API ๊ฒ์ (0) | 2024.02.06 |
[Android/Kotlin] ๋ค์ด๋ฒ ์ง๋ API ์ฐ๋ (0) | 2024.02.06 |
๋๊ธ