diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 448c07f..eb45fed 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -159,6 +159,11 @@ jobs: "@opam/num": "github:dune-universe/num:num.opam#bdb2d7653e927e142b701b51d89f393471279713", "@opam/cmdliner": "github:dune-universe/cmdliner:cmdliner.opam#4fc08affe26642b6285cedc1d2ca7ec03127caf3", "@opam/checkseum": "github:mirage/checkseum:checkseum.opam#a929e09204d2de5626390a9975eb62aefee2824a", + "@opam/http": "github:mirage/ocaml-cohttp:http.opam#2c63ba11309c66a634c3e05e346b7d84be7a0ef2", + "@opam/cohttp": "github:mirage/ocaml-cohttp:cohttp.opam#2c63ba11309c66a634c3e05e346b7d84be7a0ef2", + "@opam/cohttp-lwt": "github:mirage/ocaml-cohttp:cohttp-lwt.opam#2c63ba11309c66a634c3e05e346b7d84be7a0ef2", + "@opam/cohttp-lwt-unix": "github:mirage/ocaml-cohttp:cohttp-lwt-unix.opam#2c63ba11309c66a634c3e05e346b7d84be7a0ef2", + "@opam/clz": "github:mseri/ocaml-clz:clz.opam#965cd934bfa29fc7bde8a082edcd4d7abdcc851c", "@opam/cstruct-sexp": "5.2.0", "@opam/ocamlfind": "1.8.1", "@opam/decompress": "1.4.1" diff --git a/bin/doi2bib.ml b/bin/doi2bib.ml index 8409980..5f8aa48 100644 --- a/bin/doi2bib.ml +++ b/bin/doi2bib.ml @@ -6,18 +6,18 @@ let doi2bib id = match id with | None -> `Help (`Pager, None) | Some id -> ( - match Lwt_main.run (Http.get_bib_entry @@ Parser.parse_id id) with + match Lwt_main.run (Client.get_bib_entry @@ Parser.parse_id id) with | bibtex -> `Ok (Printf.printf "%s" bibtex) - | exception Http.PubMed_DOI_not_found -> + | exception Client.PubMed_DOI_not_found -> err @@ Printf.sprintf "Error: unable to find a DOI entry for %s.\n" id - | exception Http.Entry_not_found -> + | exception Client.Entry_not_found -> err @@ Printf.sprintf "Error: unable to find any bibtex entry for %s.\n\ Check the ID before trying again.\n" id | exception Failure s -> err @@ Printf.sprintf "Unexpected error. %s\n" s - | exception Http.Bad_gateway -> + | exception Client.Bad_gateway -> err @@ Printf.sprintf "Remote server error: wait some time and try again.\n\ diff --git a/doi2bib.opam b/doi2bib.opam index 02271c1..743f35e 100644 --- a/doi2bib.opam +++ b/doi2bib.opam @@ -11,7 +11,8 @@ depends: [ "dune" {>= "2.7"} "ocaml" {>= "4.08"} "astring" {>= "0.8.0"} - "cohttp-lwt-unix" {>= "2.5.0"} + "http" + "cohttp-lwt-unix" "cmdliner" {>= "1.0.0"} "clz" {>= "0.1.0"} "ezxmlm" {>= "1.1.0"} @@ -36,3 +37,9 @@ build: [ ] ] dev-repo: "git+https://github.com/mseri/doi2bib.git" +pin-depends: [ + ["http.dev" "git+https://github.com/mirage/ocaml-cohttp.git"] + ["cohttp.dev" "git+https://github.com/mirage/ocaml-cohttp.git"] + ["cohttp-lwt.dev" "git+https://github.com/mirage/ocaml-cohttp.git"] + ["cohttp-lwt-unix.dev" "git+https://github.com/mirage/ocaml-cohttp.git"] +] diff --git a/doi2bib.opam.template b/doi2bib.opam.template new file mode 100644 index 0000000..f48125c --- /dev/null +++ b/doi2bib.opam.template @@ -0,0 +1,6 @@ +pin-depends: [ + ["http.dev" "git+https://github.com/mirage/ocaml-cohttp.git"] + ["cohttp.dev" "git+https://github.com/mirage/ocaml-cohttp.git"] + ["cohttp-lwt.dev" "git+https://github.com/mirage/ocaml-cohttp.git"] + ["cohttp-lwt-unix.dev" "git+https://github.com/mirage/ocaml-cohttp.git"] +] diff --git a/dune-project b/dune-project index 414d4d8..9410816 100644 --- a/dune-project +++ b/dune-project @@ -24,8 +24,8 @@ (>= 4.08)) (astring (>= 0.8.0)) - (cohttp-lwt-unix - (>= 2.5.0)) + http + cohttp-lwt-unix (cmdliner (>= 1.0.0)) (clz diff --git a/lib/http.ml b/lib/client.ml similarity index 83% rename from lib/http.ml rename to lib/client.ml index 0a09928..53c19ac 100644 --- a/lib/http.ml +++ b/lib/client.ml @@ -6,26 +6,23 @@ exception PubMed_DOI_not_found let rec get ?proxy ?headers ?fallback uri = let headers = Clz_cohttp.update_header headers in - let uri = Option.value ~default:"" proxy ^ uri |> Uri.of_string in + let uri = Option.value ~default:"" proxy ^ uri in let open Lwt.Syntax in - let* resp, body = Cohttp_lwt_unix.Client.get ~headers uri in - let status = Cohttp_lwt.Response.status resp in + let* resp, body = Cohttp_lwt_unix.Client.get ~headers (Uri.of_string uri) in + let status = Http.Response.status resp in let* () = if status <> `OK then Cohttp_lwt.Body.drain_body body else Lwt.return_unit in match status with | `OK -> Clz_cohttp.decompress (resp, body) | `Found -> ( - let uri' = - Cohttp_lwt.(resp |> Response.headers |> Cohttp.Header.get_location) - in + let uri' = Http.(resp |> Response.headers |> Header.get_location) in match (uri', fallback) with - | Some uri, _ -> get ?proxy ~headers ?fallback (Uri.to_string uri) + | Some uri, _ -> get ?proxy ~headers ?fallback uri | None, Some uri -> get ?proxy ~headers uri | None, None -> - Lwt.fail_with - ("Malformed redirection trying to access '" ^ Uri.to_string uri - ^ "'.")) + Lwt.fail_with ("Malformed redirection trying to access '" ^ uri ^ "'.") + ) | d when (d = `Not_found || d = `Gateway_timeout) && Option.is_some fallback -> ( match fallback with @@ -37,7 +34,7 @@ let rec get ?proxy ?headers ?fallback uri = Lwt.fail_with ("Response error: '" ^ Cohttp.Code.string_of_status status - ^ "' trying to access '" ^ Uri.to_string uri ^ "'.") + ^ "' trying to access '" ^ uri ^ "'.") let bib_of_doi ?proxy doi = let uri = diff --git a/lib/doi2bib.ml b/lib/doi2bib.ml index 741e367..1a267c9 100644 --- a/lib/doi2bib.ml +++ b/lib/doi2bib.ml @@ -1,2 +1,2 @@ -module Http = Http +module Client = Client module Parser = Parser diff --git a/lib/dune b/lib/dune index 70ecfe2..00b45cf 100644 --- a/lib/dune +++ b/lib/dune @@ -1,4 +1,4 @@ (library (name doi2bib) - (libraries astring cohttp-lwt-unix clz.cohttp ezxmlm lwt re unix) + (libraries astring cohttp-lwt-unix clz.cohttp http ezxmlm lwt re unix) (preprocess future_syntax)) diff --git a/tests/arxiv.t/run.t b/tests/arxiv.t/run.t index e2d3994..428dfcc 100644 --- a/tests/arxiv.t/run.t +++ b/tests/arxiv.t/run.t @@ -30,11 +30,11 @@ Arxiv Entry (with DOI entry) executed without prefix } Arxiv Entry (without DOI entry) and with old id $ doi2bib arXiv:math/0606217 - @misc{Marklof2006Distribution, - title={Distribution modulo one and Ratner's theorem}, - author={Jens Marklof}, - year={2006}, - eprint={math/0606217}, - archivePrefix={arXiv}, - primaryClass={math.NT} - } + @misc{marklof2006distribution, + title={Distribution modulo one and Ratner's theorem}, + author={Jens Marklof}, + year={2006}, + eprint={math/0606217}, + archivePrefix={arXiv}, + primaryClass={math.NT} + }