Add type aliases for curl errors and headers

This commit is contained in:
Frédéric Bour 2025-03-14 18:49:33 +09:00
parent 4b7d9ec769
commit ddd750cc0c
2 changed files with 31 additions and 23 deletions

View file

@ -162,6 +162,10 @@ let with_client ?set_opts f =
delete c; delete c;
raise e raise e
type curl_error = Curl.curlCode * string
type header = string * string
type response_info = { type response_info = {
ri_response_time: float; ri_response_time: float;
ri_redirect_count: int; ri_redirect_count: int;
@ -176,7 +180,7 @@ let string_of_response_info s = Format.asprintf "%a" pp_response_info s
type 'body response = { type 'body response = {
code: int; code: int;
headers: (string * string) list; headers: header list;
body: 'body; body: 'body;
info: response_info; info: response_info;
} }
@ -236,11 +240,11 @@ module type S = sig
?config:Config.t -> ?config:Config.t ->
?range:string -> ?range:string ->
?content:[ `String of string | `Write of bytes -> int -> int ] -> ?content:[ `String of string | `Write of bytes -> int -> int ] ->
?headers:(string * string) list -> ?headers:header list ->
url:string -> url:string ->
meth:meth -> meth:meth ->
unit -> unit ->
(string response, Curl.curlCode * string) result io (string response, curl_error) result io
(** General purpose HTTP call via cURL. (** General purpose HTTP call via cURL.
@param url the URL to query @param url the URL to query
@param meth which method to use (see {!meth}) @param meth which method to use (see {!meth})
@ -274,12 +278,12 @@ module type S = sig
?config:Config.t -> ?config:Config.t ->
?range:string -> ?range:string ->
?content:[ `String of string | `Write of bytes -> int -> int ] -> ?content:[ `String of string | `Write of bytes -> int -> int ] ->
?headers:(string * string) list -> ?headers:header list ->
url:string -> url:string ->
meth:meth -> meth:meth ->
write_into:#input_stream -> write_into:#input_stream ->
unit -> unit ->
(unit response, Curl.curlCode * string) result io (unit response, curl_error) result io
(** HTTP call via cURL, with a streaming response body. (** HTTP call via cURL, with a streaming response body.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
@ -288,10 +292,10 @@ module type S = sig
?client:t -> ?client:t ->
?config:Config.t -> ?config:Config.t ->
?range:string -> ?range:string ->
?headers:(string * string) list -> ?headers:header list ->
url:string -> url:string ->
unit -> unit ->
(string response, Curl.curlCode * string) result io (string response, curl_error) result io
(** Shortcut for [http ~meth:GET] (** Shortcut for [http ~meth:GET]
See {!http} for more info. See {!http} for more info.
*) *)
@ -300,11 +304,11 @@ module type S = sig
?tries:int -> ?tries:int ->
?client:t -> ?client:t ->
?config:Config.t -> ?config:Config.t ->
?headers:(string * string) list -> ?headers:header list ->
url:string -> url:string ->
content:[ `String of string | `Write of bytes -> int -> int ] -> content:[ `String of string | `Write of bytes -> int -> int ] ->
unit -> unit ->
(string response, Curl.curlCode * string) result io (string response, curl_error) result io
(** Shortcut for [http ~meth:PUT] (** Shortcut for [http ~meth:PUT]
See {!http} for more info. See {!http} for more info.
*) *)
@ -313,18 +317,18 @@ module type S = sig
?tries:int -> ?tries:int ->
?client:t -> ?client:t ->
?config:Config.t -> ?config:Config.t ->
?headers:(string * string) list -> ?headers:header list ->
?content:[ `String of string | `Write of bytes -> int -> int ] -> ?content:[ `String of string | `Write of bytes -> int -> int ] ->
params:Curl.curlHTTPPost list -> params:Curl.curlHTTPPost list ->
url:string -> url:string ->
unit -> unit ->
(string response, Curl.curlCode * string) result io (string response, curl_error) result io
(** Shortcut for [http ~meth:(POST params)] (** Shortcut for [http ~meth:(POST params)]
See {!http} for more info. See {!http} for more info.
*) *)
end end
exception Parse_error of Curl.curlCode * string exception Parse_error of curl_error
let mk_res (self : t) headers body : (_ response, _) result = let mk_res (self : t) headers body : (_ response, _) result =
let split_colon s = let split_colon s =

View file

@ -70,6 +70,10 @@ end
val copy : t -> t val copy : t -> t
*) *)
type curl_error = Curl.curlCode * string
type header = string * string
type response_info = { type response_info = {
ri_response_time: float; ri_response_time: float;
(** Total time (in seconds) for the request/response pair. (** Total time (in seconds) for the request/response pair.
@ -86,7 +90,7 @@ val string_of_response_info : response_info -> string
type 'body response = { type 'body response = {
code: int; code: int;
(** Response code. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status *) (** Response code. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status *)
headers: (string * string) list; (** Response headers *) headers: header list; (** Response headers *)
body: 'body; (** Response body, or [""] *) body: 'body; (** Response body, or [""] *)
info: response_info; (** Information about the response *) info: response_info; (** Information about the response *)
} }
@ -135,11 +139,11 @@ module type S = sig
?config:Config.t -> ?config:Config.t ->
?range:string -> ?range:string ->
?content:[ `String of string | `Write of bytes -> int -> int ] -> ?content:[ `String of string | `Write of bytes -> int -> int ] ->
?headers:(string * string) list -> ?headers:header list ->
url:string -> url:string ->
meth:meth -> meth:meth ->
unit -> unit ->
(string response, Curl.curlCode * string) result io (string response, curl_error) result io
(** General purpose HTTP call via cURL. (** General purpose HTTP call via cURL.
@param url the URL to query @param url the URL to query
@param meth which method to use (see {!meth}) @param meth which method to use (see {!meth})
@ -173,12 +177,12 @@ module type S = sig
?config:Config.t -> ?config:Config.t ->
?range:string -> ?range:string ->
?content:[ `String of string | `Write of bytes -> int -> int ] -> ?content:[ `String of string | `Write of bytes -> int -> int ] ->
?headers:(string * string) list -> ?headers:header list ->
url:string -> url:string ->
meth:meth -> meth:meth ->
write_into:#input_stream -> write_into:#input_stream ->
unit -> unit ->
(unit response, Curl.curlCode * string) result io (unit response, curl_error) result io
(** HTTP call via cURL, with a streaming response body. (** HTTP call via cURL, with a streaming response body.
The body is given to [write_into] by chunks, The body is given to [write_into] by chunks,
then [write_into#on_close ()] is called then [write_into#on_close ()] is called
@ -190,10 +194,10 @@ module type S = sig
?client:t -> ?client:t ->
?config:Config.t -> ?config:Config.t ->
?range:string -> ?range:string ->
?headers:(string * string) list -> ?headers:header list ->
url:string -> url:string ->
unit -> unit ->
(string response, Curl.curlCode * string) result io (string response, curl_error) result io
(** Shortcut for [http ~meth:GET] (** Shortcut for [http ~meth:GET]
See {!http} for more info. See {!http} for more info.
*) *)
@ -202,11 +206,11 @@ module type S = sig
?tries:int -> ?tries:int ->
?client:t -> ?client:t ->
?config:Config.t -> ?config:Config.t ->
?headers:(string * string) list -> ?headers:header list ->
url:string -> url:string ->
content:[ `String of string | `Write of bytes -> int -> int ] -> content:[ `String of string | `Write of bytes -> int -> int ] ->
unit -> unit ->
(string response, Curl.curlCode * string) result io (string response, curl_error) result io
(** Shortcut for [http ~meth:PUT] (** Shortcut for [http ~meth:PUT]
See {!http} for more info. See {!http} for more info.
*) *)
@ -215,12 +219,12 @@ module type S = sig
?tries:int -> ?tries:int ->
?client:t -> ?client:t ->
?config:Config.t -> ?config:Config.t ->
?headers:(string * string) list -> ?headers:header list ->
?content:[ `String of string | `Write of bytes -> int -> int ] -> ?content:[ `String of string | `Write of bytes -> int -> int ] ->
params:Curl.curlHTTPPost list -> params:Curl.curlHTTPPost list ->
url:string -> url:string ->
unit -> unit ->
(string response, Curl.curlCode * string) result io (string response, curl_error) result io
(** Shortcut for [http ~meth:(POST params)] (** Shortcut for [http ~meth:(POST params)]
See {!http} for more info. See {!http} for more info.
*) *)