MiniappGoService/internal/infrastructure/grpcClient/client.go

58 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package grpcClient
import (
"context"
pbVC "gitea.cybertalant.ru/VisionCareerMiniapp/DataManagemet/pb/golang"
"gitea.cybertalant.ru/VisionCareerMiniapp/MiniappGoService/internal/application/constants"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)
type grpcClient struct {
apiKey string
conn *grpc.ClientConn
vсClient pbVC.VisionCareerApiServiceV1Client
}
func (t *grpcClient) Close() error {
return t.conn.Close()
}
func (t *grpcClient) GetUserVacancies(ctx context.Context, in *pbVC.GetUserVacanciesRequest, opts ...grpc.CallOption) (*pbVC.GetUserVacanciesResponse, error) {
// Append api key to Meta-data
md := metadata.Pairs(constants.APIKey.String(), t.apiKey)
ctx = metadata.NewOutgoingContext(ctx, md)
return t.vсClient.GetUserVacancies(ctx, in, opts...)
}
func (t *grpcClient) UpsertUserData(ctx context.Context, in *pbVC.UpsertUserDataRequest, opts ...grpc.CallOption) (*pbVC.UpsertUserDataResponse, error) {
// Append api key to Meta-data
md := metadata.Pairs(constants.APIKey.String(), t.apiKey)
ctx = metadata.NewOutgoingContext(ctx, md)
return t.vсClient.UpsertUserData(ctx, in, opts...)
}
// Init : Инициализирует общий gRPC клиент
func Init(addr, apiKey string) (*grpcClient, error) {
// Открытие соединения
conn, err := grpc.Dial(
addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
)
if err != nil {
return nil, err
}
// Инициализация gRPC клиентов
return &grpcClient{
apiKey: apiKey,
conn: conn,
vсClient: pbVC.NewVisionCareerApiServiceV1Client(conn),
}, nil
}