Add OkHttp interceptor to raise exceptions for non-successfull requests

This commit is contained in:
Shinokuni 2023-07-27 22:55:28 +02:00
parent 5c7f86b170
commit d673616bb4
4 changed files with 75 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import com.readrops.api.services.nextcloudnews.adapters.NextNewsFeedsAdapter
import com.readrops.api.services.nextcloudnews.adapters.NextNewsFoldersAdapter import com.readrops.api.services.nextcloudnews.adapters.NextNewsFoldersAdapter
import com.readrops.api.services.nextcloudnews.adapters.NextNewsItemsAdapter import com.readrops.api.services.nextcloudnews.adapters.NextNewsItemsAdapter
import com.readrops.api.utils.AuthInterceptor import com.readrops.api.utils.AuthInterceptor
import com.readrops.api.utils.ErrorInterceptor
import com.readrops.db.entities.Item import com.readrops.db.entities.Item
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
import com.squareup.moshi.Types import com.squareup.moshi.Types
@ -29,12 +30,15 @@ val apiModule = module {
.callTimeout(1, TimeUnit.MINUTES) .callTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.HOURS) .readTimeout(1, TimeUnit.HOURS)
.addInterceptor(get<AuthInterceptor>()) .addInterceptor(get<AuthInterceptor>())
.addInterceptor(get<ErrorInterceptor>())
//.addInterceptor(NiddlerOkHttpInterceptor(get(), "niddler")) //.addInterceptor(NiddlerOkHttpInterceptor(get(), "niddler"))
.build() .build()
} }
single { AuthInterceptor() } single { AuthInterceptor() }
single { ErrorInterceptor() }
single { LocalRSSDataSource(get()) } single { LocalRSSDataSource(get()) }
//region freshrss //region freshrss

View File

@ -0,0 +1,19 @@
package com.readrops.api.utils
import com.readrops.api.utils.exceptions.HttpException
import okhttp3.Interceptor
import okhttp3.Response
class ErrorInterceptor() : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val response = chain.proceed(request)
if (!response.isSuccessful) {
throw HttpException(response)
}
return response
}
}

View File

@ -0,0 +1,13 @@
package com.readrops.api.utils.exceptions
import okhttp3.Response
class HttpException(val response: Response) : Exception() {
val code: Int
get() = response.code
override val message: String
get() = "HTTP " + response.code + " " + response.message
}

View File

@ -0,0 +1,39 @@
package com.readrops.api.utils
import com.readrops.api.utils.exceptions.HttpException
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import org.junit.Test
import java.net.HttpURLConnection
class ErrorInterceptorTest {
private val interceptor = ErrorInterceptor()
private val server = MockWebServer()
private lateinit var client: OkHttpClient
@Before
fun before() {
client = OkHttpClient.Builder()
.addInterceptor(interceptor)
.build()
server.start(8080)
}
@After
fun tearDown() {
server.close()
}
@Test(expected = HttpException::class)
fun interceptorTest() {
server.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND))
client.newCall(Request.Builder().url(server.url("/url")).build()).execute()
//val request = server.takeRequest()
}
}