mirror of
https://github.com/c-cube/ezcurl.git
synced 2025-12-06 03:05:31 -05:00
feat: add more methods, add range parameter
This commit is contained in:
parent
637674f6d0
commit
54e7d1740f
2 changed files with 122 additions and 8 deletions
|
|
@ -141,11 +141,23 @@ type meth =
|
||||||
| GET
|
| GET
|
||||||
| POST of Curl.curlHTTPPost list
|
| POST of Curl.curlHTTPPost list
|
||||||
| PUT
|
| PUT
|
||||||
|
| DELETE
|
||||||
|
| HEAD
|
||||||
|
| CONNECT
|
||||||
|
| OPTIONS
|
||||||
|
| TRACE
|
||||||
|
| PATCH
|
||||||
|
|
||||||
let string_of_meth = function
|
let string_of_meth = function
|
||||||
| GET -> "GET"
|
| GET -> "GET"
|
||||||
| POST _ -> "POST"
|
| POST _ -> "POST"
|
||||||
| PUT -> "PUT"
|
| PUT -> "PUT"
|
||||||
|
| DELETE -> "DELETE"
|
||||||
|
| HEAD -> "HEAD"
|
||||||
|
| CONNECT -> "CONNECT"
|
||||||
|
| OPTIONS -> "OPTIONS"
|
||||||
|
| TRACE -> "TRACE"
|
||||||
|
| PATCH -> "PATCH"
|
||||||
|
|
||||||
let pp_meth out m = Format.pp_print_string out (string_of_meth m)
|
let pp_meth out m = Format.pp_print_string out (string_of_meth m)
|
||||||
|
|
||||||
|
|
@ -161,17 +173,44 @@ end
|
||||||
module type S = sig
|
module type S = sig
|
||||||
type 'a io
|
type 'a io
|
||||||
|
|
||||||
|
|
||||||
val http :
|
val http :
|
||||||
?tries:int ->
|
?tries:int ->
|
||||||
?client:t ->
|
?client:t ->
|
||||||
?config:Config.t ->
|
?config:Config.t ->
|
||||||
|
?range:string ->
|
||||||
?headers:(string*string) list ->
|
?headers:(string*string) list ->
|
||||||
url:string ->
|
url:string ->
|
||||||
meth:meth ->
|
meth:meth ->
|
||||||
unit ->
|
unit ->
|
||||||
(response, Curl.curlCode * string) result io
|
(response, Curl.curlCode * string) result io
|
||||||
|
(** General purpose HTTP call via cURL.
|
||||||
|
@param url the URL to query
|
||||||
|
@param meth which method to use (see {!meth})
|
||||||
|
@param tries how many times to retry in case of [CURLE_AGAIN] code
|
||||||
|
@param client a client to reuse (instead of allocating a new one)
|
||||||
|
@param range an optional
|
||||||
|
{{: https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests} byte range}
|
||||||
|
to fetch (either to get large pages
|
||||||
|
by chunks, or to resume an interrupted download).
|
||||||
|
@param config configuration to set
|
||||||
|
@param headers headers of the query
|
||||||
|
*)
|
||||||
|
|
||||||
val get :
|
val get :
|
||||||
|
?tries:int ->
|
||||||
|
?client:t ->
|
||||||
|
?config:Config.t ->
|
||||||
|
?range:string ->
|
||||||
|
?headers:(string*string) list ->
|
||||||
|
url:string ->
|
||||||
|
unit ->
|
||||||
|
(response, Curl.curlCode * string) result io
|
||||||
|
(** Shortcut for [http ~meth:GET]
|
||||||
|
See {!http} for more info.
|
||||||
|
*)
|
||||||
|
|
||||||
|
val put :
|
||||||
?tries:int ->
|
?tries:int ->
|
||||||
?client:t ->
|
?client:t ->
|
||||||
?config:Config.t ->
|
?config:Config.t ->
|
||||||
|
|
@ -179,6 +218,22 @@ module type S = sig
|
||||||
url:string ->
|
url:string ->
|
||||||
unit ->
|
unit ->
|
||||||
(response, Curl.curlCode * string) result io
|
(response, Curl.curlCode * string) result io
|
||||||
|
(** Shortcut for [http ~meth:PUT]
|
||||||
|
See {!http} for more info.
|
||||||
|
*)
|
||||||
|
|
||||||
|
val post :
|
||||||
|
?tries:int ->
|
||||||
|
?client:t ->
|
||||||
|
?config:Config.t ->
|
||||||
|
?headers:(string*string) list ->
|
||||||
|
params:Curl.curlHTTPPost list ->
|
||||||
|
url:string ->
|
||||||
|
unit ->
|
||||||
|
(response, Curl.curlCode * string) result io
|
||||||
|
(** Shortcut for [http ~meth:(POST params)]
|
||||||
|
See {!http} for more info.
|
||||||
|
*)
|
||||||
end
|
end
|
||||||
|
|
||||||
exception Parse_error of Curl.curlCode * string
|
exception Parse_error of Curl.curlCode * string
|
||||||
|
|
@ -208,14 +263,14 @@ let mk_res (self:t) headers body : (response,_) result =
|
||||||
Error (e, Curl.strerror e ^ ": " ^ msg)
|
Error (e, Curl.strerror e ^ ": " ^ msg)
|
||||||
|
|
||||||
module Make(IO : IO)
|
module Make(IO : IO)
|
||||||
(* : S with module IO = IO *)
|
: S with type 'a io = 'a IO.t
|
||||||
= struct
|
= struct
|
||||||
open IO
|
open IO
|
||||||
|
|
||||||
type 'a io = 'a IO.t
|
type 'a io = 'a IO.t
|
||||||
|
|
||||||
let http
|
let http
|
||||||
?(tries=1) ?client ?(config=Config.default) ?(headers=[]) ~url ~meth ()
|
?(tries=1) ?client ?(config=Config.default) ?range ?(headers=[]) ~url ~meth ()
|
||||||
: _ result io =
|
: _ result io =
|
||||||
let do_cleanup, self = match client with
|
let do_cleanup, self = match client with
|
||||||
| None -> true, make()
|
| None -> true, make()
|
||||||
|
|
@ -224,6 +279,7 @@ module Make(IO : IO)
|
||||||
false, c
|
false, c
|
||||||
in
|
in
|
||||||
_apply_config self config;
|
_apply_config self config;
|
||||||
|
opt_iter range ~f:(fun s -> Curl.set_range self s);
|
||||||
(* local state *)
|
(* local state *)
|
||||||
let tries = max tries 1 in (* at least one attempt *)
|
let tries = max tries 1 in (* at least one attempt *)
|
||||||
let body = ref "" in
|
let body = ref "" in
|
||||||
|
|
@ -234,6 +290,12 @@ module Make(IO : IO)
|
||||||
| POST l -> Curl.set_httppost self l;
|
| POST l -> Curl.set_httppost self l;
|
||||||
| GET -> Curl.set_httpget self true;
|
| GET -> Curl.set_httpget self true;
|
||||||
| PUT -> Curl.set_put self true;
|
| PUT -> Curl.set_put self true;
|
||||||
|
| DELETE -> Curl.set_customrequest self "DELETE";
|
||||||
|
| HEAD -> Curl.set_customrequest self "HEAD"
|
||||||
|
| CONNECT -> Curl.set_customrequest self "CONNECT"
|
||||||
|
| OPTIONS -> Curl.set_customrequest self "OPTIONS"
|
||||||
|
| TRACE -> Curl.set_customrequest self "TRACE"
|
||||||
|
| PATCH -> Curl.set_customrequest self "PATCH"
|
||||||
end;
|
end;
|
||||||
_set_headers self headers;
|
_set_headers self headers;
|
||||||
Curl.set_headerfunction self
|
Curl.set_headerfunction self
|
||||||
|
|
@ -270,11 +332,12 @@ module Make(IO : IO)
|
||||||
in
|
in
|
||||||
loop tries
|
loop tries
|
||||||
|
|
||||||
let get ?tries ?client ?config ?headers ~url () : _ result io =
|
let get ?tries ?client ?config ?range ?headers ~url () : _ result io =
|
||||||
http ?tries ?client ?config ?headers ~url ~meth:GET ()
|
http ?tries ?client ?config ?range ?headers ~url ~meth:GET ()
|
||||||
|
|
||||||
(* TODO
|
let post ?tries ?client ?config ?headers ~params ~url () : _ result io =
|
||||||
let post ?verbose ?tries ?client ?auth ?username ?password ~url () : _ result =
|
http ?tries ?client ?config ?headers ~url ~meth:(POST params) ()
|
||||||
call ?verbose ?tries ?client ?auth ?username ?password ~url ~meth:GET ()
|
|
||||||
*)
|
let put ?tries ?client ?config ?headers ~url () : _ result io =
|
||||||
|
http ?tries ?client ?config ?headers ~url ~meth:PUT ()
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -50,10 +50,18 @@ type response = {
|
||||||
val pp_response : Format.formatter -> response -> unit
|
val pp_response : Format.formatter -> response -> unit
|
||||||
val string_of_response : response -> string
|
val string_of_response : response -> string
|
||||||
|
|
||||||
|
(** The {{: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods} HTTP method}
|
||||||
|
to use *)
|
||||||
type meth =
|
type meth =
|
||||||
| GET
|
| GET
|
||||||
| POST of Curl.curlHTTPPost list
|
| POST of Curl.curlHTTPPost list
|
||||||
| PUT
|
| PUT
|
||||||
|
| DELETE
|
||||||
|
| HEAD
|
||||||
|
| CONNECT
|
||||||
|
| OPTIONS
|
||||||
|
| TRACE
|
||||||
|
| PATCH
|
||||||
|
|
||||||
val pp_meth : Format.formatter -> meth -> unit
|
val pp_meth : Format.formatter -> meth -> unit
|
||||||
val string_of_meth : meth -> string
|
val string_of_meth : meth -> string
|
||||||
|
|
@ -72,17 +80,44 @@ end
|
||||||
module type S = sig
|
module type S = sig
|
||||||
type 'a io
|
type 'a io
|
||||||
|
|
||||||
|
|
||||||
val http :
|
val http :
|
||||||
?tries:int ->
|
?tries:int ->
|
||||||
?client:t ->
|
?client:t ->
|
||||||
?config:Config.t ->
|
?config:Config.t ->
|
||||||
|
?range:string ->
|
||||||
?headers:(string*string) list ->
|
?headers:(string*string) list ->
|
||||||
url:string ->
|
url:string ->
|
||||||
meth:meth ->
|
meth:meth ->
|
||||||
unit ->
|
unit ->
|
||||||
(response, Curl.curlCode * string) result io
|
(response, Curl.curlCode * string) result io
|
||||||
|
(** General purpose HTTP call via cURL.
|
||||||
|
@param url the URL to query
|
||||||
|
@param meth which method to use (see {!meth})
|
||||||
|
@param tries how many times to retry in case of [CURLE_AGAIN] code
|
||||||
|
@param client a client to reuse (instead of allocating a new one)
|
||||||
|
@param range an optional
|
||||||
|
{{: https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests} byte range}
|
||||||
|
to fetch (either to get large pages
|
||||||
|
by chunks, or to resume an interrupted download).
|
||||||
|
@param config configuration to set
|
||||||
|
@param headers headers of the query
|
||||||
|
*)
|
||||||
|
|
||||||
val get :
|
val get :
|
||||||
|
?tries:int ->
|
||||||
|
?client:t ->
|
||||||
|
?config:Config.t ->
|
||||||
|
?range:string ->
|
||||||
|
?headers:(string*string) list ->
|
||||||
|
url:string ->
|
||||||
|
unit ->
|
||||||
|
(response, Curl.curlCode * string) result io
|
||||||
|
(** Shortcut for [http ~meth:GET]
|
||||||
|
See {!http} for more info.
|
||||||
|
*)
|
||||||
|
|
||||||
|
val put :
|
||||||
?tries:int ->
|
?tries:int ->
|
||||||
?client:t ->
|
?client:t ->
|
||||||
?config:Config.t ->
|
?config:Config.t ->
|
||||||
|
|
@ -90,6 +125,22 @@ module type S = sig
|
||||||
url:string ->
|
url:string ->
|
||||||
unit ->
|
unit ->
|
||||||
(response, Curl.curlCode * string) result io
|
(response, Curl.curlCode * string) result io
|
||||||
|
(** Shortcut for [http ~meth:PUT]
|
||||||
|
See {!http} for more info.
|
||||||
|
*)
|
||||||
|
|
||||||
|
val post :
|
||||||
|
?tries:int ->
|
||||||
|
?client:t ->
|
||||||
|
?config:Config.t ->
|
||||||
|
?headers:(string*string) list ->
|
||||||
|
params:Curl.curlHTTPPost list ->
|
||||||
|
url:string ->
|
||||||
|
unit ->
|
||||||
|
(response, Curl.curlCode * string) result io
|
||||||
|
(** Shortcut for [http ~meth:(POST params)]
|
||||||
|
See {!http} for more info.
|
||||||
|
*)
|
||||||
end
|
end
|
||||||
|
|
||||||
module Make(IO : IO) : S with type 'a io = 'a IO.t
|
module Make(IO : IO) : S with type 'a io = 'a IO.t
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue