Fix operation check

This commit is contained in:
2025-08-11 17:30:23 +03:00
parent bb93f99911
commit f625e21418
3 changed files with 61 additions and 15 deletions

View File

@@ -3,6 +3,7 @@ package speechkit
import (
"context"
"fmt"
"log"
"os"
"google.golang.org/grpc"
@@ -15,10 +16,12 @@ import (
const (
SpeechKitEndpoint = "stt.api.cloud.yandex.net:443"
OperationEndpoint = "operation.api.cloud.yandex.net:443"
)
type SpeechKitService struct {
conn *grpc.ClientConn
sttConn *grpc.ClientConn
opConn *grpc.ClientConn
sttClient stt.AsyncRecognizerClient
opClient operation.OperationServiceClient
apiKey string
@@ -33,18 +36,26 @@ func NewSpeechKitService() (*SpeechKitService, error) {
return nil, fmt.Errorf("missing required Yandex Cloud environment variables")
}
// Создаем защищенное соединение
// Создаем защищенное соединение для SpeechKit
creds := credentials.NewTLS(nil)
conn, err := grpc.NewClient(SpeechKitEndpoint, grpc.WithTransportCredentials(creds))
sttConn, err := grpc.NewClient(SpeechKitEndpoint, grpc.WithTransportCredentials(creds))
if err != nil {
return nil, fmt.Errorf("failed to connect to SpeechKit: %w", err)
}
sttClient := stt.NewAsyncRecognizerClient(conn)
opClient := operation.NewOperationServiceClient(conn)
// Создаем защищенное соединение для Operations API
opConn, err := grpc.NewClient(OperationEndpoint, grpc.WithTransportCredentials(creds))
if err != nil {
sttConn.Close()
return nil, fmt.Errorf("failed to connect to Operations API: %w", err)
}
sttClient := stt.NewAsyncRecognizerClient(sttConn)
opClient := operation.NewOperationServiceClient(opConn)
return &SpeechKitService{
conn: conn,
sttConn: sttConn,
opConn: opConn,
sttClient: sttClient,
opClient: opClient,
apiKey: apiKey,
@@ -53,15 +64,26 @@ func NewSpeechKitService() (*SpeechKitService, error) {
}
func (s *SpeechKitService) Close() error {
return s.conn.Close()
var err1, err2 error
if s.sttConn != nil {
err1 = s.sttConn.Close()
}
if s.opConn != nil {
err2 = s.opConn.Close()
}
if err1 != nil {
return err1
}
return err2
}
// RecognizeFileFromS3 запускает асинхронное распознавание файла из S3
func (s *SpeechKitService) RecognizeFileFromS3(s3URI string) (string, error) {
ctx := context.Background()
// Добавляем авторизацию в контекст
// Добавляем авторизацию и folder_id в контекст
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Api-Key "+s.apiKey)
ctx = metadata.AppendToOutgoingContext(ctx, "x-folder-id", s.folderID)
// Создаем запрос на распознавание
req := &stt.RecognizeFileRequest{
@@ -102,8 +124,9 @@ func (s *SpeechKitService) RecognizeFileFromS3(s3URI string) (string, error) {
func (s *SpeechKitService) GetRecognitionResult(operationID string) ([]*stt.StreamingResponse, error) {
ctx := context.Background()
// Добавляем авторизацию в контекст
// Добавляем авторизацию и folder_id в контекст
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Api-Key "+s.apiKey)
ctx = metadata.AppendToOutgoingContext(ctx, "x-folder-id", s.folderID)
req := &stt.GetRecognitionRequest{
OperationId: operationID,
@@ -133,6 +156,11 @@ func (s *SpeechKitService) GetRecognitionResult(operationID string) ([]*stt.Stre
func (s *SpeechKitService) CheckOperationStatus(operationID string) (*operation.Operation, error) {
ctx := context.Background()
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Api-Key "+s.apiKey)
ctx = metadata.AppendToOutgoingContext(ctx, "x-folder-id", s.folderID)
log.Printf("Check operation status: id %s\n", operationID)
op, err := s.opClient.Get(ctx, &operation.GetOperationRequest{
OperationId: operationID,
})