@@ -4,6 +4,7 @@ module Engage.Http exposing
4
4
, requestJson, requestString, requestAnything
5
5
, getErrorMessage, urlWithQueryString
6
6
, configDecoder, serverErrorDecoder, multipleServerErrorDecoder, nullDecoder
7
+ , requestBytes
7
8
)
8
9
9
10
{- | Helpers for working with DNN Web API
@@ -21,7 +22,7 @@ module Engage.Http exposing
21
22
22
23
# Raw requests
23
24
24
- @docs requestJson, requestString, requestAnything
25
+ @docs requestJson, requestString, requestAnything, requestBytes
25
26
26
27
27
28
# Helper functions
@@ -35,6 +36,8 @@ module Engage.Http exposing
35
36
36
37
-}
37
38
39
+ import Bytes exposing (Bytes )
40
+ import Bytes.Decode
38
41
import Engage.Localization as Localization exposing (Localization )
39
42
import Http
40
43
import Json.Decode as Decode
@@ -243,6 +246,52 @@ requestJson method headers url requestBody toMsg decoder =
243
246
requestString method headers url requestBody toMsg toResult
244
247
245
248
249
+ {- | Raw request that expects a Bytes response.
250
+
251
+ This version can use any method and accepts any body (e.g. `Http.fileBody`, `Http.bytesBody`, or `Http.emptyBody`).
252
+
253
+ -}
254
+ requestBytes : String -> List Http .Header -> String -> Http .Body -> (RemoteData Error Bytes -> msg ) -> Cmd msg
255
+ requestBytes method headers url requestBody toMsg =
256
+ let
257
+ sizedStringDecoder : Bytes . Decode . Decoder String
258
+ sizedStringDecoder =
259
+ Bytes . Decode . unsignedInt32 Bytes . BE
260
+ |> Bytes . Decode . andThen Bytes . Decode . string
261
+
262
+ toResult : Http . Response Bytes -> Result Error Bytes
263
+ toResult response =
264
+ case response of
265
+ Http . GoodStatus_ _ body ->
266
+ Ok body
267
+
268
+ Http . BadStatus_ { statusCode } body ->
269
+ body
270
+ |> Bytes . Decode . decode sizedStringDecoder
271
+ |> Maybe . withDefault ( " An unknown error occurred decoding the " ++ String . fromInt statusCode ++ " response" )
272
+ |> BadStatus statusCode
273
+ |> Err
274
+
275
+ Http . NetworkError_ ->
276
+ Err NetworkError
277
+
278
+ Http . Timeout_ ->
279
+ Err Timeout
280
+
281
+ Http . BadUrl_ badUrl ->
282
+ Err ( BadUrl badUrl)
283
+ in
284
+ Http . request
285
+ { method = method
286
+ , headers = headers
287
+ , url = url
288
+ , body = requestBody
289
+ , expect = Http . expectBytesResponse ( RemoteData . fromResult >> toMsg) toResult
290
+ , timeout = Nothing
291
+ , tracker = Nothing
292
+ }
293
+
294
+
246
295
{- | Raw request that does not expect a response.
247
296
248
297
This version can use any method and accepts any body (e.g. `Http.fileBody`, `Http.bytesBody`, or `Http.emptyBody`).
0 commit comments