mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-09 10:00:27 +00:00
feat: move native communication to FFI (#1457)
This commit is contained in:
86 files changed
+4558
-886
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
goapi "github.com/GopeedLab/gopeed/pkg/api"
|
||||
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||
)
|
||||
|
||||
const (
|
||||
TaskEventDone = uint64(goapi.TaskEventDone)
|
||||
TaskEventError = uint64(goapi.TaskEventError)
|
||||
)
|
||||
|
||||
// Invoke dispatches a Core API request without using an HTTP listener. It is
|
||||
// the shared entry point for Desktop FFI and gomobile bindings.
|
||||
func Invoke(method, path, rawQuery, body string) string {
|
||||
service := APIService
|
||||
if service == nil {
|
||||
return marshalInvokeResult(model.NewErrorResult("service not started"))
|
||||
}
|
||||
query, err := url.ParseQuery(rawQuery)
|
||||
if err != nil {
|
||||
return marshalInvokeResult(model.NewErrorResult(err.Error()))
|
||||
}
|
||||
_, data := service.JSON(method, path, query, []byte(body), nil)
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func SubscribeTaskEvents(mask uint64, listener func(payload string)) {
|
||||
if APIService == nil {
|
||||
return
|
||||
}
|
||||
APIService.SubscribeTaskEvents(goapi.TaskEventMask(mask), listener)
|
||||
}
|
||||
|
||||
func marshalInvokeResult(value any) string {
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return `{"code":1000,"msg":"marshal response failed"}`
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
Reference in new issue
Block a user