Skip to content

Commit 13e02dd

Browse files
committed
Add requestBytes
1 parent 847385c commit 13e02dd

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

elm.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"elm-version": "0.19.0 <= v < 0.20.0",
1111
"dependencies": {
1212
"EngageSoftware/elm-dnn-localization": "1.0.2 <= v < 2.0.0",
13+
"elm/bytes": "1.0.8 <= v < 2.0.0",
1314
"elm/core": "1.0.0 <= v < 2.0.0",
1415
"elm/http": "2.0.0 <= v < 3.0.0",
1516
"elm/json": "1.1.3 <= v < 2.0.0",

src/Engage/Http.elm

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Engage.Http exposing
44
, requestJson, requestString, requestAnything
55
, getErrorMessage, urlWithQueryString
66
, configDecoder, serverErrorDecoder, multipleServerErrorDecoder, nullDecoder
7+
, requestBytes
78
)
89

910
{-| Helpers for working with DNN Web API
@@ -21,7 +22,7 @@ module Engage.Http exposing
2122
2223
# Raw requests
2324
24-
@docs requestJson, requestString, requestAnything
25+
@docs requestJson, requestString, requestAnything, requestBytes
2526
2627
2728
# Helper functions
@@ -35,6 +36,8 @@ module Engage.Http exposing
3536
3637
-}
3738

39+
import Bytes exposing (Bytes)
40+
import Bytes.Decode
3841
import Engage.Localization as Localization exposing (Localization)
3942
import Http
4043
import Json.Decode as Decode
@@ -243,6 +246,52 @@ requestJson method headers url requestBody toMsg decoder =
243246
requestString method headers url requestBody toMsg toResult
244247

245248

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+
246295
{-| Raw request that does not expect a response.
247296
248297
This version can use any method and accepts any body (e.g. `Http.fileBody`, `Http.bytesBody`, or `Http.emptyBody`).

0 commit comments

Comments
 (0)