|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- package api
-
- import (
- "fmt"
- "io"
- "net/url"
-
- "gitlink.org.cn/cloudream/common/consts/errorcode"
- "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
- "gitlink.org.cn/cloudream/common/utils/http2"
- "gitlink.org.cn/cloudream/common/utils/io2"
- "gitlink.org.cn/cloudream/common/utils/serder"
- )
-
- // TODO2 重新梳理代码
-
- const GetStreamPath = "/hubIO/getStream"
-
- type GetStreamReq struct {
- PlanID exec.PlanID `json:"planID"`
- VarID exec.VarID `json:"varID"`
- SignalID exec.VarID `json:"signalID"`
- Signal exec.VarValue `json:"signal"`
- }
-
- func (c *Client) GetStream(req GetStreamReq) (io.ReadCloser, error) {
- targetUrl, err := url.JoinPath(c.cfg.URL, GetStreamPath)
- if err != nil {
- return nil, err
- }
-
- body, err := serder.ObjectToJSONEx(req)
- if err != nil {
- return nil, fmt.Errorf("request to json: %w", err)
- }
-
- resp, err := http2.GetJSON(targetUrl, http2.RequestParam{
- Body: body,
- })
- if err != nil {
- return nil, err
- }
-
- cr := http2.NewChunkedReader(resp.Body)
- _, str, err := cr.NextPart()
- if err != nil {
- return nil, fmt.Errorf("reading response: %w", err)
- }
-
- return io2.DelegateReadCloser(str, func() error {
- cr.Close()
- return nil
- }), nil
- }
-
- const SendStreamPath = "/hubIO/sendStream"
-
- type SendStreamReq struct {
- SendStreamInfo
- Stream io.ReadCloser
- }
- type SendStreamInfo struct {
- PlanID exec.PlanID `json:"planID"`
- VarID exec.VarID `json:"varID"`
- }
-
- func (c *Client) SendStream(req SendStreamReq) error {
- targetUrl, err := url.JoinPath(c.cfg.URL, SendStreamPath)
- if err != nil {
- return err
- }
-
- pr, pw := io.Pipe()
- errCh := make(chan error, 1)
- go func() {
- cw := http2.NewChunkedWriter(pw)
-
- infoJSON, err := serder.ObjectToJSONEx(req)
- if err != nil {
- cw.Abort(fmt.Sprintf("info to json: %v", err))
- errCh <- fmt.Errorf("info to json: %w", err)
- return
- }
-
- if err := cw.WriteDataPart("info", infoJSON); err != nil {
- cw.Close()
- errCh <- fmt.Errorf("write info: %w", err)
- return
- }
-
- _, err = cw.WriteStreamPart("stream", req.Stream)
- if err != nil {
- cw.Close()
- errCh <- fmt.Errorf("write stream: %w", err)
- return
- }
-
- err = cw.Finish()
- if err != nil {
- errCh <- fmt.Errorf("finish chunked writer: %w", err)
- return
- }
- }()
-
- resp, err := http2.PostChunked2(targetUrl, http2.Chunked2RequestParam{
- Body: pr,
- })
- if err != nil {
- return err
- }
-
- err = <-errCh
- if err != nil {
- return err
- }
-
- codeResp, err := ParseJSONResponse[response[any]](resp)
- if err != nil {
- return err
- }
-
- if codeResp.Code == errorcode.OK {
- return nil
- }
-
- return codeResp.ToError()
- }
-
- const ExecuteIOPlanPath = "/hubIO/executeIOPlan"
-
- type ExecuteIOPlanReq struct {
- Plan exec.Plan `json:"plan"`
- }
-
- func (c *Client) ExecuteIOPlan(req ExecuteIOPlanReq) error {
- targetUrl, err := url.JoinPath(c.cfg.URL, ExecuteIOPlanPath)
- if err != nil {
- return err
- }
-
- body, err := serder.ObjectToJSONEx(req)
- if err != nil {
- return fmt.Errorf("request to json: %w", err)
- }
-
- resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
- Body: body,
- })
- if err != nil {
- return err
- }
-
- codeResp, err := ParseJSONResponse[response[any]](resp)
- if err != nil {
- return err
- }
-
- if codeResp.Code == errorcode.OK {
- return nil
- }
-
- return codeResp.ToError()
- }
-
- const SendVarPath = "/hubIO/sendVar"
-
- type SendVarReq struct {
- PlanID exec.PlanID `json:"planID"`
- VarID exec.VarID `json:"varID"`
- VarValue exec.VarValue `json:"varValue"`
- }
-
- func (c *Client) SendVar(req SendVarReq) error {
- targetUrl, err := url.JoinPath(c.cfg.URL, SendVarPath)
- if err != nil {
- return err
- }
-
- body, err := serder.ObjectToJSONEx(req)
- if err != nil {
- return fmt.Errorf("request to json: %w", err)
- }
-
- resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
- Body: body,
- })
- if err != nil {
- return err
- }
-
- jsonResp, err := ParseJSONResponse[response[any]](resp)
- if err != nil {
- return err
- }
-
- if jsonResp.Code == errorcode.OK {
- return nil
- }
-
- return jsonResp.ToError()
- }
-
- const GetVarPath = "/hubIO/getVar"
-
- type GetVarReq struct {
- PlanID exec.PlanID `json:"planID"`
- VarID exec.VarID `json:"varID"`
- SignalID exec.VarID `json:"signalID"`
- Signal exec.VarValue `json:"signal"`
- }
-
- type GetVarResp struct {
- Value exec.VarValue `json:"value"`
- }
-
- func (c *Client) GetVar(req GetVarReq) (*GetVarResp, error) {
- targetUrl, err := url.JoinPath(c.cfg.URL, GetVarPath)
- if err != nil {
- return nil, err
- }
-
- body, err := serder.ObjectToJSONEx(req)
- if err != nil {
- return nil, fmt.Errorf("request to json: %w", err)
- }
-
- resp, err := http2.GetJSON(targetUrl, http2.RequestParam{
- Body: body,
- })
- if err != nil {
- return nil, err
- }
-
- jsonResp, err := ParseJSONResponse[response[GetVarResp]](resp)
- if err != nil {
- return nil, err
- }
-
- if jsonResp.Code == errorcode.OK {
- return &jsonResp.Data, nil
- }
-
- return nil, jsonResp.ToError()
- }
|