48 lines
984 B
Go
48 lines
984 B
Go
package okrpc
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
RpcRequestUrl = "http://127.0.0.1:8080"
|
|
)
|
|
|
|
type Request struct {
|
|
Class string `json:"class"`
|
|
Method string `json:"method"`
|
|
Args []interface{} `json:"args"`
|
|
}
|
|
|
|
type Response struct {
|
|
Success bool `json:"success"`
|
|
ErrorCode string `json:"errorCode,omitempty"`
|
|
ErrorMessage string `json:"errorMessage,omitempty"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
func (r Request) SendRpcRequest() (string, error) {
|
|
data, err := json.Marshal(r)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
request, err := http.NewRequest("POST", RpcRequestUrl, bytes.NewBuffer(data))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
|
client := &http.Client{}
|
|
resp, err := client.Do(request)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(body), nil
|
|
}
|