pb_study_client.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package service
  2. import (
  3. "context"
  4. "log/slog"
  5. "time"
  6. )
  7. import (
  8. "google.golang.org/grpc"
  9. "google.golang.org/grpc/credentials/insecure"
  10. "google.golang.org/protobuf/types/known/emptypb"
  11. )
  12. import (
  13. "auth-server/common"
  14. "auth-server/logger"
  15. pb "auth-server/rpc_idl/dr_study_pb"
  16. )
  17. var StudyClient *Study
  18. func init() {
  19. StudyClient = new(Study)
  20. }
  21. type Study struct {
  22. basicClient pb.BasicClient
  23. studyClient pb.StudyClient
  24. }
  25. func (s *Study) Setup() {
  26. conn, err := grpc.NewClient(common.ServerConfig.Study,
  27. grpc.WithTransportCredentials(insecure.NewCredentials()),
  28. grpc.WithUnaryInterceptor(logger.GRPCLoggerClientInterceptor(logger.WithGroup("grpc_c[study]"))),
  29. )
  30. if err != nil {
  31. slog.Error("NewClient failed:", err)
  32. panic(err)
  33. }
  34. slog.Info("study conn success", "conn", conn.GetState())
  35. s.basicClient = pb.NewBasicClient(conn)
  36. s.studyClient = pb.NewStudyClient(conn)
  37. }
  38. func (s *Study) GetSoftwareInfo() *pb.SoftwareInfoReply {
  39. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
  40. defer cancel()
  41. r, err := s.basicClient.SoftwareInfo(ctx, &emptypb.Empty{})
  42. if err != nil {
  43. return &pb.SoftwareInfoReply{
  44. Module: "",
  45. Desc: "",
  46. Build: "",
  47. Version: "",
  48. }
  49. }
  50. return r
  51. }
  52. func (s *Study) CreateStudy(ctx context.Context, in *pb.StudyRequest) (*pb.StudyReply, error) {
  53. r, err := s.studyClient.CreateStudy(ctx, in)
  54. return r, err
  55. }
  56. func (s *Study) GetStudy(ctx context.Context, id *string) (*pb.StudyReply, error) {
  57. r, err := s.studyClient.GetStudy(ctx, &pb.IDRequest{
  58. InstanceId: id,
  59. })
  60. return r, err
  61. }
  62. func (s *Study) CreateImage(ctx context.Context, in *pb.CreateImageRequest) (*pb.CreateImageReply, error) {
  63. r, err := s.studyClient.CreateImage(ctx, in)
  64. return r, err
  65. }
  66. func (s *Study) CopyImage(ctx context.Context, in *pb.IDRequest) (*pb.CreateImageReply, error) {
  67. r, err := s.studyClient.CopyImage(ctx, in)
  68. return r, err
  69. }
  70. func (s *Study) SortImage(ctx context.Context, in *pb.SortRequest) (*emptypb.Empty, error) {
  71. r, err := s.studyClient.SortImage(ctx, in)
  72. return r, err
  73. }
  74. func (s *Study) DeleteImage(ctx context.Context, id *string) (*emptypb.Empty, error) {
  75. r, err := s.studyClient.DeleteImage(ctx, &pb.IDRequest{
  76. InstanceId: id,
  77. })
  78. return r, err
  79. }