Last active
March 2, 2017 17:47
-
-
Save lepoetemaudit/a211d621f7b907d6f04c641c157a47de to your computer and use it in GitHub Desktop.
Wrapping hackney in Alpaca
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module hackney | |
export main | |
-- simple result ADT | |
type result 'a 'b = Error 'a | Ok 'b | |
-- define 'bind' for result | |
let (*>>=) a f = | |
match a with | |
| Ok val -> f val | |
| Error _ -> a | |
-- extract body from a valid hackney client ref | |
let read_body ref = | |
beam :hackney :body [ref] with | |
| (:ok, body), is_string body -> | |
Ok body | |
| (:error, err), is_string err -> | |
Error err | |
-- make an HTTP request with hackney | |
let request verb url = | |
beam :hackney :request [verb, url, [], "", []] with | |
| (:ok, _, _, ref) -> | |
Ok ref | |
| (:error, _) -> Error "Couldn't make request" | |
-- Make a GET request and return the body (in a result) | |
let get url = | |
(request :get url) *>>= read_body | |
let print str = | |
match str with | |
s, is_string str -> beam :io :format [str] with _ -> () | |
let main () = | |
print (match get "http://www.google.co.uk" with | |
| Ok val -> val | |
| Error err -> err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment