Skip to content

Commit

Permalink
Service クラスの追加
Browse files Browse the repository at this point in the history
  • Loading branch information
warahiko committed Sep 30, 2020
1 parent 395399a commit 96d772b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.openapigenerationtest

import com.example.openapigenerationtestapi.infrastructure.ApiClient
import com.example.openapigenerationtestapi.sp.api.ItemApi
import com.example.openapigenerationtestapi.sp.model.Item
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Callback

class QiitaItemService {

private val itemApi: ItemApi

init {
val loggingInterceptor = HttpLoggingInterceptor().apply {
setLevel(HttpLoggingInterceptor.Level.BODY)
}
val httpClient: OkHttpClient = OkHttpClient.Builder().apply {
addInterceptor(loggingInterceptor)
addInterceptor(Interceptor { chain ->
chain.proceed(
chain.request()
.newBuilder()
.header("Content-Type", "application/json")
.header("Authorization", "Bearer 46f9e2655f4f0afb5ee0d721acb03693171aed26")
.build()
)
})
}.build()

itemApi = ApiClient(okHttpClient = httpClient).createService(ItemApi::class.java)
}

fun getAllItems(page: Int, perPage: Int, query: String, callback: Callback<Array<Item>>) {
val call = itemApi.getAllItems(page.toString(), perPage.toString(), query)
call.enqueue(callback)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.openapigenerationtest

import com.example.openapigenerationtestapi.infrastructure.ApiClient
import com.example.openapigenerationtestapi.sp.api.UserApi
import com.example.openapigenerationtestapi.sp.model.User
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Callback

class QiitaUserService {

private val userApi: UserApi

init {
val loggingInterceptor = HttpLoggingInterceptor().apply {
setLevel(HttpLoggingInterceptor.Level.BODY)
}
val httpClient: OkHttpClient = OkHttpClient.Builder().apply {
addInterceptor(loggingInterceptor)
addInterceptor(Interceptor { chain ->
chain.proceed(
chain.request()
.newBuilder()
.header("Content-Type", "application/json")
.header("Authorization", "Bearer 46f9e2655f4f0afb5ee0d721acb03693171aed26")
.build()
)
})
}.build()

userApi = ApiClient(okHttpClient = httpClient).createService(UserApi::class.java)
}

fun getAllUser(page: Int, perPage: Int, callback: Callback<Array<User>>) {
val call = userApi.getAllUser(page.toString(), perPage.toString())
call.enqueue(callback)
}

fun getUser(userId: String, callback: Callback<User>) {
val call = userApi.getUser(userId)
call.enqueue(callback)
}
}

0 comments on commit 96d772b

Please sign in to comment.