Skip to content

Commit

Permalink
Create switch in Graphql.Request.exec
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Sep 30, 2024
1 parent 4014d8d commit 5c43d98
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
11 changes: 5 additions & 6 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,24 @@ end
let run_eio f =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
Eio.Switch.run @@ fun sw ->
let client = Client.make env in
f env sw client
f env client

let run () period user : unit =
match mode with
| `Normal ->
run_eio @@ fun _env sw client ->
run_eio @@ fun _env client ->
Period.with_period period ~last_fetch_file ~f:(fun period ->
let* token = get_token () in
let request = Contributions.request ~period ~user ~token in
let* contributions = Graphql.Request.exec client sw request in
let* contributions = Graphql.Request.exec client request in
show ~period ~user contributions)
| `Save ->
run_eio @@ fun _env sw client ->
run_eio @@ fun _env client ->
Period.with_period period ~last_fetch_file ~f:(fun period ->
let* token = get_token () in
let request = Contributions.request ~period ~user ~token in
let* contributions = Graphql.Request.exec client sw request in
let* contributions = Graphql.Request.exec client request in
Yojson.Safe.to_file "activity.json" contributions)
| `Load ->
(* When testing formatting changes, it is quicker to fetch the data once and then load it again for each test: *)
Expand Down
3 changes: 2 additions & 1 deletion lib/graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ module Request = struct
let request = Cohttp.Request.make ~meth ~headers uri in
{ request; uri; body }

let exec client sw { request; body; uri } =
let exec client { request; body; uri } =
Logs.debug (fun m -> m "request: @[%a@]@." Cohttp.Request.pp_hum request);
let headers = request.headers in
Eio.Switch.run @@ fun sw ->
let resp, body = Cohttp_eio.Client.post ~sw ~body ~headers client uri in
match resp.status with
| `OK -> (
Expand Down
5 changes: 1 addition & 4 deletions lib/graphql.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ module Request : sig
t

val exec :
Cohttp_eio.Client.t ->
Eio.Switch.t ->
t ->
(Yojson.Safe.t, [ `Msg of string ]) result
Cohttp_eio.Client.t -> t -> (Yojson.Safe.t, [ `Msg of string ]) result

val pp : t Fmt.t
end

0 comments on commit 5c43d98

Please sign in to comment.