59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package grpcClient
|
||
|
||
import (
|
||
"context"
|
||
|
||
pbVC "gitea.cybertalant.ru/VisionCareerMiniapp/DataManagement/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
|
||
}
|