Skip to content

Commit

Permalink
refactor(authentication): 优化grpc接口
Browse files Browse the repository at this point in the history
  • Loading branch information
conifercone committed Dec 10, 2024
1 parent cd66a59 commit 9e8b13a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import baby.mumu.authentication.client.api.grpc.RoleFindAllGrpcCmd;
import baby.mumu.authentication.client.api.grpc.RoleFindAllGrpcCo;
import baby.mumu.authentication.client.api.grpc.RoleFindByIdGrpcCo;
import baby.mumu.authentication.client.api.grpc.RoleId;
import baby.mumu.authentication.client.api.grpc.RoleServiceGrpc.RoleServiceImplBase;
import baby.mumu.authentication.client.dto.RoleAddAncestorCmd;
import baby.mumu.authentication.client.dto.RoleAddCmd;
Expand Down Expand Up @@ -191,8 +190,8 @@ public void findAll(RoleFindAllGrpcCmd request,
}

@Override
public void findById(RoleId request, StreamObserver<RoleFindByIdGrpcCo> responseObserver) {
Optional.ofNullable(request).filter(RoleId::hasId).map(RoleId::getId).map(Int64Value::getValue)
public void findById(Int64Value request, StreamObserver<RoleFindByIdGrpcCo> responseObserver) {
Optional.ofNullable(request).filter(Int64Value::isInitialized).map(Int64Value::getValue)
.map(
roleFindByIdCmdExe::execute).flatMap(roleConvertor::toRoleFindByIdGrpcCo)
.ifPresentOrElse((roleFindByIdGrpcCo) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import baby.mumu.authentication.client.api.grpc.PageOfRoleFindAllGrpcCo;
import baby.mumu.authentication.client.api.grpc.RoleFindAllGrpcCmd;
import baby.mumu.authentication.client.api.grpc.RoleFindByIdGrpcCo;
import baby.mumu.authentication.client.api.grpc.RoleId;
import baby.mumu.authentication.client.api.grpc.RoleServiceGrpc;
import baby.mumu.authentication.client.api.grpc.RoleServiceGrpc.RoleServiceBlockingStub;
import baby.mumu.authentication.client.api.grpc.RoleServiceGrpc.RoleServiceFutureStub;
import baby.mumu.basis.exception.MuMuException;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.protobuf.Int64Value;
import io.grpc.CallCredentials;
import io.grpc.ManagedChannel;
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor;
Expand Down Expand Up @@ -84,7 +84,7 @@ public ListenableFuture<PageOfRoleFindAllGrpcCo> syncFindAll(
}

@API(status = Status.STABLE, since = "2.4.0")
public RoleFindByIdGrpcCo findById(RoleId roleId,
public RoleFindByIdGrpcCo findById(Int64Value roleId,
CallCredentials callCredentials) {
return Optional.ofNullable(channel)
.or(this::getManagedChannelUsePlaintext)
Expand All @@ -97,7 +97,7 @@ public RoleFindByIdGrpcCo findById(RoleId roleId,

@API(status = Status.STABLE, since = "2.4.0")
public ListenableFuture<RoleFindByIdGrpcCo> syncFindById(
RoleId roleId,
Int64Value roleId,
CallCredentials callCredentials) {
return Optional.ofNullable(channel)
.or(this::getManagedChannelUsePlaintext)
Expand Down Expand Up @@ -126,15 +126,15 @@ private PageOfRoleFindAllGrpcCo findAllFromGrpc(
}

private RoleFindByIdGrpcCo findByIdFromGrpc(
RoleId roleId,
Int64Value roleId,
CallCredentials callCredentials) {
RoleServiceBlockingStub roleServiceBlockingStub = RoleServiceGrpc.newBlockingStub(channel);
return roleServiceBlockingStub.withCallCredentials(callCredentials)
.findById(roleId);
}

private @NotNull ListenableFuture<RoleFindByIdGrpcCo> syncFindByIdFromGrpc(
RoleId roleId,
Int64Value roleId,
CallCredentials callCredentials) {
RoleServiceFutureStub roleServiceFutureStub = RoleServiceGrpc.newFutureStub(
channel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@ message PermissionFindAllGrpcCmd {
google.protobuf.Int64Value id = 1;
google.protobuf.StringValue code = 2;
google.protobuf.StringValue name = 3;
google.protobuf.Int32Value current = 4;
google.protobuf.Int32Value page_size = 5;
google.protobuf.StringValue description = 4;
google.protobuf.Int32Value current = 5;
google.protobuf.Int32Value page_size = 6;
}

message PermissionFindAllGrpcCo {
google.protobuf.Int64Value id = 1;
google.protobuf.StringValue code = 2;
google.protobuf.StringValue name = 3;
google.protobuf.StringValue description = 4;
bool hasDescendant = 5;
}

message PermissionFindByIdGrpcCo {
google.protobuf.Int64Value id = 1;
google.protobuf.StringValue code = 2;
google.protobuf.StringValue name = 3;
google.protobuf.StringValue description = 4;
bool hasDescendant = 5;
}

message PageOfPermissionFindAllGrpcCo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ option java_outer_classname = "RoleServiceProto";

service RoleService {
rpc findAll (RoleFindAllGrpcCmd) returns (PageOfRoleFindAllGrpcCo);
rpc findById (RoleId) returns (RoleFindByIdGrpcCo);
rpc findById (google.protobuf.Int64Value) returns (RoleFindByIdGrpcCo);
}

message RoleFindAllGrpcCmd {
google.protobuf.Int64Value id = 1;
google.protobuf.StringValue code = 2;
google.protobuf.StringValue name = 3;
google.protobuf.Int32Value current = 4;
google.protobuf.Int32Value page_size = 5;
}

message RoleId {
google.protobuf.Int64Value id = 1;
google.protobuf.StringValue description = 4;
google.protobuf.Int32Value current = 5;
google.protobuf.Int32Value page_size = 6;
}

message RoleFindByIdGrpcCo {
google.protobuf.Int64Value id = 1;
google.protobuf.StringValue code = 2;
google.protobuf.StringValue name = 3;
bool hasDescendant = 4;
google.protobuf.StringValue description = 4;
bool hasDescendant = 5;
}

message RoleFindAllGrpcCo {
google.protobuf.Int64Value id = 1;
google.protobuf.StringValue code = 2;
google.protobuf.StringValue name = 3;
repeated RoleFindAllPermissionGrpcCo permissions = 4;
bool hasDescendant = 5;
google.protobuf.StringValue description = 5;
bool hasDescendant = 6;
}

message RoleFindAllPermissionGrpcCo {
google.protobuf.Int64Value id = 1;
google.protobuf.StringValue code = 2;
google.protobuf.StringValue name = 3;
google.protobuf.StringValue description = 4;
bool hasDescendant = 5;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import baby.mumu.authentication.client.api.grpc.PageOfRoleFindAllGrpcCo;
import baby.mumu.authentication.client.api.grpc.RoleFindAllGrpcCmd;
import baby.mumu.authentication.client.api.grpc.RoleFindByIdGrpcCo;
import baby.mumu.authentication.client.api.grpc.RoleId;
import baby.mumu.basis.exception.MuMuException;
import baby.mumu.basis.response.ResponseCode;
import com.google.common.util.concurrent.ListenableFuture;
Expand Down Expand Up @@ -114,7 +113,7 @@ public void findById() {
() -> getToken(mockMvc).orElseThrow(
() -> new MuMuException(ResponseCode.INTERNAL_SERVER_ERROR)));
RoleFindByIdGrpcCo roleFindByIdGrpcCo = roleGrpcService.findById(
RoleId.newBuilder().setId(Int64Value.of(0L)).build(),
Int64Value.of(0L),
callCredentials);
logger.info("RoleFindByIdGrpcCo: {}", roleFindByIdGrpcCo);
Assertions.assertNotNull(roleFindByIdGrpcCo);
Expand All @@ -127,7 +126,7 @@ public void syncFindById() throws InterruptedException {
() -> getToken(mockMvc).orElseThrow(
() -> new MuMuException(ResponseCode.INTERNAL_SERVER_ERROR)));
ListenableFuture<RoleFindByIdGrpcCo> roleFindByIdGrpcCoListenableFuture = roleGrpcService.syncFindById(
RoleId.newBuilder().setId(Int64Value.of(0L)).build(),
Int64Value.of(0L),
callCredentials);
roleFindByIdGrpcCoListenableFuture.addListener(() -> {
try {
Expand Down

0 comments on commit 9e8b13a

Please sign in to comment.