add codeword client to api

This commit is contained in:
Pavel 2024-04-25 15:53:33 +03:00
parent 9ba775ee02
commit 8ba11c62b3
3 changed files with 70 additions and 3 deletions

@ -0,0 +1,61 @@
package client
import (
"context"
"fmt"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
codeword_rpc "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/proto/codeword"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate"
)
type CodewordClientDeps struct {
Logger *zap.Logger
CodewordServiceHost string
}
type CodewordClient struct {
logger *zap.Logger
codewordServiceHost string
}
func NewCodewordClient(deps CodewordClientDeps) *CodewordClient {
if deps.Logger == nil {
log.Panicln("logger is nil on <NewCodewordClient>")
}
if validate.IsStringEmpty(deps.CodewordServiceHost) {
log.Panicln("codeword host is empty on <NewCodewordClient>")
}
return &CodewordClient{
logger: deps.Logger,
codewordServiceHost: deps.CodewordServiceHost,
}
}
func (receiver *CodewordClient) GetAllPromoActivations(ctx context.Context, request *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, errors.Error) {
connection, err := grpc.Dial(receiver.codewordServiceHost, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
receiver.logger.Error("failed to connect on <GetAllPromoActivations> of <CodewordClient>", zap.Error(err), zap.String("codeword host", receiver.codewordServiceHost))
return nil, errors.New(fmt.Errorf("failed connect to codeword service: %w", err), errors.ErrInternalError)
}
defer func() {
if closeErr := connection.Close(); closeErr != nil {
receiver.logger.Error("failed to close connection on <GetAllPromoActivations> of <CodewordClient>", zap.Error(closeErr))
}
}()
client := codeword_rpc.NewPromoCodeServiceClient(connection)
response, err := client.GetAllPromoActivations(ctx, request)
if err != nil {
receiver.logger.Error("failed getting stats resp on <GetAllPromoActivations> of <DiscountClient>", zap.Error(err), zap.Any("request", request))
return nil, errors.New(fmt.Errorf("failed getting stats resp from codeword: %w", err), errors.ErrInternalError)
}
return response, nil
}

@ -51,6 +51,7 @@ type clients struct {
verify *client.VerificationClient
template *client.TemplateClient
mail *client.MailClient
codeword *client.CodewordClient
}
var _ ServerInterface = (*API2)(nil)
@ -85,6 +86,7 @@ func NewAPI2(logger *zap.Logger, db *mongo.Database, config *models.Config, cons
FiberClient: fiber.AcquireClient(),
MailAddress: config.Service.Mail.MailAddress,
}),
codeword: client.NewCodewordClient(client.CodewordClientDeps{Logger: logger, CodewordServiceHost: config.Service.CodewordMicroservice.HostGRPC}),
},
}
}

@ -30,15 +30,15 @@ type ServiceConfiguration struct {
HubadminMicroservice HubadminMicroserviceConfiguration
CurrencyMicroservice CurrencyMicroserviceConfiguration
DiscountMicroservice DiscountMicroserviceConfiguration
CodewordMicroservice CodewordMicroserviceConfiguration
PaymentMicroservice PaymentMicroserviceConfiguration
VerificationMicroservice VerificationMicroserviceConfiguration
TemplategenMicroserviceURL TemplategenMicroserviceConfiguration
JWT JWTConfiguration
Kafka KafkaConfiguration
Mail MailConfiguration
PubKey string `env:"PUBLIC_KEY"`
PrivKey string `env:"PRIVATE_KEY"`
PubKey string `env:"PUBLIC_KEY"`
PrivKey string `env:"PRIVATE_KEY"`
}
type KafkaConfiguration struct {
@ -79,6 +79,10 @@ type TemplategenMicroserviceConfiguration struct {
URL TemplategenMicroserviceURL
}
type CodewordMicroserviceConfiguration struct {
HostGRPC string `env:"CODEWORD_MICROSERVICE_GRPC_HOST,required"`
}
type PaymentMicroserviceConfiguration struct {
HostGRPC string `env:"PAYMENT_MICROSERVICE_GRPC_HOST,required"`
}