@@ -16,7 +16,8 @@ fun cachingHttpClient(
16
16
cacheSize : Long = 10L * 1024L * 1024L,
17
17
trustAllCerts : Boolean = true,
18
18
connectTimeoutSecs : Long = 30L,
19
- readTimeoutSecs : Long = 30L
19
+ readTimeoutSecs : Long = 30L,
20
+ block : (OkHttpClient .Builder .() -> Unit )? = null,
20
21
): OkHttpClient {
21
22
val builder: OkHttpClient .Builder = OkHttpClient .Builder ()
22
23
@@ -33,6 +34,10 @@ fun cachingHttpClient(
33
34
builder.trustAllCerts()
34
35
}
35
36
37
+ block?.let {
38
+ builder.block()
39
+ }
40
+
36
41
return builder.build()
37
42
}
38
43
@@ -45,24 +50,24 @@ fun feedAdapter(): JsonAdapter<Feed> =
45
50
*/
46
51
class JsonFeedParser (
47
52
private val httpClient : OkHttpClient ,
48
- private val jsonFeedAdapter : JsonAdapter <Feed >
53
+ private val jsonFeedAdapter : JsonAdapter <Feed >,
49
54
) {
50
55
51
56
constructor (
52
57
cacheDirectory: File ? = null ,
53
58
cacheSize: Long = 10L * 1024L * 1024L ,
54
59
trustAllCerts: Boolean = true ,
55
60
connectTimeoutSecs: Long = 5L ,
56
- readTimeoutSecs: Long = 5L
61
+ readTimeoutSecs: Long = 5L ,
57
62
) : this (
58
63
cachingHttpClient(
59
64
cacheDirectory = cacheDirectory,
60
65
cacheSize = cacheSize,
61
66
trustAllCerts = trustAllCerts,
62
67
connectTimeoutSecs = connectTimeoutSecs,
63
- readTimeoutSecs = readTimeoutSecs
68
+ readTimeoutSecs = readTimeoutSecs,
64
69
),
65
- feedAdapter()
70
+ feedAdapter(),
66
71
)
67
72
68
73
/* *
@@ -77,7 +82,7 @@ class JsonFeedParser(
77
82
} catch (error: Throwable ) {
78
83
throw IllegalArgumentException (
79
84
" Bad URL. Perhaps it is missing an http:// prefix?" ,
80
- error
85
+ error,
81
86
)
82
87
}
83
88
@@ -95,11 +100,13 @@ class JsonFeedParser(
95
100
contentType.subtype.contains(" json" ) -> {
96
101
parseJson(body)
97
102
}
103
+
98
104
else -> {
99
105
throw IOException (" Incorrect subtype: ${contentType.type} /${contentType.subtype} " )
100
106
}
101
107
}
102
108
}
109
+
103
110
else -> {
104
111
throw IOException (" Incorrect type: ${contentType?.type} /${contentType?.subtype} " )
105
112
}
@@ -133,13 +140,13 @@ data class Feed(
133
140
val author : Author ? = null ,
134
141
val expired : Boolean? = null ,
135
142
val hubs : List <Hub >? = null ,
136
- val items : List <Item >?
143
+ val items : List <Item >? ,
137
144
)
138
145
139
146
data class Author (
140
147
val name : String? = null ,
141
148
val url : String? = null ,
142
- val avatar : String? = null
149
+ val avatar : String? = null ,
143
150
)
144
151
145
152
data class Item (
@@ -156,18 +163,18 @@ data class Item(
156
163
val date_modified : String? = null ,
157
164
val author : Author ? = null ,
158
165
val tags : List <String >? = null ,
159
- val attachments : List <Attachment >? = null
166
+ val attachments : List <Attachment >? = null ,
160
167
)
161
168
162
169
data class Attachment (
163
170
val url : String? ,
164
171
val mime_type : String? = null ,
165
172
val title : String? = null ,
166
173
val size_in_bytes : Long? = null ,
167
- val duration_in_seconds : Long? = null
174
+ val duration_in_seconds : Long? = null ,
168
175
)
169
176
170
177
data class Hub (
171
178
val type : String? ,
172
- val url : String?
179
+ val url : String? ,
173
180
)
0 commit comments