package ezcurl-lwt

  1. Overview
  2. Docs
Friendly wrapper around OCurl, Lwt version

Install

Dune Dependency

Authors

Maintainers

Sources

v0.1.tar.gz
md5=e2213c3b66680ebaa4572660b8de4756
sha512=b40824b7a4d38b7082884f265918b4100a1492548ca37d1b495bffee15ea6743e3557d70b524a3a56a7c4504678becc16a3f40e7d5054c3d81ec2ee831f4adda

Description

Tags

curl web http client lwt

Published: 30 Jul 2021

README

EZCurl

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-baed 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.

# #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 : 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:

# #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]

Dependencies (5)

  1. ocaml >= "4.03.0"
  2. dune >= "1.0"
  3. lwt
  4. ezcurl = version
  5. ocurl

Dev Dependencies (2)

  1. mdx with-test
  2. odoc with-doc

Used by (2)

  1. fred
  2. openai

Conflicts

None

OCaml

Innovation. Community. Security.