1

There are a type and a task

type Msg
  = Fail String
  | Success (Int, Int)


makeRequest =
  let
    req =
      { verb = "GET"
      , headers = []
      , url = "http://localhost:8080"
      , body = empty
      }
  in
    Task.perform Fail Success <| send defaultSettings req

The argument of Fail constructor is for error message (just "Error"), first argument of Succeess is for status from Http.Response, second is for size of value from Http.Response.

How to convert Task Http.RawError Http.Response to Task String (Int, Int)?

I'm looking at Task.map and Tsk.mapError and I don't understand how to combine them. Am I on a right way?

ztsu
  • 25
  • 7

1 Answers1

3

Yes, you can use Task.map and Task.mapError to achieve your results.

First off, you'll need a way to determine the size of your Http Response. Since it can be either a string or binary blob, and blob is not yet supported, you could define a function like this:

httpValueSize : Http.Value -> Int
httpValueSize val =
  case val of
    Text str -> String.length str
    Blob blob -> Debug.crash "Blobs have no implementation yet"

Now you can use the mapping functions in your task like this:

send defaultSettings req
  |> Task.map (\r -> (r.status, httpValueSize r.value))
  |> Task.mapError (always "Error")
  |> Task.perform Fail Success

You could also do this without the mapping functions like so:

send defaultSettings req
  |> Task.perform (always <| Fail "Error") (\r -> Success (r.status, httpValueSize r.value))
Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97