11package com.prof18.rssparser.internal
22
33import com.prof18.rssparser.exception.HttpException
4- import kotlinx.coroutines.suspendCancellableCoroutine
54import okhttp3.Call
6- import okhttp3.Callback
75import okhttp3.Request
8- import okhttp3.Response
9- import java.io.IOException
10- import java.io.InputStream
11- import kotlin.coroutines.resume
12- import kotlin.coroutines.resumeWithException
6+ import okhttp3.coroutines.executeAsync
137
148internal class JvmXmlFetcher (
159 private val callFactory : Call .Factory ,
@@ -18,71 +12,37 @@ internal class JvmXmlFetcher(
1812 override suspend fun fetchXml (url : String ): ParserInput {
1913 val request = createRequest(url)
2014 val baseUrl = request.url.scheme + " ://" + request.url.host
15+ val response = callFactory.newCall(request).executeAsync()
16+ if (! response.isSuccessful) {
17+ val exception = HttpException (
18+ code = response.code,
19+ message = response.message,
20+ )
21+ response.close()
22+ throw exception
23+ }
2124 return ParserInput (
22- inputStream = callFactory.newCall(request).awaitForInputStream (),
25+ inputStream = response.body.byteStream (),
2326 baseUrl = baseUrl,
2427 )
2528 }
2629
2730 override suspend fun fetchXmlAsString (url : String ): String {
2831 val request = createRequest(url)
29- return callFactory.newCall(request).awaitForString()
32+ val response = callFactory.newCall(request).executeAsync()
33+ if (! response.isSuccessful) {
34+ val exception = HttpException (
35+ code = response.code,
36+ message = response.message,
37+ )
38+ response.close()
39+ throw exception
40+ }
41+ return response.body.string()
3042 }
3143
3244 private fun createRequest (url : String ): Request =
3345 Request .Builder ()
3446 .url(url)
3547 .build()
36-
37- private suspend fun Call.awaitForInputStream (): InputStream = suspendCancellableCoroutine { continuation ->
38- continuation.invokeOnCancellation {
39- cancel()
40- }
41-
42- enqueue(object : Callback {
43- override fun onResponse (call : Call , response : Response ) {
44- if (response.isSuccessful) {
45- val body = requireNotNull(response.body)
46- continuation.resume(body.byteStream())
47- } else {
48- val exception = HttpException (
49- code = response.code,
50- message = response.message,
51- )
52- response.close()
53- continuation.resumeWithException(exception)
54- }
55- }
56-
57- override fun onFailure (call : Call , e : IOException ) {
58- continuation.resumeWithException(e)
59- }
60- })
61- }
62-
63- private suspend fun Call.awaitForString (): String = suspendCancellableCoroutine { continuation ->
64- continuation.invokeOnCancellation {
65- cancel()
66- }
67-
68- enqueue(object : Callback {
69- override fun onResponse (call : Call , response : Response ) {
70- if (response.isSuccessful) {
71- val body = requireNotNull(response.body)
72- continuation.resume(body.string())
73- } else {
74- val exception = HttpException (
75- code = response.code,
76- message = response.message,
77- )
78- response.close()
79- continuation.resumeWithException(exception)
80- }
81- }
82-
83- override fun onFailure (call : Call , e : IOException ) {
84- continuation.resumeWithException(e)
85- }
86- })
87- }
8848}
0 commit comments