forked from patricoferris/get-activity
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use cohttp and eio instead of curly #44
Open
gpetiot
wants to merge
8
commits into
tarides:main
Choose a base branch
from
gpetiot:eio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f20e522
Use cohttp and eio instead of curly
gpetiot 926cd69
Add missing deps
gpetiot 09f67f1
Update CHANGES.md
gpetiot 8cc5f0d
need 5.2
gpetiot 4500195
5.1 is enough?
gpetiot f65013c
tls-eio >= 1.0.0
gpetiot 4014d8d
5.0 enough?
gpetiot 5c43d98
Create switch in Graphql.Request.exec
gpetiot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
## unreleased | ||
|
||
### Changed | ||
|
||
- Use cohttp and eio instead of curly (#44, @gpetiot) | ||
|
||
## 2.0.1 | ||
|
||
### Fixed | ||
|
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
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,6 @@ | ||
(library | ||
(name get_activity) | ||
(public_name get-activity-lib) | ||
(libraries astring curly fmt logs yojson) | ||
(libraries astring cohttp-eio fmt logs yojson unix) | ||
(preprocess | ||
(pps ppx_yojson_conv))) |
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,47 +1,58 @@ | ||
let ( let* ) = Result.bind | ||
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; | ||
} | ||
module Request = struct | ||
type t = { request : Cohttp.Request.t; uri : Uri.t; body : Cohttp_eio.Body.t } | ||
|
||
let request ?variables ~token ~query () = | ||
let body = | ||
`Assoc | ||
(("query", `String query) | ||
:: | ||
(match variables with | ||
| None -> [] | ||
| Some v -> [ ("variables", `Assoc v) ])) | ||
in | ||
let url = "https://api.github.com/graphql" in | ||
let headers = [ ("Authorization", "bearer " ^ token) ] in | ||
{ meth = `POST; url; headers; body } | ||
let make ?variables ~token ~query () = | ||
let body = | ||
`Assoc | ||
(("query", `String query) | ||
:: | ||
(match variables with | ||
| None -> [] | ||
| Some v -> [ ("variables", `Assoc v) ])) | ||
|> Yojson.Safe.to_string |> Cohttp_eio.Body.of_string | ||
in | ||
let uri = Uri.of_string "https://api.github.com/graphql" in | ||
let meth = `POST in | ||
let headers = Cohttp.Header.init_with "Authorization" ("bearer " ^ token) in | ||
let request = Cohttp.Request.make ~meth ~headers uri in | ||
{ request; uri; 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 | ||
Logs.debug (fun m -> m "request: @[%a@]@." Curly.Request.pp request); | ||
match Curly.run request with | ||
| 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 | ||
| `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))) | ||
| Error e -> | ||
Error | ||
(`Msg | ||
(Format.asprintf | ||
"@[<v2>Error performing GraphQL query on GitHub: %a@]" | ||
Curly.Error.pp e)) | ||
let exec client { request; body; uri } = | ||
Logs.debug (fun m -> m "request: @[%a@]@." Cohttp.Request.pp_hum request); | ||
let headers = request.headers in | ||
Eio.Switch.run @@ fun sw -> | ||
let resp, body = Cohttp_eio.Client.post ~sw ~body ~headers client uri in | ||
match resp.status with | ||
| `OK -> ( | ||
Logs.debug (fun m -> m "response: @[%a@]@." Http.Response.pp resp); | ||
let* body = (Eio.Buf_read.(parse take_all) body) ~max_size:max_int in | ||
let json = Yojson.Safe.from_string body in | ||
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))) | ||
| status -> | ||
Error | ||
(`Msg | ||
(Fmt.str | ||
"@[<v2>Error performing GraphQL query on GitHub: Unexpected \ | ||
HTTP status %a@]" | ||
Http.Status.pp status)) | ||
|
||
let pp ppf { request; uri = _; body = _ } = | ||
let pp_request ppf r = | ||
Fmt.pf ppf "@[<v>request =@;<1 2>@[<v2>%a@]@]" Cohttp.Request.pp_hum r | ||
in | ||
let pp_body ppf () = Fmt.pf ppf "@[<v>body =@;<1 2><...>@]" in | ||
Fmt.pf ppf "@[<v2>{@ %a;@ %a@ }@]" pp_request request pp_body () | ||
end |
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,15 +1,15 @@ | ||
type request = { | ||
meth : Curly.Meth.t; | ||
url : string; | ||
headers : Curly.Header.t; | ||
body : Yojson.Safe.t; | ||
} | ||
module Request : sig | ||
type t | ||
|
||
val request : | ||
?variables:(string * Yojson.Safe.t) list -> | ||
token:string -> | ||
query:string -> | ||
unit -> | ||
request | ||
val make : | ||
?variables:(string * Yojson.Safe.t) list -> | ||
token:string -> | ||
query:string -> | ||
unit -> | ||
t | ||
|
||
val exec : request -> (Yojson.Safe.t, [ `Msg of string ]) result | ||
val exec : | ||
Cohttp_eio.Client.t -> t -> (Yojson.Safe.t, [ `Msg of string ]) result | ||
|
||
val pp : t Fmt.t | ||
end |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We really should make a cohttp-eio-tls package with a proper authenticator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can take a look at doing it this week.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, but I'm only going to work on this PR during Hacking Days, so no pressure!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the example to use a proper authenticator now: mirage/ocaml-cohttp#1091
It's just: