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
- Loading branch information
Showing
10 changed files
with
29 additions
and
60 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
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,37 @@ | ||
open Lwt.Infix | ||
|
||
let graphql_endpoint = Uri.of_string "https://api.github.com/graphql" | ||
let url = "https://api.github.com/graphql" | ||
let ( / ) a b = Yojson.Safe.Util.member b a | ||
|
||
let exec ?variables ~token ~query () = | ||
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 | ||
|> Yojson.Safe.to_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 headers = [ ("Authorization", "bearer " ^ token) ] in | ||
Curly.Request.make ~headers ~body ~url ~meth:`POST () | ||
|
||
let exec ?variables ~token ~query () = | ||
let request = request ?variables ~token ~query () 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
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