Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to what will be cohttp 6 #13

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions bin/doi2bib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down
9 changes: 8 additions & 1 deletion doi2bib.opam
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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"]
]
6 changes: 6 additions & 0 deletions doi2bib.opam.template
Original file line number Diff line number Diff line change
@@ -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"]
]
4 changes: 2 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 8 additions & 11 deletions lib/http.ml → lib/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion lib/doi2bib.ml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module Http = Http
module Client = Client
module Parser = Parser
2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
@@ -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))
16 changes: 8 additions & 8 deletions tests/arxiv.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}