Skip to content

Commit 69b9c00

Browse files
committed
Introduce "bytes" versions of HTTP network methods
Bug: 756
1 parent f08428d commit 69b9c00

File tree

5 files changed

+674
-76
lines changed

5 files changed

+674
-76
lines changed

common/kobwebx-serialization-kotlinx/src/jsMain/kotlin/com/varabyte/kobweb/browser/ApiFetcherExtensions.kt

Lines changed: 100 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ suspend inline fun <reified R> ApiFetcher.get(
2626
): R {
2727
return Json.decodeFromString(
2828
responseDeserializer,
29-
get(apiPath, headers, redirect, abortController).decodeToString()
29+
getBytes(apiPath, headers, redirect, abortController).decodeToString()
3030
)
3131
}
3232

@@ -43,7 +43,7 @@ suspend inline fun <reified R> ApiFetcher.tryGet(
4343
abortController: AbortController? = null,
4444
responseDeserializer: DeserializationStrategy<R> = serializer()
4545
): R? {
46-
return tryGet(apiPath, headers, redirect, abortController)
46+
return tryGetBytes(apiPath, headers, redirect, abortController)
4747
?.decodeToString()
4848
?.let { Json.decodeFromString(responseDeserializer, it) }
4949
}
@@ -69,7 +69,7 @@ suspend inline fun <reified B, reified R> ApiFetcher.post(
6969
bodySerializer: SerializationStrategy<B> = serializer(),
7070
responseDeserializer: DeserializationStrategy<R> = serializer()
7171
): R {
72-
val responseBytes = post(
72+
val responseBytes = postBytes(
7373
apiPath,
7474
body,
7575
mapOf("Content-type" to "application/json") + headers.orEmpty(),
@@ -86,15 +86,28 @@ suspend inline fun <reified B, reified R> ApiFetcher.post(
8686
/**
8787
* A serialize-friendly version of [post] that expects a body but does not expect a serialized response.
8888
*/
89+
@Deprecated("DO NOT IGNORE. Please change to `postBytes` instead. This method will be modified soon in a backwards incompatible way, in order to support additional cases that the current form doesn't support.", replaceWith = ReplaceWith("postBytes(apiPath, body, headers, redirect, abortController, bodySerializer)"))
8990
suspend inline fun <reified B> ApiFetcher.post(
9091
apiPath: String,
9192
body: B,
9293
headers: Map<String, Any>? = FetchDefaults.Headers,
9394
redirect: RequestRedirect? = FetchDefaults.Redirect,
9495
abortController: AbortController? = null,
9596
bodySerializer: SerializationStrategy<B> = serializer(),
97+
): ByteArray = postBytes(apiPath, body, headers, redirect, abortController, bodySerializer)
98+
99+
/**
100+
* A serialize-friendly version of [post] that expects a body and returns its response as a raw byte array.
101+
*/
102+
suspend inline fun <reified B> ApiFetcher.postBytes(
103+
apiPath: String,
104+
body: B,
105+
headers: Map<String, Any>? = FetchDefaults.Headers,
106+
redirect: RequestRedirect? = FetchDefaults.Redirect,
107+
abortController: AbortController? = null,
108+
bodySerializer: SerializationStrategy<B> = serializer(),
96109
): ByteArray {
97-
return post(
110+
return postBytes(
98111
apiPath,
99112
mapOf("Content-type" to "application/json") + headers.orEmpty(),
100113
Json.encodeToString(bodySerializer, body).encodeToByteArray(),
@@ -113,7 +126,7 @@ suspend inline fun <reified R> ApiFetcher.post(
113126
abortController: AbortController? = null,
114127
responseDeserializer: DeserializationStrategy<R> = serializer(),
115128
): R {
116-
val responseBytes = post(
129+
val responseBytes = postBytes(
117130
apiPath,
118131
headers,
119132
body = null,
@@ -142,7 +155,7 @@ suspend inline fun <reified B, reified R> ApiFetcher.tryPost(
142155
bodySerializer: SerializationStrategy<B> = serializer(),
143156
responseDeserializer: DeserializationStrategy<R> = serializer()
144157
): R? {
145-
val responseBytes = tryPost(
158+
val responseBytes = tryPostBytes(
146159
apiPath,
147160
body,
148161
mapOf("Content-type" to "application/json") + headers.orEmpty(),
@@ -160,15 +173,28 @@ suspend inline fun <reified B, reified R> ApiFetcher.tryPost(
160173
/**
161174
* A serialize-friendly version of [tryPost] that expects a body but does not expect a serialized response.
162175
*/
176+
@Deprecated("DO NOT IGNORE. Please change to `tryPostBytes` instead. This method will be modified soon in a backwards incompatible way, in order to support additional cases that the current form doesn't support.", replaceWith = ReplaceWith("tryPostBytes(apiPath, body, headers, redirect, abortController, bodySerializer)"))
163177
suspend inline fun <reified B> ApiFetcher.tryPost(
164178
apiPath: String,
165179
body: B,
166180
headers: Map<String, Any>? = FetchDefaults.Headers,
167181
redirect: RequestRedirect? = FetchDefaults.Redirect,
168182
abortController: AbortController? = null,
169183
bodySerializer: SerializationStrategy<B> = serializer(),
184+
): ByteArray? = tryPostBytes(apiPath, body, headers, redirect, abortController, bodySerializer)
185+
186+
/**
187+
* A serialize-friendly version of [tryPost] that expects a body and returns its response as a raw byte array.
188+
*/
189+
suspend inline fun <reified B> ApiFetcher.tryPostBytes(
190+
apiPath: String,
191+
body: B,
192+
headers: Map<String, Any>? = FetchDefaults.Headers,
193+
redirect: RequestRedirect? = FetchDefaults.Redirect,
194+
abortController: AbortController? = null,
195+
bodySerializer: SerializationStrategy<B> = serializer(),
170196
): ByteArray? {
171-
return tryPost(
197+
return tryPostBytes(
172198
apiPath,
173199
headers,
174200
Json.encodeToString(bodySerializer, body).encodeToByteArray(),
@@ -187,7 +213,7 @@ suspend inline fun <reified R> ApiFetcher.tryPost(
187213
abortController: AbortController? = null,
188214
responseDeserializer: DeserializationStrategy<R> = serializer(),
189215
): R? {
190-
val responseBytes = tryPost(
216+
val responseBytes = tryPostBytes(
191217
apiPath,
192218
headers,
193219
body = null,
@@ -222,7 +248,7 @@ suspend inline fun <reified B, reified R> ApiFetcher.put(
222248
bodySerializer: SerializationStrategy<B> = serializer(),
223249
responseDeserializer: DeserializationStrategy<R> = serializer()
224250
): R {
225-
val responseBytes = put(
251+
val responseBytes = putBytes(
226252
apiPath,
227253
body,
228254
mapOf("Content-type" to "application/json") + headers.orEmpty(),
@@ -239,15 +265,28 @@ suspend inline fun <reified B, reified R> ApiFetcher.put(
239265
/**
240266
* A serialize-friendly version of [put] that expects a body but does not expect a serialized response.
241267
*/
268+
@Deprecated("DO NOT IGNORE. Please change to `putBytes` instead. This method will be modified soon in a backwards incompatible way, in order to support additional cases that the current form doesn't support.", replaceWith = ReplaceWith("putBytes(apiPath, body, headers, redirect, abortController, bodySerializer)"))
242269
suspend inline fun <reified B> ApiFetcher.put(
243270
apiPath: String,
244271
body: B,
245272
headers: Map<String, Any>? = FetchDefaults.Headers,
246273
redirect: RequestRedirect? = FetchDefaults.Redirect,
247274
abortController: AbortController? = null,
248275
bodySerializer: SerializationStrategy<B> = serializer(),
276+
): ByteArray = putBytes(apiPath, body, headers, redirect, abortController, bodySerializer)
277+
278+
/**
279+
* A serialize-friendly version of [put] that expects a body and returns its response as a raw byte array.
280+
*/
281+
suspend inline fun <reified B> ApiFetcher.putBytes(
282+
apiPath: String,
283+
body: B,
284+
headers: Map<String, Any>? = FetchDefaults.Headers,
285+
redirect: RequestRedirect? = FetchDefaults.Redirect,
286+
abortController: AbortController? = null,
287+
bodySerializer: SerializationStrategy<B> = serializer(),
249288
): ByteArray {
250-
return put(
289+
return putBytes(
251290
apiPath,
252291
mapOf("Content-type" to "application/json") + headers.orEmpty(),
253292
Json.encodeToString(bodySerializer, body).encodeToByteArray(),
@@ -266,7 +305,7 @@ suspend inline fun <reified R> ApiFetcher.put(
266305
abortController: AbortController? = null,
267306
responseDeserializer: DeserializationStrategy<R> = serializer(),
268307
): R {
269-
val responseBytes = put(
308+
val responseBytes = putBytes(
270309
apiPath,
271310
headers,
272311
body = null,
@@ -295,7 +334,7 @@ suspend inline fun <reified B, reified R> ApiFetcher.tryPut(
295334
bodySerializer: SerializationStrategy<B> = serializer(),
296335
responseDeserializer: DeserializationStrategy<R> = serializer()
297336
): R? {
298-
val responseBytes = tryPut(
337+
val responseBytes = tryPutBytes(
299338
apiPath,
300339
body,
301340
mapOf("Content-type" to "application/json") + headers.orEmpty(),
@@ -313,15 +352,28 @@ suspend inline fun <reified B, reified R> ApiFetcher.tryPut(
313352
/**
314353
* A serialize-friendly version of [tryPut] that expects a body but does not expect a serialized response.
315354
*/
355+
@Deprecated("DO NOT IGNORE. Please change to `tryPutBytes` instead. This method will be modified soon in a backwards incompatible way, in order to support additional cases that the current form doesn't support.", replaceWith = ReplaceWith("tryPutBytes(apiPath, body, headers, redirect, abortController, bodySerializer)"))
316356
suspend inline fun <reified B> ApiFetcher.tryPut(
317357
apiPath: String,
318358
body: B,
319359
headers: Map<String, Any>? = FetchDefaults.Headers,
320360
redirect: RequestRedirect? = FetchDefaults.Redirect,
321361
abortController: AbortController? = null,
322362
bodySerializer: SerializationStrategy<B> = serializer(),
363+
): ByteArray? = tryPutBytes(apiPath, body, headers, redirect, abortController, bodySerializer)
364+
365+
/**
366+
* A serialize-friendly version of [tryPut] that expects a body and returns its response as a raw byte array.
367+
*/
368+
suspend inline fun <reified B> ApiFetcher.tryPutBytes(
369+
apiPath: String,
370+
body: B,
371+
headers: Map<String, Any>? = FetchDefaults.Headers,
372+
redirect: RequestRedirect? = FetchDefaults.Redirect,
373+
abortController: AbortController? = null,
374+
bodySerializer: SerializationStrategy<B> = serializer(),
323375
): ByteArray? {
324-
return tryPut(
376+
return tryPutBytes(
325377
apiPath,
326378
headers,
327379
Json.encodeToString(bodySerializer, body).encodeToByteArray(),
@@ -340,7 +392,7 @@ suspend inline fun <reified R> ApiFetcher.tryPut(
340392
abortController: AbortController? = null,
341393
responseDeserializer: DeserializationStrategy<R> = serializer(),
342394
): R? {
343-
val responseBytes = tryPut(
395+
val responseBytes = tryPutBytes(
344396
apiPath,
345397
headers,
346398
body = null,
@@ -374,7 +426,7 @@ suspend inline fun <reified B, reified R> ApiFetcher.patch(
374426
bodySerializer: SerializationStrategy<B> = serializer(),
375427
responseDeserializer: DeserializationStrategy<R> = serializer()
376428
): R {
377-
val responseBytes = patch(
429+
val responseBytes = patchBytes(
378430
apiPath,
379431
body,
380432
mapOf("Content-type" to "application/json") + headers.orEmpty(),
@@ -391,15 +443,28 @@ suspend inline fun <reified B, reified R> ApiFetcher.patch(
391443
/**
392444
* A serialize-friendly version of [patch] that expects a body but does not expect a serialized response.
393445
*/
446+
@Deprecated("DO NOT IGNORE. Please change to `patchBytes` instead. This method will be modified soon in a backwards incompatible way, in order to support additional cases that the current form doesn't support.", replaceWith = ReplaceWith("patchBytes(apiPath, body, headers, redirect, abortController, bodySerializer)"))
394447
suspend inline fun <reified B> ApiFetcher.patch(
395448
apiPath: String,
396449
body: B,
397450
headers: Map<String, Any>? = FetchDefaults.Headers,
398451
redirect: RequestRedirect? = FetchDefaults.Redirect,
399452
abortController: AbortController? = null,
400453
bodySerializer: SerializationStrategy<B> = serializer(),
454+
): ByteArray = patchBytes(apiPath, body, headers, redirect, abortController, bodySerializer)
455+
456+
/**
457+
* A serialize-friendly version of [patch] that expects a body and returns its response as a raw byte array.
458+
*/
459+
suspend inline fun <reified B> ApiFetcher.patchBytes(
460+
apiPath: String,
461+
body: B,
462+
headers: Map<String, Any>? = FetchDefaults.Headers,
463+
redirect: RequestRedirect? = FetchDefaults.Redirect,
464+
abortController: AbortController? = null,
465+
bodySerializer: SerializationStrategy<B> = serializer(),
401466
): ByteArray {
402-
return patch(
467+
return patchBytes(
403468
apiPath,
404469
mapOf("Content-type" to "application/json") + headers.orEmpty(),
405470
Json.encodeToString(bodySerializer, body).encodeToByteArray(),
@@ -418,7 +483,7 @@ suspend inline fun <reified R> ApiFetcher.patch(
418483
abortController: AbortController? = null,
419484
responseDeserializer: DeserializationStrategy<R> = serializer(),
420485
): R {
421-
val responseBytes = patch(
486+
val responseBytes = patchBytes(
422487
apiPath,
423488
headers,
424489
body = null,
@@ -447,7 +512,7 @@ suspend inline fun <reified B, reified R> ApiFetcher.tryPatch(
447512
bodySerializer: SerializationStrategy<B> = serializer(),
448513
responseDeserializer: DeserializationStrategy<R> = serializer()
449514
): R? {
450-
val responseBytes = tryPatch(
515+
val responseBytes = tryPatchBytes(
451516
apiPath,
452517
body,
453518
mapOf("Content-type" to "application/json") + headers.orEmpty(),
@@ -465,15 +530,28 @@ suspend inline fun <reified B, reified R> ApiFetcher.tryPatch(
465530
/**
466531
* A serialize-friendly version of [tryPatch] that expects a body but does not expect a serialized response.
467532
*/
533+
@Deprecated("DO NOT IGNORE. Please change to `tryPatchBytes` instead. This method will be modified soon in a backwards incompatible way, in order to support additional cases that the current form doesn't support.", replaceWith = ReplaceWith("tryPatchBytes(apiPath, body, headers, redirect, abortController, bodySerializer)"))
468534
suspend inline fun <reified B> ApiFetcher.tryPatch(
469535
apiPath: String,
470536
body: B,
471537
headers: Map<String, Any>? = FetchDefaults.Headers,
472538
redirect: RequestRedirect? = FetchDefaults.Redirect,
473539
abortController: AbortController? = null,
474540
bodySerializer: SerializationStrategy<B> = serializer(),
541+
): ByteArray? = tryPatchBytes(apiPath, body, headers, redirect, abortController, bodySerializer)
542+
543+
/**
544+
* A serialize-friendly version of [tryPatch] that expects a body and returns its response as a raw byte array.
545+
*/
546+
suspend inline fun <reified B> ApiFetcher.tryPatchBytes(
547+
apiPath: String,
548+
body: B,
549+
headers: Map<String, Any>? = FetchDefaults.Headers,
550+
redirect: RequestRedirect? = FetchDefaults.Redirect,
551+
abortController: AbortController? = null,
552+
bodySerializer: SerializationStrategy<B> = serializer(),
475553
): ByteArray? {
476-
return tryPatch(
554+
return tryPatchBytes(
477555
apiPath,
478556
headers,
479557
Json.encodeToString(bodySerializer, body).encodeToByteArray(),
@@ -492,7 +570,7 @@ suspend inline fun <reified R> ApiFetcher.tryPatch(
492570
abortController: AbortController? = null,
493571
responseDeserializer: DeserializationStrategy<R> = serializer(),
494572
): R? {
495-
val responseBytes = tryPatch(
573+
val responseBytes = tryPatchBytes(
496574
apiPath,
497575
headers,
498576
body = null,
@@ -521,7 +599,7 @@ suspend inline fun <reified R> ApiFetcher.delete(
521599
abortController: AbortController? = null,
522600
responseDeserializer: DeserializationStrategy<R> = serializer(),
523601
): R {
524-
val responseBytes = delete(
602+
val responseBytes = deleteBytes(
525603
apiPath,
526604
headers,
527605
redirect,
@@ -541,7 +619,7 @@ suspend inline fun <reified R> ApiFetcher.tryDelete(
541619
abortController: AbortController? = null,
542620
responseDeserializer: DeserializationStrategy<R> = serializer(),
543621
): R? {
544-
val responseBytes = tryDelete(
622+
val responseBytes = tryDeleteBytes(
545623
apiPath,
546624
headers,
547625
redirect,

0 commit comments

Comments
 (0)