open Yojson.Safe

let url = "https://bugs.lezzo.org/api/v1/repos/bparodi/Documenti/issues?state=open&type=issues"

let headers =
  let base64password = Datatypes.forgejo in
  [("accept", "application/json");
   ("authorization", [%string "Basic %{base64password}"])]

type issue = {
  url: string;
  title: string;
  body: string;
  due_date: string option;
}
and issues = issue list

let issue_of_json json =
  let open Yojson.Safe.Util in
  {
    url = json |> member "url" |> to_string;
    title = json |> member "title" |> to_string;
    body = json |> member "body" |> to_string;
    due_date = json |> member "due_date" |> to_option to_string;
  }

let string_of_issue issue =
  let due_date_str = match issue.due_date with
    | Some date -> date
    | None -> ""
  in
  [%string {|{ url="%{issue.url}"; title="%{issue.title}"; body = "%{issue.body}"; due_date=%{due_date_str} }|}]

let issues_of_json json_str =
  let open Yojson.Safe.Util in
  try json_str |> from_string |> to_list |> List.map issue_of_json |> Result.ok
  with | Yojson.Json_error msg -> Error [%string "JSON parsing error: %{msg}"]