Skip to content

Commit 9e5d3e1

Browse files
fit2botLeeEirc
andauthored
perf: add call api (#59)
* perf: fix conflict * perf: update call api --------- Co-authored-by: Eric <xplzv@126.com>
1 parent 97404e8 commit 9e5d3e1

17 files changed

Lines changed: 5128 additions & 1577 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ proto_go_dir=./protobuf-go
7777
protobuf_py_dir=./protobuf-py/protobuf
7878

7979
.PHONY: proto
80-
proto: proto-go proto-java proto-rust
80+
proto: proto-go proto-java proto-rust proto-py
8181

8282
.PHONY: proto-go
8383
proto-go:

cmd/impl/jms_chat.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package impl
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67

@@ -47,3 +48,35 @@ func (j *JMServer) GetAccountChat(ctx context.Context, req *pb.Empty) (*pb.Accou
4748
status.Ok = true
4849
return &pb.AccountDetailResponse{Status: &status, Payload: st}, nil
4950
}
51+
52+
func (j *JMServer) CallAPI(ctx context.Context, req *pb.HTTPRequest) (*pb.HTTPResponse, error) {
53+
method := req.GetMethod()
54+
reqUrl := req.GetPath()
55+
query := req.GetQuery()
56+
body := req.GetBody()
57+
header := req.GetHeader()
58+
apiClient := j.apiClient.Copy()
59+
var (
60+
status pb.Status
61+
)
62+
for k, v := range header {
63+
apiClient.SetHeader(k, v)
64+
}
65+
var bodyObj any
66+
if len(body) > 0 {
67+
if err := json.NewDecoder(bytes.NewBuffer(body)).Decode(&bodyObj); err != nil {
68+
logger.Errorf("Decode body failed: %v", err)
69+
status.Err = err.Error()
70+
return &pb.HTTPResponse{Status: &status}, err
71+
}
72+
}
73+
var resJson bytes.Buffer
74+
_, err := apiClient.Call(method, reqUrl, bodyObj, &resJson, query)
75+
if err != nil {
76+
logger.Errorf("Call: %v", err)
77+
status.Err = err.Error()
78+
return &pb.HTTPResponse{Status: &status}, err
79+
}
80+
status.Ok = true
81+
return &pb.HTTPResponse{Status: &status, Body: resJson.Bytes()}, nil
82+
}

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
6363
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
6464
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
6565
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
66-
github.com/jumpserver-dev/sdk-go v0.0.0-20250925081620-d22b2b8e24d1 h1:6/d5CAJvnHwEiPmY7Pm5XgvIHeHATe/apnx9dmQR1yQ=
67-
github.com/jumpserver-dev/sdk-go v0.0.0-20250925081620-d22b2b8e24d1/go.mod h1:CDNCGOGgeudYS4IKsHOG+ma1rBSbjv8toKtRax2Z0EE=
6866
github.com/jumpserver-dev/sdk-go v0.0.0-20251007162206-113f4e3fdde6 h1:5DZ9IYsnu8E5H0JVwQWP2wRLQioO8X4ScFqvi0itc1E=
6967
github.com/jumpserver-dev/sdk-go v0.0.0-20251007162206-113f4e3fdde6/go.mod h1:CDNCGOGgeudYS4IKsHOG+ma1rBSbjv8toKtRax2Z0EE=
7068
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=

0 commit comments

Comments
 (0)