Skip to content

Commit

Permalink
Keep the json in the request body
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Mar 7, 2024
1 parent eb5da8b commit b49d213
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/contributions.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
Expand Down
15 changes: 12 additions & 3 deletions lib/graphql.ml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions lib/graphql.mli
Original file line number Diff line number Diff line change
@@ -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
46 changes: 38 additions & 8 deletions test/lib/alcotest_ext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
"@[<hv 2>{@;\
meth = %a;@;\
url = %S@;\
headers =@ %a@;\
body =@ @[<hv 0>%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
2 changes: 1 addition & 1 deletion test/lib/alcotest_ext.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
60 changes: 59 additions & 1 deletion test/lib/test_contributions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "") ] );
];
};
]

Expand Down
2 changes: 1 addition & 1 deletion test/lib/test_graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let test_request =
meth = `POST;
url = "https://api.github.com/graphql";
headers = [ ("Authorization", "bearer ") ];
body = {|{"query":""}|};
body = `Assoc [ ("query", `String "") ];
}
();
]
Expand Down

0 comments on commit b49d213

Please sign in to comment.