lanonna/pam/bin/httpclient.ml

32 lines
1.1 KiB
OCaml
Raw Normal View History

2024-03-16 09:34:29 +01:00
open Pyops
open Pytypes
open Batteries
type http_actor = {requests: pyobject; url: pyobject; headers: pyobject}
let reminder_of_issue : Api.issue -> Datatypes.reminder option = function
| {due_date=None; _} -> None
| {url=url; title=title; due_date=Some due_date; body=_} -> Some {url=url; title=title; due_date=due_date}
let init () =
let _ = Py.initialize () in
let requests = Py.import "requests" in
let url = Py.String.of_string Api.url in
let headers =
Api.headers
|> List.map (fun (k, v) -> (k, Py.String.of_string v))
|> Py.Dict.of_bindings_string in
{requests=requests; url=url; headers=headers}
let make_get_request {requests; url; headers} =
let get = Py.Module.get_function_with_keywords requests "get" in
let resp = get [|url|] [("headers", headers)] in
let jsontext =
resp.@$("text")
|> Py.String.to_string
in
Api.issues_of_json jsontext
|> Result.map (Datatypes.forgejo_issues % List.filter_map reminder_of_issue)
|> Result.map_error Datatypes.forgejo_error (*TODO: maybe not really a forgejo error, more like internal *)