GEEKCAMP-Vol12 / GEEKCAMP_Vol.12

0 stars 0 forks source link

Ktorfitを導入する #18

Open SEKI-YUTA opened 1 month ago

SEKI-YUTA commented 1 month ago

ktorfit https://foso.github.io/Ktorfit/

github-actions[bot] commented 1 month ago

This issue's work branch is "HealthApp-18". branch name:

HealthApp-18

To checkout this branch, run the following command:

git checkout -b HealthApp-18
SEKI-YUTA commented 1 month ago

このコードで動作確認をした

interface PhotoAPI {
    @GET("albums/1/photos")
    suspend fun getPhotos(): List<PhotoAPIResponseItem>

    companion object {
        val BASE_URL = "https://jsonplaceholder.typicode.com/"

        suspend fun callPhotosAPI(): List<PhotoAPIResponseItem> {
            val client = HttpClient {
                install(ContentNegotiation) {
                    json(Json {
                        ignoreUnknownKeys = true
                        isLenient = true
                    })
                }
            }

            val ktorfit = Ktorfit.Builder()
                .httpClient(client)
                .baseUrl(BASE_URL)
                .build()

            return withContext(Dispatchers.IO) {
                val photoList = ktorfit.createPhotoAPI().getPhotos()
                photoList
            }
        }

    }
}