add generic request

This commit is contained in:
saces 2026-03-16 09:48:30 +01:00
parent bf3d383615
commit 442aaf0056
3 changed files with 45 additions and 0 deletions

View file

@ -192,6 +192,18 @@ func cliv0_mxpassitem(mxpassfile_path *C.char, url *C.char, localpart *C.char, d
return C.CString(s)
}
//export cliv0_genericrequest
func cliv0_genericrequest(hs *C.char, tk *C.char, reqData *C.char) *C.char {
_hs := C.GoString(hs)
_tk := C.GoString(tk)
_reqData := C.GoString(reqData)
resp, err := mxutils.GenericRequest(_hs, _tk, _reqData)
if err != nil {
return C.CString(fmt.Sprintf("ERR: %v\n%s", err, resp))
}
return C.CString(resp)
}
/*
high level api, supports multiple clients
the same handler can be attached to multiple clients

View file

@ -0,0 +1,32 @@
// Copyright (C) 2026 saces@c-base.org
// SPDX-License-Identifier: AGPL-3.0-only
package mxutils
import (
"context"
"encoding/json"
"maunium.net/go/mautrix"
)
type req_data struct {
Method string `json:"method"`
Path mautrix.BaseURLPath `json:"path"`
Payload any `json:"payload"`
}
func GenericRequest(hs string, accessToken string, reqData string) (string, error) {
var rd req_data
err := json.Unmarshal([]byte(reqData), &rd)
if err != nil {
return "", err
}
mauclient, err := mautrix.NewClient(hs, "", accessToken)
if err != nil {
return "", err
}
urlPath := mauclient.BuildURLWithFullQuery(mautrix.BaseURLPath(rd.Path), nil)
resp, err := mauclient.MakeRequest(context.Background(), rd.Method, urlPath, rd.Payload, nil)
return string(resp), err
}