1. camlspotter
  2. ocaml-bitbucket

Source

ocaml-bitbucket / api.ml

open Common
open Tiny_json
open Json_conv
open Meta_conv.Conv
open Sexplib.Conv

module Json = struct
  include Json
  let sexp_of_t j = Sexplib.Sexp.Atom (Json.show j)
  let t_of_sexp = function
    | Sexplib.Sexp.Atom s -> Json.parse s
    | _ -> assert false
end

module Address = struct
  include Address
  let sexp_of_t _ = Sexplib.Sexp.Atom ""
  let t_of_sexp _ = top
end

type 'target fields = (string * ('target * Address.t)) list with sexp

module Scheme = struct
  type t = Hg(:"hg":) | Git(:"git":) with conv(json)
end

module User = struct

  module Repositories = struct

    type repo = { 
      owner      : string;
      scm        : Scheme.t;
      slug       : string;
      is_private : bool;
      name       : string
    } 

    and resp = repo list with conv(json)

    let get ~user ~password = 
      resp_of_json (Json.parse (Curl.get_string (fun h ->
        h#set_url "https://api.bitbucket.org/1.0/user/repositories/";
        h#set_userpwd (Printf.sprintf "%s:%s" user password))))
  end

  type user = {
    username     : string;
    first_name   : string;
    last_name    : string;
    is_team      : bool;
    avatar       : string;
    resource_uri : string
  } with conv(json), sexp

  type resp = {
    user : user;
    rest (: Rest_in_raw :) : Json.t fields;
  } with conv(json), sexp

  let get ~user ~password = 
    let s = Curl.get_string (fun h ->
      h#set_url "https://api.bitbucket.org/1.0/user";
      h#set_userpwd (Printf.sprintf "%s:%s" user password))
    in
    try resp_of_json_exn (Json.parse s) with e -> 
      prerr_endline (Printexc.to_string e);
      prerr_endline s;
      raise e

end