-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardSetGrpcClient.java
More file actions
65 lines (55 loc) · 2.12 KB
/
CardSetGrpcClient.java
File metadata and controls
65 lines (55 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package flipnote.reaction.common.grpc;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;
import cardset.Cardset.CardSetSummary;
import cardset.Cardset.GetCardSetsByIdsRequest;
import cardset.Cardset.GetCardSetsByIdsResponse;
import cardset.Cardset.IsCardSetViewableRequest;
import cardset.Cardset.IsCardSetViewableResponse;
import cardset.CardsetServiceGrpc;
import flipnote.reaction.common.exception.BizException;
import flipnote.reaction.common.exception.CommonErrorCode;
import io.grpc.ManagedChannel;
import io.grpc.StatusRuntimeException;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class CardSetGrpcClient {
private final CardsetServiceGrpc.CardsetServiceBlockingStub stub;
public CardSetGrpcClient(ManagedChannel cardSetChannel) {
this.stub = CardsetServiceGrpc.newBlockingStub(cardSetChannel);
}
public boolean isCardSetViewable(Long cardSetId, Long userId) {
try {
IsCardSetViewableRequest request = IsCardSetViewableRequest.newBuilder()
.setCardSetId(cardSetId.intValue())
.setUserId(userId.intValue())
.build();
IsCardSetViewableResponse response = stub.isCardSetViewable(request);
return response.getViewable();
} catch (StatusRuntimeException e) {
log.error("gRPC call failed: IsCardSetViewable, cardSetId={}, userId={}", cardSetId, userId, e);
throw new BizException(CommonErrorCode.GRPC_CALL_FAILED);
}
}
public Map<Long, CardSetSummary> getCardSetsByIds(List<Long> cardSetIds, Long userId) {
try {
GetCardSetsByIdsRequest request = GetCardSetsByIdsRequest.newBuilder()
.addAllCardSetIds(cardSetIds)
.setUserId(userId.intValue())
.build();
GetCardSetsByIdsResponse response = stub.getCardSetsByIds(request);
return response.getCardSetsList().stream()
.collect(Collectors.toMap(
cs -> (long) cs.getId(),
Function.identity()
));
} catch (StatusRuntimeException e) {
log.error("gRPC call failed: GetCardSetsByIds, cardSetIds={}, userId={}", cardSetIds, userId, e);
throw new BizException(CommonErrorCode.GRPC_CALL_FAILED);
}
}
}