feat: add a way to send content

This commit is contained in:
Simon Cruanes 2019-09-14 12:50:33 -05:00
parent 8ee9668441
commit fa37890804
2 changed files with 17 additions and 3 deletions

View file

@ -179,6 +179,7 @@ module type S = sig
?client:t -> ?client:t ->
?config:Config.t -> ?config:Config.t ->
?range:string -> ?range:string ->
?content:string ->
?headers:(string*string) list -> ?headers:(string*string) list ->
url:string -> url:string ->
meth:meth -> meth:meth ->
@ -194,6 +195,7 @@ module type S = sig
to fetch (either to get large pages to fetch (either to get large pages
by chunks, or to resume an interrupted download). by chunks, or to resume an interrupted download).
@param config configuration to set @param config configuration to set
@param content the content to send as the query's body
@param headers headers of the query @param headers headers of the query
*) *)
@ -216,6 +218,7 @@ module type S = sig
?config:Config.t -> ?config:Config.t ->
?headers:(string*string) list -> ?headers:(string*string) list ->
url:string -> url:string ->
content:string ->
unit -> unit ->
(response, Curl.curlCode * string) result io (response, Curl.curlCode * string) result io
(** Shortcut for [http ~meth:PUT] (** Shortcut for [http ~meth:PUT]
@ -270,7 +273,7 @@ module Make(IO : IO)
type 'a io = 'a IO.t type 'a io = 'a IO.t
let http let http
?(tries=1) ?client ?(config=Config.default) ?range ?(headers=[]) ~url ~meth () ?(tries=1) ?client ?(config=Config.default) ?range ?content ?(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()
@ -280,6 +283,14 @@ module Make(IO : IO)
in in
_apply_config self config; _apply_config self config;
opt_iter range ~f:(fun s -> Curl.set_range self s); opt_iter range ~f:(fun s -> Curl.set_range self s);
opt_iter content ~f:(fun s ->
Curl.set_readfunction self
(let n = ref 0 in
(fun i ->
let len = min i (String.length s - !n) in
let r = String.sub s !n len in
n := !n + len;
r)));
(* 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
@ -338,6 +349,6 @@ module Make(IO : IO)
let post ?tries ?client ?config ?headers ~params ~url () : _ result io = let post ?tries ?client ?config ?headers ~params ~url () : _ result io =
http ?tries ?client ?config ?headers ~url ~meth:(POST params) () http ?tries ?client ?config ?headers ~url ~meth:(POST params) ()
let put ?tries ?client ?config ?headers ~url () : _ result io = let put ?tries ?client ?config ?headers ~url ~content () : _ result io =
http ?tries ?client ?config ?headers ~url ~meth:PUT () http ?tries ?client ?config ?headers ~url ~content ~meth:PUT ()
end end

View file

@ -86,6 +86,7 @@ module type S = sig
?client:t -> ?client:t ->
?config:Config.t -> ?config:Config.t ->
?range:string -> ?range:string ->
?content:string ->
?headers:(string*string) list -> ?headers:(string*string) list ->
url:string -> url:string ->
meth:meth -> meth:meth ->
@ -101,6 +102,7 @@ module type S = sig
to fetch (either to get large pages to fetch (either to get large pages
by chunks, or to resume an interrupted download). by chunks, or to resume an interrupted download).
@param config configuration to set @param config configuration to set
@param content the content to send as the query's body
@param headers headers of the query @param headers headers of the query
*) *)
@ -123,6 +125,7 @@ module type S = sig
?config:Config.t -> ?config:Config.t ->
?headers:(string*string) list -> ?headers:(string*string) list ->
url:string -> url:string ->
content:string ->
unit -> unit ->
(response, Curl.curlCode * string) result io (response, Curl.curlCode * string) result io
(** Shortcut for [http ~meth:PUT] (** Shortcut for [http ~meth:PUT]