Skip to content

Instantly share code, notes, and snippets.

@lepoetemaudit
Last active March 2, 2017 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepoetemaudit/a211d621f7b907d6f04c641c157a47de to your computer and use it in GitHub Desktop.
Save lepoetemaudit/a211d621f7b907d6f04c641c157a47de to your computer and use it in GitHub Desktop.
Wrapping hackney in Alpaca
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