notifier/internal/clients/quiz_rpc.go
2024-04-04 10:10:09 +03:00

68 lines
2.0 KiB
Go

package clients
import (
"context"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
notifyer "mailnotifier/internal/proto"
)
type QuizClient struct {
address string
logger *zap.Logger
}
func NewQuizClient(address string, logger *zap.Logger) *QuizClient {
return &QuizClient{
address: address,
logger: logger,
}
}
func (q *QuizClient) GetQuizzes(ctx context.Context, req *notifyer.GetQuizzesRequest) (*notifyer.GetQuizzesResponse, error) {
connection, err := grpc.Dial(q.address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
q.logger.Error("failed to connect on GetQuizzes of core rpc", zap.Error(err), zap.String("core rpc host", q.address))
return nil, err
}
defer func() {
if closeErr := connection.Close(); closeErr != nil {
q.logger.Error("failed to close connection on GetQuizzes of core rpc", zap.Error(closeErr))
}
}()
client := notifyer.NewQuizServiceClient(connection)
response, err := client.GetQuizzes(ctx, req)
if err != nil {
q.logger.Error("failed to GetQuizzes core rpc", zap.Error(err), zap.Any("request", req))
return nil, err
}
return response, nil
}
func (q *QuizClient) GetStartedQuizzes(ctx context.Context, req *notifyer.GetStartedQuizzesRequest) (*notifyer.GetStartedQuizzesResponse, error) {
connection, err := grpc.Dial(q.address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
q.logger.Error("failed to connect on GetStartedQuizzes of core rpc", zap.Error(err), zap.String("core rpc host", q.address))
return nil, err
}
defer func() {
if closeErr := connection.Close(); closeErr != nil {
q.logger.Error("failed to close connection on GetStartedQuizzes of core rpc", zap.Error(closeErr))
}
}()
client := notifyer.NewQuizServiceClient(connection)
response, err := client.GetStartedQuizzes(ctx, req)
if err != nil {
q.logger.Error("failed to GetStartedQuizzes core rpc", zap.Error(err), zap.Any("request", req))
return nil, err
}
return response, nil
}