2024-06-24 14:29:40 +00:00
|
|
|
package connector
|
|
|
|
|
2024-07-12 18:12:14 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type API struct {
|
|
|
|
s *Service
|
|
|
|
r *CommandRegistry
|
|
|
|
}
|
|
|
|
|
2024-06-24 14:29:40 +00:00
|
|
|
func NewAPI(s *Service) *API {
|
2024-07-12 18:12:14 +00:00
|
|
|
r := NewCommandRegistry()
|
|
|
|
|
2024-06-24 14:29:40 +00:00
|
|
|
return &API{
|
|
|
|
s: s,
|
2024-07-12 18:12:14 +00:00
|
|
|
r: r,
|
2024-06-24 14:29:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 18:12:14 +00:00
|
|
|
type RPCRequest struct {
|
|
|
|
JSONRPC string `json:"jsonrpc"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
Params []interface{} `json:"params"`
|
2024-06-24 14:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) CallRPC(inputJSON string) (string, error) {
|
2024-07-12 18:12:14 +00:00
|
|
|
var request RPCRequest
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(inputJSON), &request)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error unmarshalling JSON: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if command, exists := api.r.GetCommand(request.Method); exists {
|
|
|
|
return command.Execute(inputJSON)
|
|
|
|
}
|
|
|
|
|
2024-06-24 14:29:40 +00:00
|
|
|
return api.s.rpcClient.CallRaw(inputJSON), nil
|
|
|
|
}
|