Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/c-cube/ezcurl
A simple wrapper around OCurl.
https://github.com/c-cube/ezcurl
curl easy http http-get-it-while-it-s-hot lwt ocaml ocurl
Last synced: 3 months ago
JSON representation
A simple wrapper around OCurl.
- Host: GitHub
- URL: https://github.com/c-cube/ezcurl
- Owner: c-cube
- License: mit
- Created: 2019-09-13T23:22:24.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-03T15:04:59.000Z (3 months ago)
- Last Synced: 2024-10-13T01:46:13.800Z (3 months ago)
- Topics: curl, easy, http, http-get-it-while-it-s-hot, lwt, ocaml, ocurl
- Language: OCaml
- Homepage: https://c-cube.github.io/ezcurl/
- Size: 722 KB
- Stars: 27
- Watchers: 3
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# EZCurl [![build](https://github.com/c-cube/ezcurl/actions/workflows/main.yml/badge.svg)](https://github.com/c-cube/ezcurl/actions/workflows/main.yml)
A simple wrapper around OCurl, for easy tasks around http.
**project goals**
- be as simple to use as possible.
- be as reliable as possible (work is done by cURL and the ocurl bindings anyway).
- stable API with few dependencies, so that user code compiles without breakage
for a long time.## Installation
- for the synchronous library: `opam install ezcurl`
- for the lwt-based library: `opam install ezcurl-lwt` (depends on `ezcurl`)## Usage
A small web crawler can be found in `examples/argiope`. It's very incomplete
and naive but demonstrates basic usage of `Ezcurl_lwt.get`.### 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 ();;
...
# let content = match res with Ok c -> c | Error (_,s) -> failwith s;;
val content : string Ezcurl_core.response =
...# content.Ezcurl.code;;
- : int = 200
```It is also possible to create a client with `Ezcurl.make()` and re-use
it across calls with the optional parameter `client`.### 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"))
;;
...
# codes;;
- : int list = [200; 200; 200]
```