api: cleanup generic-request endpoints

This commit is contained in:
saces 2026-04-28 19:43:01 +02:00
parent 43bf804515
commit 7ed8e1c186
7 changed files with 39 additions and 26 deletions

View file

@ -245,16 +245,26 @@ 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)
//export cliv0_generic_request
func cliv0_generic_request(hs_url *C.char, access_token *C.char, method *C.char, path *C.char, data *C.char) *C.char {
var bup mautrix.BaseURLPath
err := json.Unmarshal([]byte(C.GoString(path)), &bup)
if err != nil {
return C.CString(fmt.Sprintf("ERR: %v\n%s", err, resp))
return returnErr(err)
}
return C.CString(resp)
mauclient, err := mautrix.NewClient(C.GoString(hs_url), "", C.GoString(access_token))
if err != nil {
return returnErr(err)
}
urlPath := mauclient.BuildURLWithFullQuery(bup, nil)
resp, err := mauclient.MakeFullRequest(context.Background(), mautrix.FullRequest{
Method: C.GoString(method), URL: urlPath, RequestBytes: []byte(C.GoString(data)), ResponseJSON: nil})
if err != nil {
return returnErr(err)
}
return C.CString(string(resp))
}
/*
@ -616,20 +626,19 @@ func apiv0_joinedrooms(cid C.int) *C.char {
return returnJSON(roomList, nil)
}
//export apiv0_genericrequest
func apiv0_genericrequest(cid C.int, method *C.char, path *C.char, data *C.char) *C.char {
//export apiv0_generic_request
func apiv0_generic_request(cid C.int, method *C.char, path *C.char, data *C.char) *C.char {
cli, err := getClient(int(cid))
if err != nil {
return C.CString(fmt.Sprintf("ERR: %v", err))
return returnErr(err)
}
var bup mautrix.BaseURLPath
err = json.Unmarshal([]byte(C.GoString(path)), &bup)
if err != nil {
return C.CString(fmt.Sprintf("ERR: %v", err))
return returnErr(err)
}
urlPath := cli.BuildURLWithFullQuery(mautrix.BaseURLPath(bup), nil)
d := C.GoString(data)
resp, err := cli.MakeFullRequest(context.Background(), mautrix.FullRequest{Method: C.GoString(method), URL: urlPath, RequestBytes: []byte(d), ResponseJSON: nil})
resp, err := cli.MakeFullRequest(context.Background(), mautrix.FullRequest{Method: C.GoString(method), URL: urlPath, RequestBytes: []byte(C.GoString(data)), ResponseJSON: nil})
return returnJSON(resp, err)
}

View file

@ -47,7 +47,7 @@ ffibuilder.cdef(
extern char* cliv0_clearaccount(char* hs, char* accessToken);
extern char* cliv0_serverinfo(char* url);
extern char* cliv0_mxpassitem(char* mxpassfile, char* hs, char* localpart, char* domain);
extern char* cliv0_genericrequest(char* hs, char* accessToken, char* req);
extern char* cliv0_generic_request(char* hs, char* access_token, char* method, char* path, char* data);
extern int apiv0_initialize();
extern int apiv0_deinitialize();
extern char* apiv0_discover(char* mxid);
@ -74,7 +74,7 @@ ffibuilder.cdef(
extern char* apiv0_joinroom(int cid, char* roomid);
extern char* apiv0_createroom(int cid, char* data);
extern char* apiv0_createdm(int cid, char* uid);
extern char* apiv0_genericrequest(int cid, char* method, char* path, char* data);
extern char* apiv0_generic_request(int cid, char* method, char* path, char* data);
extern char* apiv0_getuserdm(int cid, char* userid);
extern char* apiv0_self_sign(int cid);
extern int apiv0_removeclient(int cid);

View file

@ -6,7 +6,7 @@ apiv0_createroom
apiv0_createdm
apiv0_deinitialize
apiv0_discover
apiv0_genericrequest
apiv0_generic_request
apiv0_getoptions
apiv0_getuserdm
apiv0_initialize
@ -36,7 +36,7 @@ apiv0_self_sign
cliv0_accountinfo
cliv0_clearaccount
cliv0_discoverhs
cliv0_genericrequest
cliv0_generic_request
cliv0_mkmxtoken
cliv0_mxpassitem
cliv0_serverinfo

View file

@ -52,7 +52,7 @@ class ApiV0Api:
@staticmethod
def generic(cid, method, path, data):
return _stringresult(
lib.apiv0_genericrequest(
lib.apiv0_generic_request(
cid, _autostring(method), _autolist(path), _autodict(data)
)
)

View file

@ -96,7 +96,7 @@ class _AsyncClient:
r = ApiV0Api.createroom(self.client_id, data_dict)
return CheckApiResult(r)
async def generic(self, method, path, data):
async def generic(self, method, path, data=None):
r = ApiV0Api.generic(self.client_id, method, path, data)
return CheckApiErrorOnly(r)

View file

@ -3,7 +3,7 @@
from _pygomx import lib
from .errors import CheckApiResult
from .util import _stringresult, _autostring, _autodict
from .util import _stringresult, _autostring, _autodict, _autolist
class CliV0Api:
@ -29,11 +29,13 @@ class CliV0Api:
)
@staticmethod
def generic(hs_url, token, data):
def generic(hs_url, access_token, method, path, data):
return _stringresult(
lib.cliv0_genericrequest(
lib.cliv0_generic_request(
_autostring(hs_url),
_autostring(token),
_autostring(access_token),
_autostring(method),
_autolist(path),
_autodict(data),
)
)
@ -76,10 +78,10 @@ class CliV0:
res = CliV0Api.whoami(self.hs_url, self.token)
return CheckApiResult(res)
def Generic(self, data, omitt_token=False):
def Generic(self, method, path, data=None, omitt_token=False):
if omitt_token:
token = ""
else:
token = self.token
res = CliV0Api.generic(self.hs_url, token, data)
res = CliV0Api.generic(self.hs_url, token, method, path, data)
return CheckApiResult(res)

View file

@ -22,6 +22,8 @@ def _autostring(xstr):
def _autodict(xdict):
match xdict:
case None:
return b""
case bytes():
return xdict
case str():