mirror of
https://github.com/c-cube/ezcurl.git
synced 2025-12-06 11:15:44 -05:00
doc: use mdx to update the readme (and test)
This commit is contained in:
parent
bf31f32a5b
commit
d6c687d16c
4 changed files with 79 additions and 0 deletions
67
README.md
67
README.md
|
|
@ -1,3 +1,70 @@
|
||||||
# EZCurl
|
# EZCurl
|
||||||
|
|
||||||
A simple wrapper around OCurl, for easy tasks around http.
|
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
10
dune
Normal 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))))
|
||||||
|
|
||||||
|
|
@ -18,6 +18,7 @@ depends: [
|
||||||
"lwt"
|
"lwt"
|
||||||
"dune" {build}
|
"dune" {build}
|
||||||
"odoc" {with-doc}
|
"odoc" {with-doc}
|
||||||
|
"mdx" {with-test}
|
||||||
"ocaml" { >= "4.03.0" }
|
"ocaml" { >= "4.03.0" }
|
||||||
]
|
]
|
||||||
tags: [ "curl" "web" "http" "client" "lwt" ]
|
tags: [ "curl" "web" "http" "client" "lwt" ]
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
(library
|
(library
|
||||||
(name ezcurl)
|
(name ezcurl)
|
||||||
|
(public_name ezcurl)
|
||||||
(flags :standard -warn-error -32)
|
(flags :standard -warn-error -32)
|
||||||
(libraries curl ezcurl.core))
|
(libraries curl ezcurl.core))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue