lanonna/pam/bin/httpclient.ml

68 lines
2.4 KiB
OCaml
Raw Normal View History

2024-03-16 09:34:29 +01:00
open Pyops
open Pytypes
open Batteries
2024-04-02 14:38:00 +02:00
open Yojson.Safe
open Config
open Utils
2024-03-16 09:34:29 +01:00
2024-04-02 14:38:00 +02:00
let forgejo_url repo_id = [%string
"https://salsa.lezzo.org/api/v1/repos/%{repo_id}/issues?state=open&type=issues"]
2024-03-16 09:34:29 +01:00
2024-04-02 14:38:00 +02:00
let issue_of_json (m_room: Datatypes.MatrixRoom.t) (json): Datatypes.reminder option =
let open Yojson.Safe.Util in
let due_date = json |> member "due_date" |> to_option to_string in
match due_date with
| None -> None
| Some due_date ->
let record: Datatypes.reminder = {
url = json |> member "url" |> to_string;
title = json |> member "title" |> to_string;
body = json |> member "body" |> to_string;
matrix_target = m_room;
due_date = due_date
}
in Some record
2024-03-16 09:34:29 +01:00
2024-04-02 14:38:00 +02:00
let issues_of_json matrix_room json_str =
let open Yojson.Safe.Util in
try json_str |> from_string |> to_list |> List.map (issue_of_json matrix_room) |> List.filter_map identity |> Result.ok
with | Yojson.Json_error msg -> Error [%string "JSON parsing error: %{msg}"]
type repo_pytuple = {url: pyobject; headers: pyobject}
type http_actor = {requests: pyobject; repos: repo_pytuple StringMap.t}
let make_headers base64_password =
let headers =
[("accept", "application/json");
("authorization", [%string "Basic %{base64_password}"])] in
headers
2024-03-16 09:34:29 +01:00
|> List.map (fun (k, v) -> (k, Py.String.of_string v))
2024-04-02 14:38:00 +02:00
|> Py.Dict.of_bindings_string
2024-03-16 09:34:29 +01:00
2024-04-02 14:38:00 +02:00
let init (repos: Config.repo_data list) =
let _ = Py.initialize () in
let requests = Py.import "requests" in
let urls =
repos
|> List.map (fun {forgejo_id; base64_password; matrix_room} ->
(matrix_room, {url=forgejo_url forgejo_id |> Py.String.of_string; headers=make_headers base64_password}))
|> StringMap.of_list
in
{requests=requests; repos=urls}
2024-03-16 09:34:29 +01:00
2024-04-02 14:38:00 +02:00
let make_get_request {requests; repos} =
2024-03-16 09:34:29 +01:00
let get = Py.Module.get_function_with_keywords requests "get" in
2024-04-02 14:38:00 +02:00
let fold_fn = (fun m_room_string {url; headers} acc ->
let resp = get [|url|] [("headers", headers)] in
let jsontext =
resp.@$("text")
|> Py.String.to_string
in
let matrix_room = Datatypes.MatrixRoom.make m_room_string in
let value =
issues_of_json matrix_room jsontext
|> Result.map Datatypes.forgejo_issues
|> Result.map_error Datatypes.forgejo_error (*TODO: maybe not really a forgejo error, more like internal *)
in value::acc)
in StringMap.fold fold_fn repos []