Re: [elm-discuss] http request: How to access the whole response

2016-06-24 Thread germain
Hi William,
 

> The error type has to be "Maybe" because we could hit a network error 
> before we get a response
>

Yes,the Task.andThen error returned value could be with or without 
response! 
The compiler forces us to take into consideration both scenarios => the 
error returned value (what value it could ever be, with or without 
response) should be of identical Type: Maybe Http.Response, Http.Error
We won't later on have some kind of JS troubles like "response is 
undefined" :) 

In the code below you will see that I have used pattern matching to grab 
the relevant fields of the response. It looks natural to me as I am an 
aficionado of Elixir.

I think this post is almost over now. Til now I have hardcoded the body of 
the send request. I should make a form :)  Thanks again !


-- MODEL

type alias Model =
{ error : String
, responseValue: Seat
, responseWithoutValue: responseWithoutValue
 }

type alias Seat =
  { seatNo : Int
  , occupied : Bool
  , id : Int
  }

type alias ResponseWithoutValue =
{ status : Int
, statusText : String
, headers : Dict.Dict String String
, url : String
}


-- JSON DECODER

seatDecoder : JsonDecode.Decoder Seat
seatDecoder =
  let
seat =
  JsonDecode.object3 Seat
("seatNo" := JsonDecode.int)
("occupied" := JsonDecode.bool)
("id" := JsonDecode.int)
  in
-- I use phoenix as backend which wraps the body of the response in a 
Json "data" field 
"data"  := seat


-- UPDATE 

type Msg
= CreateSeat
| PostSucceed (Http.Response, Seat)
| PostFail ( Maybe Http.Response, Http.Error )


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
CreateSeat ->
  (model, postCommand )

PostSucceed  ( { status, statusText, headers, url }, decodedValue ) ->
  (
  { model
| postData = decodedValue
, responseWithoutValue = { status =  status, statusText = 
 statusText, headers =  headers, url =  url }
  }
  , Cmd.none
  )

PostFail errorMessage ->
  ( {  model | error = (toString errorMessage) }, Cmd.none)


Le mercredi 22 juin 2016 00:33:05 UTC+8, William Casarin a écrit :
>
> Hey germain, 
>
> On Mon, Jun 20, 2016 at 11:23 PM, germain  > wrote: 
> > IT'S WORK, thank you so much William ~ 
>
> No problem. 
>
> > - Http.fromJson decoder (Task.succeed response) => why do you use 
> > (Task.succeed response) ? And not just give as argument "response"? 
> > Is it connected with `andThen` and its signature  andThen : Task x a -> 
> (a 
> > -> Task x b) -> Task x b where a is "just" the response of the post 
> request 
> > which must be transformed into a Task by using Task.succeed succeed : a 
> -> 
> > Task x a in order to pass it to the fromJson function which requires 
> > fromJson : Decoder a -> Task RawError Response -> Task Error a ? 
>
> Exactly right, you answered your own question. Since we temporarily 
> extracted 
> the response value using `andThen`, we need to wrap it up with succeed 
> again so 
> that fromJson accepts it. Also the second argument to `andThen` is a 
> function 
> that must return a task, and since `decoded` returns a task, it works out. 
>
> The error type has to be "Maybe" because we could hit a network error 
> before we get a response. 
>
> > - Do you know the difference between Task.Task and Platform.Task? 
>
> They're the same. They're aliased to each other. 
>
> Cheers, 
> William 
>
> --- 
> https://jb55.com 
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] http request: How to access the whole response

2016-06-21 Thread germain
IT'S WORK, thank you so much William ~ 

BUT I HAVE A FEW MORE QUESTIONS:

- Http.fromJson decoder (Task.succeed response) => why do you use 
(Task.succeed response) ? And not just give as argument "response"?
Is it connected with `andThen` and its signature  andThen 
 : 
Task  x 
a -> (a -> Task 
 x b) -> 
Task  x 
b where a is "just" the response of the post request which must be 
transformed into a Task by using Task.succeed succeed 
 : a 
-> Task  
x a in order to pass it to the fromJson function which requires fromJson 
 : 
Decoder a -> Task RawError 
 
Response 
 
-> Task Error 
 a ?

- Do you know the difference between Task.Task and Platform.Task?

I still have to write the "correct" decoder of the result of the 
Http.fromJson :)

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] http request: How to access the whole response

2016-06-16 Thread William Casarin
Hey germain,

On Thu, Jun 16, 2016 at 9:52 AM, germain  wrote:
>
> HOWEVER I just get the value field of the response and nothing else
>
> UnexpectedPayload "Expecting an object with a field named `fakeInt` but
> instead got: {\"data\":{\"seatNo\":100,\"occupied\":true,\"id\":67}}"
>
> What's wrong?

If you're looking to handle UnexpectedPayload messages, you'll need to
include the Response in the error type:


withResponse : JsonDecode.Decoder a
-> Task.Task Http.RawError Http.Response
-> Task.Task (Maybe Http.Response, Http.Error) a
withResponse decoder task =
  let
decoded response = Http.fromJson decoder (Task.succeed response)
  in
-- Task.mapError promoteError task `Task.andThen` decoded
Task.mapError (\rawErr -> (Nothing, promoteError rawErr)) task
`Task.andThen` (\resp -> Task.mapError (\err -> (Just
resp, err)) (decoded resp))


Cheers,
William

---
https://jb55.com

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] http request: How to access the whole response

2016-06-16 Thread germain
post' : String -> Http.Body -> Task.Task RawError Http.Responsepost' url 
body =Http.send Http.defaultSettings{ verb = "POST", headers =[ 
("Content-type", "application/json")], url = url, body = body}

promoteError : RawError -> ErrorpromoteError rawError =  case rawError of  
Http.RawTimeout  -> Http.Timeout  Http.RawNetworkError -> 
Http.NetworkError
withResponse : JsonDecode.Decoder a -> Task.Task Http.RawError 
Http.Response -> Task.Task Http.Error (Http.Response, a)withResponse 
decoder task =  letdecoded response =  Task.map (\val -> (response, 
val)) (Http.fromJson decoder (Task.succeed response))  inTask.mapError 
promoteError task `Task.andThen` decoded


-- a FAKE decoder to get the UnexpectedPayload message :)
fakeDecoder : JsonDecode.Decoder IntfakeDecoder ="fakeInt"  := ( 
JsonDecode.int )


postCommand : Cmd Msg
postCommand =
  let
body = Http.string """{"seat":{"seat_no": 100, "occupied": true}}"""
url = "http://localhost:4000/api/seats;

  in
post' url body
|> withResponse fakeDecoder
|> Task.perform PostFail PostSucceed

type alias MyResponse =
  (Http.Response, Int)


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
 CreateSeat ->
  (model, postCommand )

  PostSucceed  response ->
  ( { model | response = (toString response) },  Cmd.none )
 
  PostFail errorMessage ->
  ( {  model | error = (toString errorMessage) }, Cmd.none)


And then I got the same result, the value of the Http.Response only :
UnexpectedPayload "Expecting an object with a field named `seatNo` but 
instead got: {\"data\":{\"seatNo\":100,\"occupied\":true,\"id\":62}}"

What's wrong?


Le jeudi 16 juin 2016 01:10:02 UTC+8, William Casarin a écrit :
>
> Hey germain, 
>
> On Wed, Jun 15, 2016 at 8:51 AM, germain  > wrote: 
> > Hello, 
> > [..] 
> > Is it possible to retrieve the whole response, for example to retrieve 
> the 
> > status and the value fields? 
>
> So this is a bit ugly because promoteError is not exposed, but it can 
> be accomplished with `Task.andThen`: 
>
>
> promoteError : RawError -> Error 
> promoteError rawError = case rawError of 
>   RawTimeout  -> Timeout 
>   RawNetworkError -> NetworkError 
>
> withResponse : Decoder a -> Task RawError Response -> Task Error 
> (Response, a) 
> withResponse decoder task = 
>   let 
>   decoded resp = Task.map (\val -> (resp, val)) 
>   (Http.fromJson decoder (Task.succeed 
> resp)) 
>   in 
>   Task.mapError promoteError task `Task.andThen` decoded 
>
>
> Cheers, 
> William 
>
> --- 
> https://jb55.com 
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] http request: How to access the whole response

2016-06-16 Thread germain
post' : String -> Http.Body -> Task.Task RawError Http.Responsepost' url 
body =Http.send Http.defaultSettings{ verb = "POST", headers =[ 
("Content-type", "application/json")], url = url, body = body}

promoteError : RawError -> ErrorpromoteError rawError =  case rawError of  
Http.RawTimeout  -> Http.Timeout  Http.RawNetworkError -> 
Http.NetworkError
withResponse : JsonDecode.Decoder a -> Task.Task Http.RawError 
Http.Response -> Task.Task Http.Error (Http.Response, a)withResponse 
decoder task =  letdecoded response =  Task.map (\val -> (response, 
val)) (Http.fromJson decoder (Task.succeed response))  inTask.mapError 
promoteError task `Task.andThen` decoded


-- a FAKE decoder to get the UnexpectedPayload message :)
fakeInt : JsonDecode.Decoder IntfakeInt ="fakeInt"  := ( JsonDecode.int 
)


postCommand : Cmd Msg
postCommand =
  let
body = Http.string """{"seat":{"seat_no": 100, "occupied": true}}"""
url = "http://localhost:4000/api/seats;

  in
post' url body
|> withResponse statusAndValueDecoder
|> Task.perform PostFail PostSucceed

type alias MyResponse =
  (Http.Response, Int)


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
 CreateSeat ->
  (model, postCommand )

  PostSucceed  response ->
  ( { model | response = (toString response) },  Cmd.none )
 
  PostFail errorMessage ->
  ( {  model | error = (toString errorMessage) }, Cmd.none)


And then I got the same result, the value of the Http.Response only :
UnexpectedPayload "Expecting an object with a field named `seatNo` but 
instead got: {\"data\":{\"seatNo\":100,\"occupied\":true,\"id\":62}}"

What's wrong?


>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] http request: How to access the whole response

2016-06-15 Thread germain
Hello,

To make a request,  I chain:
send  
: Settings 
 
-> Request 
 -> 
Task RawError 
 
Response 

and
fromJson 
 : 
Decoder a -> Task RawError 
 
Response 
 
-> Task Error 
 a

It works well (thanks to Simon) however I could not retrieve the whole 
response, only the value field of the response. Because the fromJson 
function only: 

   - Check that the status code is in the 200 range.
   - Make sure the response Value is a string.
   - Convert the string to Elm with the given Decoder.

Right?

type alias Response 
 = { 
status : Int , statusText : String , headers : Dict String String , url : 
String , value : Value 
 }

Is it possible to retrieve the whole response, for example to retrieve the 
status and the value fields?

Thanks,
Germain.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.