diff --git a/lib/contributions.mli b/lib/contributions.mli index 868d4bb..cf59050 100644 --- a/lib/contributions.mli +++ b/lib/contributions.mli @@ -15,7 +15,7 @@ module Repo_map : Map.S with type key = string type t = { username : string; activity : item list Repo_map.t } -val request : period:string * string -> token:Token.t -> Curly.Request.t +val request : period:string * string -> token:Token.t -> Graphql.request val of_json : from:string -> Yojson.Safe.t -> t (** We pass [from] again here so we can filter out anything that GitHub included by accident. *) diff --git a/lib/graphql.ml b/lib/graphql.ml index 562a1a1..af5a7b8 100644 --- a/lib/graphql.ml +++ b/lib/graphql.ml @@ -1,6 +1,12 @@ -let url = "https://api.github.com/graphql" let ( / ) a b = Yojson.Safe.Util.member b a +type request = { + meth : Curly.Meth.t; + url : string; + headers : Curly.Header.t; + body : Yojson.Safe.t; +} + let request ?variables ~token ~query () = let body = `Assoc @@ -9,12 +15,15 @@ let request ?variables ~token ~query () = (match variables with | None -> [] | Some v -> [ ("variables", `Assoc v) ])) - |> Yojson.Safe.to_string in + let url = "https://api.github.com/graphql" in let headers = [ ("Authorization", "bearer " ^ token) ] in - Curly.Request.make ~headers ~body ~url ~meth:`POST () + { meth = `POST; url; headers; body } 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 match Curly.run request with | Ok { Curly.Response.body; _ } -> ( let json = Yojson.Safe.from_string body in diff --git a/lib/graphql.mli b/lib/graphql.mli index eae6c1d..f0094c8 100644 --- a/lib/graphql.mli +++ b/lib/graphql.mli @@ -1,8 +1,15 @@ +type request = { + meth : Curly.Meth.t; + url : string; + headers : Curly.Header.t; + body : Yojson.Safe.t; +} + val request : ?variables:(string * Yojson.Safe.t) list -> token:string -> query:string -> unit -> - Curly.Request.t + request -val exec : Curly.Request.t -> (Yojson.Safe.t, [ `Msg of string ]) result +val exec : request -> (Yojson.Safe.t, [ `Msg of string ]) result diff --git a/test/lib/alcotest_ext.ml b/test/lib/alcotest_ext.ml index 5cde3fa..330db4a 100644 --- a/test/lib/alcotest_ext.ml +++ b/test/lib/alcotest_ext.ml @@ -27,19 +27,49 @@ end let yojson = Yojson.testable module Curly = struct - module Request = struct - type t = Curly.Request.t + module Meth = struct + type t = Curly.Meth.t - let pp = Curly.Request.pp + let pp = Curly.Meth.pp let eq (x : t) (y : t) = - let x = Format.asprintf "%a" Curly.Request.pp x in - - let y = Format.asprintf "%a" Curly.Request.pp y in + let x = Format.asprintf "%a" Curly.Meth.pp x in + let y = Format.asprintf "%a" Curly.Meth.pp y in String.equal x y + end + + module Header = struct + type t = Curly.Header.t - let testable = Alcotest.testable pp eq + let pp = Curly.Header.pp + + let eq (x : t) (y : t) = + let x = Format.asprintf "%a" Curly.Header.pp x in + let y = Format.asprintf "%a" Curly.Header.pp y in + String.equal x y end end -let request = Curly.Request.testable +module Request = struct + type t = Get_activity.Graphql.request + + let pp fs (x : t) = + Format.fprintf fs + "@[{@;\ + meth = %a;@;\ + url = %S@;\ + headers =@ %a@;\ + body =@ @[%a@];@]@;\ + }" + Curly.Meth.pp x.meth x.url Curly.Header.pp x.headers Yojson.pp x.body + + let eq (x : t) (y : t) = + Curly.Meth.eq x.meth y.meth + && String.equal x.url y.url + && Curly.Header.eq x.headers y.headers + && Yojson.eq x.body y.body + + let testable = Alcotest.testable pp eq +end + +let request = Request.testable diff --git a/test/lib/alcotest_ext.mli b/test/lib/alcotest_ext.mli index 651740e..d8aecc3 100644 --- a/test/lib/alcotest_ext.mli +++ b/test/lib/alcotest_ext.mli @@ -5,4 +5,4 @@ val or_msg : 'a Alcotest.testable -> ('a, [ `Msg of string ]) result Alcotest.testable val yojson : Yojson.Safe.t Alcotest.testable -val request : Curly.Request.t Alcotest.testable +val request : Get_activity.Graphql.request Alcotest.testable diff --git a/test/lib/test_contributions.ml b/test/lib/test_contributions.ml index 0b75d17..fde6c91 100644 --- a/test/lib/test_contributions.ml +++ b/test/lib/test_contributions.ml @@ -100,7 +100,65 @@ let test_request = url = "https://api.github.com/graphql"; headers = [ ("Authorization", "bearer ") ]; body = - {|{"query":"query($from: DateTime!, $to: DateTime!) {\n viewer {\n login\n contributionsCollection(from: $from, to: $to) {\n issueContributions(first: 100) {\n nodes {\n occurredAt\n issue {\n url\n title\n body\n repository { nameWithOwner }\n }\n }\n }\n pullRequestContributions(first: 100) {\n nodes {\n occurredAt\n pullRequest {\n url\n title\n body\n repository { nameWithOwner }\n }\n }\n }\n pullRequestReviewContributions(first: 100) {\n nodes {\n occurredAt\n pullRequestReview {\n url\n pullRequest { title }\n body\n state\n comments(first: 100) { nodes { body } }\n repository { nameWithOwner }\n }\n }\n }\n repositoryContributions(first: 100) {\n nodes {\n occurredAt\n repository {\n url\n nameWithOwner\n }\n }\n }\n }\n }\n}","variables":{"from":"","to":""}}|}; + `Assoc + [ + ( "query", + `String + {|query($from: DateTime!, $to: DateTime!) { + viewer { + login + contributionsCollection(from: $from, to: $to) { + issueContributions(first: 100) { + nodes { + occurredAt + issue { + url + title + body + repository { nameWithOwner } + } + } + } + pullRequestContributions(first: 100) { + nodes { + occurredAt + pullRequest { + url + title + body + repository { nameWithOwner } + } + } + } + pullRequestReviewContributions(first: 100) { + nodes { + occurredAt + pullRequestReview { + url + pullRequest { title } + body + state + comments(first: 100) { nodes { body } } + repository { nameWithOwner } + } + } + } + repositoryContributions(first: 100) { + nodes { + occurredAt + repository { + url + nameWithOwner + } + } + } + } + } +}|} + ); + ( "variables", + `Assoc [ ("from", `String ""); ("to", `String "") ] ); + ]; }; ] diff --git a/test/lib/test_graphql.ml b/test/lib/test_graphql.ml index ccf0c4a..a58230d 100644 --- a/test/lib/test_graphql.ml +++ b/test/lib/test_graphql.ml @@ -16,7 +16,7 @@ let test_request = meth = `POST; url = "https://api.github.com/graphql"; headers = [ ("Authorization", "bearer ") ]; - body = {|{"query":""}|}; + body = `Assoc [ ("query", `String "") ]; } (); ]