In preparation for my own project this summer, what follows are some notes on the ocaml-github library.
Dealing with JSON
Requests to/from github’s API are JSON. The JSON parsing/unparsing is done with using Yojson. There is a helpful RWO chapter that demonstrates Yojson use.
Organization of the ocaml-github library
For the most part the different modules (see lib/github.ml(i)) correspond to github API actions eg making pull requests or querying the issues a repository.
Of particular interest is the Monad module. Illuminating comment explains it quite wonderfully, I think :)
(** All API requests are bound through this monad. The [run] function
will unpack an API response into an Lwt thread that will hold the
ultimate response. *)
module Monad : sig
type 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t
val return : 'a -> 'a t
val run : 'a t -> 'a Lwt.t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
The Role of Cohttp
Cohttp makes the actual requests to the github api. I thought these were interesting/important examples of uses of the Cohttp library (called C in the following snippets:
let get ~user ~pass ~id () =
let uri = URI.authorization id in
let headers = C.Header.(add_authorization (init ()) (C.Auth.Basic (user,pass))) in
API.get ~headers ~uri ~expected_code:`OK (fun body -> return (auth_of_string body))
(* Add the correct mime-type header *)
let realize_headers headers = C.Header.add_opt headers "content-type" "application/json"