tmcgilchrist / ocaml-gitlab

Native OCaml bindings to Gitlab REST API v4
https://tmcgilchrist.github.io/ocaml-gitlab/gitlab/
BSD 3-Clause "New" or "Revised" License
27 stars 8 forks source link

Getting the total number of items in a stream #70

Open arvidj opened 1 year ago

arvidj commented 1 year ago

It is often necessary to get the total number of items in a given stream (e.g. the number of open merge requests). If I understand correctly, this is not yet possible in ocaml-gitlab without requesting all items in the stream and counting them. However, GitLab's API returns the total number of items in the x-total header of responses.

I want to expose this value somehow but I have a hard time wrapping my head around Streams. I've made a proof-of-concept here: https://github.com/arvidj/ocaml-gitlab/commit/0dc9f7e but there are some concerns:

Another option is making the proposed total field of Stream have the type unit -> int option Monad.t. For Gitlab streams, the total function can capture the endpoint in a closure and query it (possible using a http HEAD request to only retrieve headers) at all calls to total. The value could be memoized. A disadvantage of this approach is that if you want to get the total and also retrieve the items, an additional API request has to be made. A small optimizations would be to retrieve the x-total for during calls to next and fill in the total if it is not yet available.