Skip to content

Commit

Permalink
Display curl requests and responses in debug (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot authored Apr 2, 2024
1 parent a44441a commit 4756a4d
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

- API: `Contributions.of_json` parameter `~from` is replaced by `~period` (#31, @gpetiot)

### Added

- Display curl requests and responses in debug mode (`-vv` or `--verbosity debug`) (#36, @gpetiot)

## 1.0.1

### Fixed
Expand Down
9 changes: 8 additions & 1 deletion bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@
(name main)
(public_name get-activity)
(package get-activity)
(libraries cmdliner dune-build-info get-activity-lib))
(libraries
cmdliner
dune-build-info
get-activity-lib
logs.cli
logs.fmt
fmt.cli
fmt.tty))
14 changes: 11 additions & 3 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ let mode = `Normal

open Cmdliner

let setup_log style_renderer level =
Fmt_tty.setup_std_outputs ?style_renderer ();
Logs.set_level level;
Logs.set_reporter (Logs_fmt.reporter ());
()

let setup_log =
Term.(const setup_log $ Fmt_cli.style_renderer () $ Logs_cli.level ())

let from =
let doc =
Arg.info ~docv:"TIMESTAMP" ~doc:"Starting date (ISO8601)." [ "from" ]
Expand Down Expand Up @@ -88,11 +97,10 @@ let version =

let info = Cmd.info "get-activity" ~version

let run period user : unit =
let run () period user : unit =
match mode with
| `Normal ->
Period.with_period period ~last_fetch_file ~f:(fun period ->
(* Fmt.pr "period: %a@." Fmt.(pair string string) period; *)
let* token = get_token () in
let request = Contributions.request ~period ~user ~token in
let* contributions = Graphql.exec request in
Expand All @@ -112,6 +120,6 @@ let run period user : unit =
in
show ~period ~user @@ Yojson.Safe.from_file "activity.json"

let term = Term.(const run $ term_result period $ user)
let term = Term.(const run $ setup_log $ term_result period $ user)
let cmd = Cmd.v info term
let () = Stdlib.exit @@ Cmd.eval cmd
26 changes: 20 additions & 6 deletions dune-project
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
(lang dune 2.8)

(name get-activity)

(generate_opam_files true)
(source (github tarides/get-activity))

(source
(github tarides/get-activity))

(authors "[email protected]")

(maintainers "Guillaume Petiot <[email protected]>")

(package
(name get-activity)
(synopsis "Collect activity as markdown")
(depends
(cmdliner (>= 1.1.1))
(cmdliner
(>= 1.1.1))
dune-build-info
(get-activity-lib (= :version))))
fmt
logs
(get-activity-lib
(= :version))))

(package
(name get-activity-lib)
Expand All @@ -21,7 +31,11 @@
(ppx_expect :with-test)
astring
curly
(fmt (>= 0.8.7))
(fmt
(>= 0.8.7))
logs
ppx_yojson_conv
(yojson (>= 1.6))
(ocaml (>= 4.08))))
(yojson
(>= 1.6))
(ocaml
(>= 4.08))))
1 change: 1 addition & 0 deletions get-activity-lib.opam
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ depends: [
"astring"
"curly"
"fmt" {>= "0.8.7"}
"logs"
"ppx_yojson_conv"
"yojson" {>= "1.6"}
"ocaml" {>= "4.08"}
Expand Down
2 changes: 2 additions & 0 deletions get-activity.opam
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ depends: [
"dune" {>= "2.8"}
"cmdliner" {>= "1.1.1"}
"dune-build-info"
"fmt"
"logs"
"get-activity-lib" {= version}
"odoc" {with-doc}
]
Expand Down
2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(library
(name get_activity)
(public_name get-activity-lib)
(libraries astring curly fmt yojson)
(libraries astring curly fmt logs yojson)
(preprocess
(pps ppx_yojson_conv)))
4 changes: 3 additions & 1 deletion lib/graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ let exec request =
let { meth; url; headers; body } = request in
let body = Yojson.Safe.to_string body in
let request = Curly.Request.make ~headers ~body ~url ~meth () in
Logs.debug (fun m -> m "request: @[%a@]@." Curly.Request.pp request);
match Curly.run request with
| Ok { Curly.Response.body; _ } -> (
| Ok ({ Curly.Response.body; _ } as response) -> (
Logs.debug (fun m -> m "response: @[%a@]@." Curly.Response.pp response);
let json = Yojson.Safe.from_string body in
match json / "message" with
| `Null -> Ok json
Expand Down

0 comments on commit 4756a4d

Please sign in to comment.