Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New version of HttpFs affects example of the book #9

Open
mariomeyrelles opened this issue Dec 28, 2016 · 3 comments
Open

New version of HttpFs affects example of the book #9

mariomeyrelles opened this issue Dec 28, 2016 · 3 comments

Comments

@mariomeyrelles
Copy link

Good morning Tamizhvendan,

I have been following the examples of the book and thinks have changed a little bit since the edition of the book. Now, HttpFs using Hopac.dll to do its inner workings. This change impacts a little bit on how to make things work. Now there are more abstractions to consider like "Alt", "Job" and so on.

I changed a little bit your example of get request async in order to make thinks work again. In fact, it works but I'm not sure if this is the best way:

let getResponseAsync url = 
    async { 
        let req = Request.create Get (Uri url) |> Request.setHeader (UserAgent "FsharpTest")
        return job { 
                   let! response = getResponse req
                   let output = 
                       match response.statusCode with
                       | 200 -> 
                           let b = Response.readBodyAsString response |> run
                           Ok b
                       | _ -> Error response.statusCode
                   return output
               }
               |> run
    }

let asyncResponseToObservable = getResponseAsync >> Observable.ofAsync

Do you agree? Do you have a better solution?

Kind regards from Brazil.

@tamizhvendan
Copy link
Owner

I was not aware of this @mariomeyrelles and I haven't used the recent version of httpfs. Will check and revert back.

Thanks for the suggestion :)

@mariomeyrelles
Copy link
Author

Hello @tamizhvendan

I think the correct approach to use the new version of HttpFs is like this:

type ApiHttpResponse = 
    | Ok of body : string
    | Error of statusCode : int
    | Exception of e : exn
    
/// Get request Async.
let getAsync urlStr : Async<ApiHttpResponse> = 
    let resp =
        Request.create Get (Uri urlStr)
        |> getResponse
    
    resp
    |> Alt.afterJob (fun resp -> 
        match resp.statusCode with
        | x when x < 300 ->
            resp
            |> Response.readBodyAsString
            |> Job.map Ok
        | x -> Error resp.statusCode |> Job.result)
    |> Alt.toAsync 


/// Post request async, após a ajuda do @haf: https://github.com/haf/Http.fs/issues/117#event-914147149
let postAsync urlStr bodyStr : Async<ApiHttpResponse> = 
    let resp = 
        Request.create Post (Uri urlStr)
        |> Request.bodyString bodyStr
        |> getResponse
    resp
    |> Alt.afterJob (fun resp -> 
           // if we don't cancel the request, let's read the body in full
           match resp.statusCode with
           | x when x < 300 -> 
               resp
               |> Response.readBodyAsString
               |> Job.map Ok
           | x -> Error resp.statusCode |> Job.result)
    |> Alt.toAsync

@tamizhvendan
Copy link
Owner

Thanks, @mariomeyrelles. I will update this in the next update of the book.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants