mirror of
https://github.com/c-cube/ezcurl.git
synced 2025-12-06 03:05:31 -05:00
add Config value
This commit is contained in:
parent
ca83aa49ee
commit
80fc740e39
2 changed files with 73 additions and 37 deletions
|
|
@ -1,3 +1,34 @@
|
||||||
|
|
||||||
|
let opt_map ~f = function None -> None | Some x -> Some (f x)
|
||||||
|
let opt_iter ~f = function None -> () | Some x -> f x
|
||||||
|
|
||||||
|
module Config = struct
|
||||||
|
type t = {
|
||||||
|
verbose: bool;
|
||||||
|
authmethod: Curl.curlAuth list option;
|
||||||
|
max_redirects: int;
|
||||||
|
follow_location: bool;
|
||||||
|
username: string option;
|
||||||
|
password: string option;
|
||||||
|
}
|
||||||
|
|
||||||
|
let default : t = {
|
||||||
|
verbose=false;
|
||||||
|
max_redirects = 50;
|
||||||
|
follow_location=true;
|
||||||
|
authmethod=None;
|
||||||
|
username=None;
|
||||||
|
password=None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let password x self = {self with password=Some x}
|
||||||
|
let username x self = {self with username=Some x}
|
||||||
|
let verbose x self = { self with verbose=x}
|
||||||
|
let follow_location x self = {self with follow_location=x}
|
||||||
|
let max_redirects x self = {self with max_redirects=max 1 x}
|
||||||
|
let authmethod x self = {self with authmethod=Some x}
|
||||||
|
end
|
||||||
|
|
||||||
type t = Curl.t
|
type t = Curl.t
|
||||||
|
|
||||||
let _init = lazy (
|
let _init = lazy (
|
||||||
|
|
@ -14,6 +45,27 @@ let make ?(set_opts=fun _ -> ()) () : t =
|
||||||
|
|
||||||
let delete = Curl.cleanup
|
let delete = Curl.cleanup
|
||||||
|
|
||||||
|
(* set options *)
|
||||||
|
let _apply_config (self:t) (config:Config.t) : unit =
|
||||||
|
let {
|
||||||
|
Config.verbose; max_redirects; follow_location; authmethod;
|
||||||
|
username; password;
|
||||||
|
} = config in
|
||||||
|
Curl.set_verbose self verbose;
|
||||||
|
Curl.set_maxredirs self max_redirects;
|
||||||
|
Curl.set_followlocation self follow_location;
|
||||||
|
opt_iter authmethod ~f:(Curl.set_httpauth self);
|
||||||
|
opt_iter username ~f:(Curl.set_username self);
|
||||||
|
opt_iter password ~f:(Curl.set_password self);
|
||||||
|
()
|
||||||
|
|
||||||
|
let _set_headers (self:t) (headers: _ list) : unit =
|
||||||
|
let headers =
|
||||||
|
List.map (fun (k,v) -> k ^ ": " ^ v ^ "\r\n") headers
|
||||||
|
in
|
||||||
|
Curl.set_httpheader self headers;
|
||||||
|
()
|
||||||
|
|
||||||
let with_client ?set_opts f =
|
let with_client ?set_opts f =
|
||||||
let c = make ?set_opts () in
|
let c = make ?set_opts () in
|
||||||
try
|
try
|
||||||
|
|
@ -72,42 +124,28 @@ let string_of_meth = function
|
||||||
| POST _ -> "POST"
|
| POST _ -> "POST"
|
||||||
| PUT -> "PUT"
|
| PUT -> "PUT"
|
||||||
|
|
||||||
let opt_map ~f = function None -> None | Some x -> Some (f x)
|
let http
|
||||||
let opt_iter ~f = function None -> () | Some x -> f x
|
?(tries=1) ?client ?(config=Config.default) ?(headers=[]) ~url ~meth ()
|
||||||
|
: _ result =
|
||||||
let http ?(verbose=false) ?(tries=1)
|
|
||||||
?client ?authmethod ?(max_redirects=50) ?(follow_location=false)
|
|
||||||
?username ?password ?(headers=[])
|
|
||||||
~url ~meth () : _ result =
|
|
||||||
let do_cleanup, self = match client with
|
let do_cleanup, self = match client with
|
||||||
| None -> true, make()
|
| None -> true, make()
|
||||||
| Some c ->
|
| Some c ->
|
||||||
Curl.reset c;
|
Curl.reset c;
|
||||||
false, c
|
false, c
|
||||||
in
|
in
|
||||||
|
_apply_config self config;
|
||||||
(* 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
|
||||||
let resp_headers = ref [] in
|
let resp_headers = ref [] in
|
||||||
let resp_headers_done = ref false in (* once we get "\r\n" header line *)
|
let resp_headers_done = ref false in (* once we get "\r\n" header line *)
|
||||||
(* start setting options *)
|
|
||||||
Curl.set_url self url;
|
Curl.set_url self url;
|
||||||
Curl.set_verbose self verbose;
|
|
||||||
Curl.set_maxredirs self max_redirects;
|
|
||||||
Curl.set_followlocation self follow_location;
|
|
||||||
Curl.set_header self false;
|
|
||||||
begin match meth with
|
begin match meth with
|
||||||
| 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;
|
||||||
end;
|
end;
|
||||||
opt_iter authmethod ~f:(Curl.set_httpauth self);
|
_set_headers self headers;
|
||||||
opt_iter username ~f:(Curl.set_username self);
|
|
||||||
opt_iter password ~f:(Curl.set_password self);
|
|
||||||
let headers =
|
|
||||||
List.map (fun (k,v) -> k ^ ": " ^ v ^ "\r\n") headers
|
|
||||||
in
|
|
||||||
Curl.set_httpheader self headers;
|
|
||||||
Curl.set_headerfunction self
|
Curl.set_headerfunction self
|
||||||
(fun s0 ->
|
(fun s0 ->
|
||||||
let s = String.trim s0 in
|
let s = String.trim s0 in
|
||||||
|
|
@ -143,12 +181,8 @@ let http ?(verbose=false) ?(tries=1)
|
||||||
in
|
in
|
||||||
loop tries
|
loop tries
|
||||||
|
|
||||||
let get ?verbose ?tries ?client ?authmethod ?max_redirects
|
let get ?tries ?client ?config ?headers ~url () : _ result =
|
||||||
?follow_location
|
http ?tries ?client ?config ?headers ~url ~meth:GET ()
|
||||||
?username ?password ?headers ~url () : _ result =
|
|
||||||
http ?verbose ?tries ?client ?authmethod ?max_redirects
|
|
||||||
?follow_location
|
|
||||||
?username ?password ?headers ~url ~meth:GET ()
|
|
||||||
|
|
||||||
(* TODO
|
(* TODO
|
||||||
let post ?verbose ?tries ?client ?auth ?username ?password ~url () : _ result =
|
let post ?verbose ?tries ?client ?auth ?username ?password ~url () : _ result =
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,15 @@
|
||||||
|
|
||||||
|
module Config : sig
|
||||||
|
type t
|
||||||
|
val default : t
|
||||||
|
val verbose : bool -> t -> t
|
||||||
|
val authmethod : Curl.curlAuth list -> t -> t
|
||||||
|
val max_redirects : int -> t -> t
|
||||||
|
val follow_location : bool -> t -> t
|
||||||
|
val username : string -> t -> t
|
||||||
|
val password : string -> t -> t
|
||||||
|
end
|
||||||
|
|
||||||
type t
|
type t
|
||||||
|
|
||||||
val make :
|
val make :
|
||||||
|
|
@ -35,14 +47,9 @@ type meth =
|
||||||
val string_of_meth : meth -> string
|
val string_of_meth : meth -> string
|
||||||
|
|
||||||
val http :
|
val http :
|
||||||
?verbose:bool ->
|
|
||||||
?tries:int ->
|
?tries:int ->
|
||||||
?client:t ->
|
?client:t ->
|
||||||
?authmethod:Curl.curlAuth list ->
|
?config:Config.t ->
|
||||||
?max_redirects:int ->
|
|
||||||
?follow_location:bool ->
|
|
||||||
?username:string ->
|
|
||||||
?password:string ->
|
|
||||||
?headers:(string*string) list ->
|
?headers:(string*string) list ->
|
||||||
url:string ->
|
url:string ->
|
||||||
meth:meth ->
|
meth:meth ->
|
||||||
|
|
@ -50,14 +57,9 @@ val http :
|
||||||
(response, Curl.curlCode * string) result
|
(response, Curl.curlCode * string) result
|
||||||
|
|
||||||
val get :
|
val get :
|
||||||
?verbose:bool ->
|
|
||||||
?tries:int ->
|
?tries:int ->
|
||||||
?client:t ->
|
?client:t ->
|
||||||
?authmethod:Curl.curlAuth list ->
|
?config:Config.t ->
|
||||||
?max_redirects:int ->
|
|
||||||
?follow_location:bool ->
|
|
||||||
?username:string ->
|
|
||||||
?password:string ->
|
|
||||||
?headers:(string*string) list ->
|
?headers:(string*string) list ->
|
||||||
url:string ->
|
url:string ->
|
||||||
unit ->
|
unit ->
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue