diff --git a/app/src/main/java/com/example/openapigenerationtest/QiitaItemService.kt b/app/src/main/java/com/example/openapigenerationtest/QiitaItemService.kt new file mode 100644 index 0000000..b00ba5b --- /dev/null +++ b/app/src/main/java/com/example/openapigenerationtest/QiitaItemService.kt @@ -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>) { + val call = itemApi.getAllItems(page.toString(), perPage.toString(), query) + call.enqueue(callback) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/openapigenerationtest/QiitaUserService.kt b/app/src/main/java/com/example/openapigenerationtest/QiitaUserService.kt new file mode 100644 index 0000000..b35f34f --- /dev/null +++ b/app/src/main/java/com/example/openapigenerationtest/QiitaUserService.kt @@ -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>) { + val call = userApi.getAllUser(page.toString(), perPage.toString()) + call.enqueue(callback) + } + + fun getUser(userId: String, callback: Callback) { + val call = userApi.getUser(userId) + call.enqueue(callback) + } +} \ No newline at end of file