doc: use mdx to update the readme (and test)

This commit is contained in:
Simon Cruanes 2019-09-14 10:07:11 -05:00
parent bf31f32a5b
commit d6c687d16c
4 changed files with 79 additions and 0 deletions

View file

@ -1,3 +1,70 @@
# EZCurl
A simple wrapper around OCurl, for easy tasks around http.
## Installation
- for the synchronous library: `opam install ezcurl`
- for the lwt-baed library: `opam install ezcurl-lwt` (depends on `ezcurl`)
## Usage
### Synchronous API
The library lives in a module `Ezcurl`, which wraps `Curl.t` with functions
such as `get` that combine many different low level API calls into one.
It also follows redirections by default, and returns a `Ezcurl.response`
object that contains the body, headers, and error code.
```ocaml
# #require "ezcurl";;
# let url = "https://curl.haxx.se/";;
val url : string = "https://curl.haxx.se/"
# let res = Ezcurl.get ~url ();;
val res : (Ezcurl_core.response, Curl.curlCode * string) result =
Ok
{Ezcurl_core.code = 200;
headers =
...
# let content = match res with Ok c -> c | Error (_,s) -> failwith s;;
val content : Ezcurl_core.response =
{Ezcurl_core.code = 200;
headers =
...
# content.code;;
- : int = 200
```
### Lwt API
Using `ezcurl-lwt`, a module `Ezcurl_lwt` becomes available, with
functions that are similar to the ones in `Ezcurl` but are non blocking.
This makes it easy to run several queries in parallel:
```ocaml
# #require "ezcurl-lwt";;
# let urls = [
"https://en.wikipedia.org/wiki/CURL";
"https://en.wikipedia.org/wiki/OCaml";
"https://curl.haxx.se/";
];;
val urls : string list =
["https://en.wikipedia.org/wiki/CURL";
"https://en.wikipedia.org/wiki/OCaml"; "https://curl.haxx.se/"]
# open Lwt.Infix;;
# let codes =
List.map (fun url -> Ezcurl_lwt.get ~url ()) urls
|> Lwt_list.map_p
(fun fut ->
fut >>= function
| Ok r -> Lwt.return r.Ezcurl_lwt.code
| Error e -> Lwt.fail (Failure "oh no"))
;;
val codes : int list Ezcurl_lwt.io = <abstr>
# codes;;
- : int list = [200; 200; 200]
```

10
dune Normal file
View file

@ -0,0 +1,10 @@
(alias
(name runtest)
(package ezcurl-lwt)
(deps README.md)
(action
(progn
(run ocaml-mdx test %{deps})
(diff? %{deps} %{deps}.corrected))))

View file

@ -18,6 +18,7 @@ depends: [
"lwt"
"dune" {build}
"odoc" {with-doc}
"mdx" {with-test}
"ocaml" { >= "4.03.0" }
]
tags: [ "curl" "web" "http" "client" "lwt" ]

View file

@ -1,4 +1,5 @@
(library
(name ezcurl)
(public_name ezcurl)
(flags :standard -warn-error -32)
(libraries curl ezcurl.core))