forked from patricoferris/get-activity
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use curly instead of cohttp-lwt-unix (#12)
- Loading branch information
Showing
9 changed files
with
55 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(library | ||
(name get_activity) | ||
(public_name get-activity-lib) | ||
(libraries cohttp cohttp-lwt cohttp-lwt-unix yojson)) | ||
(libraries astring curly fmt yojson)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,45 @@ | ||
open Lwt.Infix | ||
|
||
let graphql_endpoint = Uri.of_string "https://api.github.com/graphql" | ||
let ( / ) a b = Yojson.Safe.Util.member b a | ||
|
||
let exec ?variables ~token ~query () = | ||
type request = { | ||
meth : Curly.Meth.t; | ||
url : string; | ||
headers : Curly.Header.t; | ||
body : Yojson.Safe.t; | ||
} | ||
|
||
let request ?variables ~token ~query () = | ||
let body = | ||
`Assoc | ||
(("query", `String query) | ||
:: | ||
(match variables with | ||
| None -> [] | ||
| Some v -> [ ("variables", `Assoc v) ])) | ||
|> Yojson.Safe.to_string |> Cohttp_lwt.Body.of_string | ||
in | ||
let headers = Cohttp.Header.init_with "Authorization" ("bearer " ^ token) in | ||
Cohttp_lwt_unix.Client.post ~headers ~body graphql_endpoint | ||
>>= fun (resp, body) -> | ||
Cohttp_lwt.Body.to_string body >|= fun body -> | ||
match Cohttp.Response.status resp with | ||
| `OK -> ( | ||
let url = "https://api.github.com/graphql" in | ||
let headers = [ ("Authorization", "bearer " ^ token) ] in | ||
{ 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 | ||
match json / "errors" with | ||
match json / "message" with | ||
| `Null -> Ok json | ||
| `String e -> | ||
Error (`Msg (Format.asprintf "@[<v2>GitHub returned errors: %s@]" e)) | ||
| _errors -> | ||
Error | ||
(`Msg | ||
(Format.asprintf "@[<v2>GitHub returned errors: %a@]" | ||
(Yojson.Safe.pretty_print ~std:true) | ||
json))) | ||
| err -> | ||
| Error e -> | ||
Error | ||
(`Msg | ||
(Format.asprintf | ||
"@[<v2>Error performing GraphQL query on GitHub: %s@,%s@]" | ||
(Cohttp.Code.string_of_status err) | ||
body)) | ||
"@[<v2>Error performing GraphQL query on GitHub: %a@]" | ||
Curly.Error.pp e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
val exec : | ||
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 -> | ||
(Yojson.Safe.t, [ `Msg of string ]) result Lwt.t | ||
request | ||
|
||
val exec : request -> (Yojson.Safe.t, [ `Msg of string ]) result |