Source

ocaml-bitbucket / api.ml

Full commit
open Common
open Json_wrap
open Json_conv
open Sexplib.Conv

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

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_io.json_of_string (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 :) : (string * Json.t) list
  } 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 (Json_io.json_of_string s) with e -> 
      prerr_endline (Printexc.to_string e);
      prerr_endline s;
      raise e

end