Skip to content

Commit 0dc9f7e

Browse files
committed
Stream: add total
1 parent a38267a commit 0dc9f7e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/gitlab_core.ml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ struct
558558
buffer : 'a list;
559559
refill : (unit -> 'a t Monad.t) option;
560560
endpoint : Endpoint.t;
561+
total : int option;
561562
}
562563

563564
type 'a parse = string -> 'a list Lwt.t
@@ -568,6 +569,7 @@ struct
568569
buffer = [];
569570
refill = None;
570571
endpoint = Endpoint.empty;
572+
total = Some 0;
571573
}
572574

573575
let rec next =
@@ -578,6 +580,8 @@ struct
578580
| { buffer = h :: buffer; _ } as s ->
579581
return (Some (h, { s with buffer })))
580582

583+
let total s = s.total
584+
581585
let take : int -> 'a t -> 'b t = fun n s ->
582586
let rec refill n' s () =
583587
Monad.(
@@ -645,7 +649,7 @@ struct
645649
in
646650
aux [] s
647651

648-
let of_list buffer = { empty with buffer; refill = None }
652+
let of_list buffer = { empty with buffer; refill = None; total = Some (List.length buffer) }
649653
let poll stream = stream.restart stream.endpoint
650654

651655
let since stream version =
@@ -848,6 +852,7 @@ struct
848852
buffer;
849853
refill = None;
850854
endpoint = Endpoint.empty;
855+
total = None;
851856
})
852857
fhs
853858

@@ -867,8 +872,13 @@ struct
867872
| Some uri ->
868873
request ~uri (stream_next restart request uri fn endpoint))
869874
in
875+
let total =
876+
Option.bind
877+
Cohttp.(Header.get envelope.Response.headers "x-total")
878+
int_of_string_opt
879+
in
870880
fn body >>= fun buffer ->
871-
return { Stream.restart; buffer; refill; endpoint })
881+
return { Stream.restart; buffer; refill; endpoint; total })
872882

873883
let rec restart_stream ~fail_handlers ~expected_code ?headers ?token ?params
874884
fn endpoint =
@@ -933,7 +943,7 @@ struct
933943
Some
934944
(fun () -> request ~uri (stream_next restart request uri fn endpoint))
935945
in
936-
{ Stream.restart; buffer = []; refill; endpoint }
946+
{ Stream.restart; buffer = []; refill; endpoint; total = None }
937947

938948
let post ?(fail_handlers = []) ~expected_code ?headers =
939949
let headers =

lib/gitlab_s.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ module type Gitlab = sig
212212
means to iterate over ordered API results which may be too
213213
numerous to fit into a single request/response pair. *)
214214

215+
val total : 'a t -> int option
216+
215217
val map : ('a -> 'b list Monad.t) -> 'a t -> 'b t
216218
(** [map f s] is the lazy stream of [f] applied to elements of [s]
217219
as they are demanded. *)

0 commit comments

Comments
 (0)