Replace all infra services with interfaces
This commit is contained in:
95
internal/adapter/yandex/recognizer.go
Normal file
95
internal/adapter/yandex/recognizer.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package yandex
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"git.vakhrushev.me/av/transcriber/internal/entity"
|
||||
)
|
||||
|
||||
type YandexAudioRecognizerConfig struct {
|
||||
// s3
|
||||
Region string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
BucketName string
|
||||
Endpoint string
|
||||
// speech kit
|
||||
ApiKey string
|
||||
FolderID string
|
||||
}
|
||||
|
||||
type YandexAudioRecognizerService struct {
|
||||
s3Sevice *yandexS3Service
|
||||
sttService *speechKitService
|
||||
}
|
||||
|
||||
func NewYandexAudioRecognizerService(cfg YandexAudioRecognizerConfig) (*YandexAudioRecognizerService, error) {
|
||||
s3, err := newYandexS3Service(s3Config{
|
||||
Region: cfg.Region,
|
||||
AccessKey: cfg.AccessKey,
|
||||
SecretKey: cfg.SecretKey,
|
||||
BucketName: cfg.BucketName,
|
||||
Endpoint: cfg.Endpoint,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stt, err := newSpeechKitService(speechKitConfig{
|
||||
ApiKey: cfg.ApiKey,
|
||||
FolderID: cfg.FolderID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &YandexAudioRecognizerService{
|
||||
s3Sevice: s3,
|
||||
sttService: stt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *YandexAudioRecognizerService) Close() error {
|
||||
return s.sttService.Close()
|
||||
}
|
||||
|
||||
func (s *YandexAudioRecognizerService) RecognizeFile(filePath string) (string, error) {
|
||||
fileName := filepath.Base(filePath)
|
||||
|
||||
err := s.s3Sevice.uploadFile(filePath, fileName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
uri := s.s3Sevice.fileUrl(fileName)
|
||||
|
||||
opId, err := s.sttService.recognizeFileFromS3(uri)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return opId, nil
|
||||
}
|
||||
|
||||
func (s *YandexAudioRecognizerService) GetRecognitionText(operationID string) (string, error) {
|
||||
return s.sttService.getRecognitionText(operationID)
|
||||
}
|
||||
|
||||
func (s *YandexAudioRecognizerService) CheckRecognitionStatus(operationID string) (*entity.RecognitionResult, error) {
|
||||
operation, err := s.sttService.checkOperationStatus(operationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !operation.Done {
|
||||
return entity.NewInProgressResult(), nil
|
||||
}
|
||||
|
||||
if opErr := operation.GetError(); opErr != nil {
|
||||
errorText := fmt.Sprintf("operation failed: code %d, message: %s", opErr.Code, opErr.Message)
|
||||
return entity.NewFailedResult(errorText), nil
|
||||
}
|
||||
|
||||
return entity.NewCompletedResult(), nil
|
||||
}
|
Reference in New Issue
Block a user