Skip to content

Commit ccb47f0

Browse files
committed
perf: add call api
1 parent 9cdcf77 commit ccb47f0

14 files changed

Lines changed: 2808 additions & 254 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.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/elastic/go-elasticsearch/v8 v8.14.0
1111
github.com/gorilla/websocket v1.5.3
1212
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.25.4+incompatible
13-
github.com/jumpserver-dev/sdk-go v0.0.0-20250925081620-d22b2b8e24d1
13+
github.com/jumpserver-dev/sdk-go v0.0.0-20250930082009-cdf7b54d5161
1414
github.com/sirupsen/logrus v1.9.3
1515
github.com/spf13/cobra v1.10.1
1616
github.com/spf13/viper v1.21.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ 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=
66+
github.com/jumpserver-dev/sdk-go v0.0.0-20250930082009-cdf7b54d5161 h1:V+DA/HInwngzqZK7OxZl9OAhiRzQV0UWlCq1HJBCnWA=
67+
github.com/jumpserver-dev/sdk-go v0.0.0-20250930082009-cdf7b54d5161/go.mod h1:CDNCGOGgeudYS4IKsHOG+ma1rBSbjv8toKtRax2Z0EE=
6868
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
6969
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
7070
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=

protobuf-go/protobuf/service.pb.go

Lines changed: 392 additions & 197 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protobuf-go/protobuf/service_grpc.pb.go

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protobuf-java/org/jumpserver/wisp/ServiceGrpc.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,37 @@ org.jumpserver.wisp.ServiceOuterClass.AccountDetailResponse> getGetAccountChatMe
759759
return getGetAccountChatMethod;
760760
}
761761

762+
private static volatile io.grpc.MethodDescriptor<org.jumpserver.wisp.ServiceOuterClass.HTTPRequest,
763+
org.jumpserver.wisp.ServiceOuterClass.HTTPResponse> getCallAPIMethod;
764+
765+
@io.grpc.stub.annotations.RpcMethod(
766+
fullMethodName = SERVICE_NAME + '/' + "CallAPI",
767+
requestType = org.jumpserver.wisp.ServiceOuterClass.HTTPRequest.class,
768+
responseType = org.jumpserver.wisp.ServiceOuterClass.HTTPResponse.class,
769+
methodType = io.grpc.MethodDescriptor.MethodType.UNARY)
770+
public static io.grpc.MethodDescriptor<org.jumpserver.wisp.ServiceOuterClass.HTTPRequest,
771+
org.jumpserver.wisp.ServiceOuterClass.HTTPResponse> getCallAPIMethod() {
772+
io.grpc.MethodDescriptor<org.jumpserver.wisp.ServiceOuterClass.HTTPRequest, org.jumpserver.wisp.ServiceOuterClass.HTTPResponse> getCallAPIMethod;
773+
if ((getCallAPIMethod = ServiceGrpc.getCallAPIMethod) == null) {
774+
synchronized (ServiceGrpc.class) {
775+
if ((getCallAPIMethod = ServiceGrpc.getCallAPIMethod) == null) {
776+
ServiceGrpc.getCallAPIMethod = getCallAPIMethod =
777+
io.grpc.MethodDescriptor.<org.jumpserver.wisp.ServiceOuterClass.HTTPRequest, org.jumpserver.wisp.ServiceOuterClass.HTTPResponse>newBuilder()
778+
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
779+
.setFullMethodName(generateFullMethodName(SERVICE_NAME, "CallAPI"))
780+
.setSampledToLocalTracing(true)
781+
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
782+
org.jumpserver.wisp.ServiceOuterClass.HTTPRequest.getDefaultInstance()))
783+
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
784+
org.jumpserver.wisp.ServiceOuterClass.HTTPResponse.getDefaultInstance()))
785+
.setSchemaDescriptor(new ServiceMethodDescriptorSupplier("CallAPI"))
786+
.build();
787+
}
788+
}
789+
}
790+
return getCallAPIMethod;
791+
}
792+
762793
/**
763794
* Creates a new async stub that supports all call types for the service
764795
*/
@@ -974,6 +1005,13 @@ default void getAccountChat(org.jumpserver.wisp.ServiceOuterClass.Empty request,
9741005
io.grpc.stub.StreamObserver<org.jumpserver.wisp.ServiceOuterClass.AccountDetailResponse> responseObserver) {
9751006
io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getGetAccountChatMethod(), responseObserver);
9761007
}
1008+
1009+
/**
1010+
*/
1011+
default void callAPI(org.jumpserver.wisp.ServiceOuterClass.HTTPRequest request,
1012+
io.grpc.stub.StreamObserver<org.jumpserver.wisp.ServiceOuterClass.HTTPResponse> responseObserver) {
1013+
io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getCallAPIMethod(), responseObserver);
1014+
}
9771015
}
9781016

9791017
/**
@@ -1194,6 +1232,14 @@ public void getAccountChat(org.jumpserver.wisp.ServiceOuterClass.Empty request,
11941232
io.grpc.stub.ClientCalls.asyncUnaryCall(
11951233
getChannel().newCall(getGetAccountChatMethod(), getCallOptions()), request, responseObserver);
11961234
}
1235+
1236+
/**
1237+
*/
1238+
public void callAPI(org.jumpserver.wisp.ServiceOuterClass.HTTPRequest request,
1239+
io.grpc.stub.StreamObserver<org.jumpserver.wisp.ServiceOuterClass.HTTPResponse> responseObserver) {
1240+
io.grpc.stub.ClientCalls.asyncUnaryCall(
1241+
getChannel().newCall(getCallAPIMethod(), getCallOptions()), request, responseObserver);
1242+
}
11971243
}
11981244

11991245
/**
@@ -1372,6 +1418,13 @@ public org.jumpserver.wisp.ServiceOuterClass.AccountDetailResponse getAccountCha
13721418
return io.grpc.stub.ClientCalls.blockingUnaryCall(
13731419
getChannel(), getGetAccountChatMethod(), getCallOptions(), request);
13741420
}
1421+
1422+
/**
1423+
*/
1424+
public org.jumpserver.wisp.ServiceOuterClass.HTTPResponse callAPI(org.jumpserver.wisp.ServiceOuterClass.HTTPRequest request) {
1425+
return io.grpc.stub.ClientCalls.blockingUnaryCall(
1426+
getChannel(), getCallAPIMethod(), getCallOptions(), request);
1427+
}
13751428
}
13761429

13771430
/**
@@ -1573,6 +1626,14 @@ public com.google.common.util.concurrent.ListenableFuture<org.jumpserver.wisp.Se
15731626
return io.grpc.stub.ClientCalls.futureUnaryCall(
15741627
getChannel().newCall(getGetAccountChatMethod(), getCallOptions()), request);
15751628
}
1629+
1630+
/**
1631+
*/
1632+
public com.google.common.util.concurrent.ListenableFuture<org.jumpserver.wisp.ServiceOuterClass.HTTPResponse> callAPI(
1633+
org.jumpserver.wisp.ServiceOuterClass.HTTPRequest request) {
1634+
return io.grpc.stub.ClientCalls.futureUnaryCall(
1635+
getChannel().newCall(getCallAPIMethod(), getCallOptions()), request);
1636+
}
15761637
}
15771638

15781639
private static final int METHODID_GET_TOKEN_AUTH_INFO = 0;
@@ -1598,7 +1659,8 @@ public com.google.common.util.concurrent.ListenableFuture<org.jumpserver.wisp.Se
15981659
private static final int METHODID_FACE_MONITOR_CALLBACK = 20;
15991660
private static final int METHODID_JOIN_FACE_MONITOR = 21;
16001661
private static final int METHODID_GET_ACCOUNT_CHAT = 22;
1601-
private static final int METHODID_DISPATCH_TASK = 23;
1662+
private static final int METHODID_CALL_API = 23;
1663+
private static final int METHODID_DISPATCH_TASK = 24;
16021664

16031665
private static final class MethodHandlers<Req, Resp> implements
16041666
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
@@ -1709,6 +1771,10 @@ public void invoke(Req request, io.grpc.stub.StreamObserver<Resp> responseObserv
17091771
serviceImpl.getAccountChat((org.jumpserver.wisp.ServiceOuterClass.Empty) request,
17101772
(io.grpc.stub.StreamObserver<org.jumpserver.wisp.ServiceOuterClass.AccountDetailResponse>) responseObserver);
17111773
break;
1774+
case METHODID_CALL_API:
1775+
serviceImpl.callAPI((org.jumpserver.wisp.ServiceOuterClass.HTTPRequest) request,
1776+
(io.grpc.stub.StreamObserver<org.jumpserver.wisp.ServiceOuterClass.HTTPResponse>) responseObserver);
1777+
break;
17121778
default:
17131779
throw new AssertionError();
17141780
}
@@ -1898,6 +1964,13 @@ public static final io.grpc.ServerServiceDefinition bindService(AsyncService ser
18981964
org.jumpserver.wisp.ServiceOuterClass.Empty,
18991965
org.jumpserver.wisp.ServiceOuterClass.AccountDetailResponse>(
19001966
service, METHODID_GET_ACCOUNT_CHAT)))
1967+
.addMethod(
1968+
getCallAPIMethod(),
1969+
io.grpc.stub.ServerCalls.asyncUnaryCall(
1970+
new MethodHandlers<
1971+
org.jumpserver.wisp.ServiceOuterClass.HTTPRequest,
1972+
org.jumpserver.wisp.ServiceOuterClass.HTTPResponse>(
1973+
service, METHODID_CALL_API)))
19011974
.build();
19021975
}
19031976

@@ -1970,6 +2043,7 @@ public static io.grpc.ServiceDescriptor getServiceDescriptor() {
19702043
.addMethod(getFaceMonitorCallbackMethod())
19712044
.addMethod(getJoinFaceMonitorMethod())
19722045
.addMethod(getGetAccountChatMethod())
2046+
.addMethod(getCallAPIMethod())
19732047
.build();
19742048
}
19752049
}

0 commit comments

Comments
 (0)