Skip to content

Commit

Permalink
Refactor & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
vmilovanovicc committed Sep 30, 2023
1 parent d174290 commit 15f83b4
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 65 deletions.
24 changes: 13 additions & 11 deletions backend/mock_client_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package backend

import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/polly"
typesPolly "github.com/aws/aws-sdk-go-v2/service/polly/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
typesS3 "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/aws-sdk-go-v2/service/translate"
"strings"
)
Expand All @@ -24,9 +25,9 @@ type TranslateTranslateTextAPI interface {
func TranslateTextFromTranslate(ctx context.Context, api TranslateTranslateTextAPI, text, language string) (string, error) {
sourceLanguageCode := "auto"
response, err := api.TranslateText(ctx, &translate.TranslateTextInput{
Text: &text,
TargetLanguageCode: &language,
SourceLanguageCode: &sourceLanguageCode,
Text: aws.String(text),
TargetLanguageCode: aws.String(language),
SourceLanguageCode: aws.String(sourceLanguageCode),
})
if err != nil {
return "", err
Expand All @@ -44,9 +45,9 @@ type S3CreateBucketAPI interface {

func CreateBucketFromS3(ctx context.Context, api S3CreateBucketAPI, bucketName string) (string, error) {
response, err := api.CreateBucket(ctx, &s3.CreateBucketInput{
Bucket: &bucketName,
CreateBucketConfiguration: &types.CreateBucketConfiguration{
LocationConstraint: types.BucketLocationConstraint(region),
Bucket: aws.String(bucketName),
CreateBucketConfiguration: &typesS3.CreateBucketConfiguration{
LocationConstraint: typesS3.BucketLocationConstraint(region),
},
})
if err != nil {
Expand All @@ -65,16 +66,17 @@ type PollyStartSpeechSynthesisTaskAPI interface {

func StartSpeechSynthesisTaskFromPolly(ctx context.Context, api PollyStartSpeechSynthesisTaskAPI, text, bucketName, languageCode, targetVoice string) (string, string, error) {
response, err := api.StartSpeechSynthesisTask(ctx, &polly.StartSpeechSynthesisTaskInput{
Text: &text,
OutputS3BucketName: &bucketName,
Text: aws.String(text),
OutputS3BucketName: aws.String(bucketName),
VoiceId: typesPolly.VoiceId(targetVoice),
LanguageCode: typesPolly.LanguageCode(languageCode),
OutputFormat: typesPolly.OutputFormatMp3,
Engine: typesPolly.EngineStandard,
})
if err != nil {
return "", "", nil
return "", "", err
}
outputURI := strings.Split(*response.SynthesisTask.OutputUri, "/")
return *response.SynthesisTask.TaskId, outputURI[len(outputURI)-1], nil
objectName := outputURI[len(outputURI)-1]
return *response.SynthesisTask.TaskId, objectName, nil
}
20 changes: 10 additions & 10 deletions backend/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ package backend
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
typesS3 "github.com/aws/aws-sdk-go-v2/service/s3/types"
"log"
"net/url"
"strings"
"time"
)

// CreateBucket creates an S3 bucket to store the output of Amazon Polly Synthesis Tasks i.e. audio streams.
func CreateBucket(bucketName string) (location string, err error) {
func CreateBucket(bucketName string) (string, error) {
cfg := LoadAWSConfig()
client := s3.NewFromConfig(cfg)
bucketName = fmt.Sprintf("%s-%s", "amazon-polly-audio", time.Now().Format("01022006150405"))

params := &s3.CreateBucketInput{
Bucket: &bucketName,
CreateBucketConfiguration: &types.CreateBucketConfiguration{
LocationConstraint: types.BucketLocationConstraint(region),
Bucket: aws.String(bucketName),
CreateBucketConfiguration: &typesS3.CreateBucketConfiguration{
LocationConstraint: typesS3.BucketLocationConstraint(region),
}, // basic configuration
}
ctx := context.TODO()
Expand All @@ -29,13 +30,12 @@ func CreateBucket(bucketName string) (location string, err error) {
log.Fatalf("failed to create s3 bucket, error: %v\n", err)
return "", err
}
location = *resp.Location
fmt.Println(location)
return location, nil
fmt.Println(*resp.Location)
return *resp.Location, nil
}

// GetBucketName retrieves the S3 bucket name.
func GetBucketName(locationURL string) (outputS3BucketName string, err error) {
func GetBucketName(locationURL string) (string, error) {
parsedURL, err := url.Parse(locationURL)
if err != nil {
log.Fatalf("failed to parse S3 bucket locationURL, error: %v\n", err)
Expand All @@ -45,7 +45,7 @@ func GetBucketName(locationURL string) (outputS3BucketName string, err error) {
if len(hostParts) < 3 {
log.Fatalf("invalid s3 location URL: %s", locationURL)
}
outputS3BucketName = hostParts[0]
outputS3BucketName := hostParts[0]
fmt.Println(outputS3BucketName)
return outputS3BucketName, nil
}
28 changes: 13 additions & 15 deletions backend/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"regexp"
"testing"
Expand All @@ -24,11 +25,10 @@ func isValidBucketName(bucketName string) bool {
}

func TestCreateBucketFromS3(t *testing.T) {
location := "http://amazon-polly-09232023111111.s3.amazonaws.com/"
cases := []struct {
client func(t *testing.T) S3CreateBucketAPI
bucketName string
expect string
client func(t *testing.T) S3CreateBucketAPI
bucketName string
expectLocation string
}{
{
client: func(t *testing.T) S3CreateBucketAPI {
Expand All @@ -43,29 +43,27 @@ func TestCreateBucketFromS3(t *testing.T) {
if !isValidBucketName(*params.Bucket) {
t.Fatalf("expect bucket to meet S3 bucket naming rules")
}
if e, a := "http://amazon-polly-09232023111111.s3.amazonaws.com/", location; e != a {
t.Errorf("expect %v, got %v", e, a)
}

return &s3.CreateBucketOutput{
Location: &location,
Location: aws.String("http://amazon-polly-09232023111111.s3.amazonaws.com/"),
}, nil
})
},
bucketName: "amazon-polly-09232023111111",
expect: "http://amazon-polly-09232023111111.s3.amazonaws.com/",
bucketName: "amazon-polly-09232023111111",
expectLocation: "http://amazon-polly-09232023111111.s3.amazonaws.com/",
},
}

for _, tt := range cases {
t.Run("Test bucket creation", func(t *testing.T) {
ctx := context.TODO()
content, err := CreateBucketFromS3(ctx, tt.client(t), tt.bucketName)
gotLocation, err := CreateBucketFromS3(ctx, tt.client(t), tt.bucketName)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := tt.expect, content; e != a {
t.Errorf("expect %v, got %v", e, a)
if gotLocation != tt.expectLocation {
t.Errorf("expect %v, got %v", tt.expectLocation, gotLocation)

}
})
}
Expand Down Expand Up @@ -94,11 +92,11 @@ func TestGetBucketName(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
gotOutputS3BucketName, err := GetBucketName(tt.args.locationURL)
if (err != nil) != tt.wantErr {
t.Errorf("GetBucketName() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("got error %v, wantErr %v", err, tt.wantErr)
return
}
if gotOutputS3BucketName != tt.wantOutputS3BucketName {
t.Errorf("GetBucketName() gotOutputS3BucketName = %v, want %v", gotOutputS3BucketName, tt.wantOutputS3BucketName)
t.Errorf("got OutputS3BucketName %v, want %v", gotOutputS3BucketName, tt.wantOutputS3BucketName)
}
})
}
Expand Down
20 changes: 9 additions & 11 deletions backend/text_to_speech.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backend
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/polly"
"github.com/aws/aws-sdk-go-v2/service/polly/types"
"log"
Expand All @@ -27,22 +28,20 @@ func GetTargetVoice(language string) (string, error) {
}

// GetSpeechSynthesisTaskId function creates a synthesis task which converts provided text into an audio stream.
func GetSpeechSynthesisTaskId(text, bucketName, languageCode, targetVoice string) (taskId, objectName string, err error) {
func GetSpeechSynthesisTaskId(text, bucketName, languageCode, targetVoice string) (string, string, error) {
cfg := LoadAWSConfig()
client := polly.NewFromConfig(cfg)

outputFormat := types.OutputFormatMp3
engine := types.EngineStandard
targetVoice, _ = GetTargetVoice(languageCode)
targetVoiceId := types.VoiceId(targetVoice)
bucketLocation, _ := CreateBucket("")
bucketName, _ = GetBucketName(bucketLocation)

params := &polly.StartSpeechSynthesisTaskInput{
Text: &text,
OutputS3BucketName: &bucketName,
OutputFormat: outputFormat,
Engine: engine,
Text: aws.String(text),
OutputS3BucketName: aws.String(bucketName),
OutputFormat: types.OutputFormatMp3,
Engine: types.EngineStandard,
VoiceId: targetVoiceId,
}

Expand All @@ -52,10 +51,9 @@ func GetSpeechSynthesisTaskId(text, bucketName, languageCode, targetVoice string
log.Fatalf("failed to retrieve task id and object name, error: %v\n", err)
return "", "", err
}
taskId = *resp.SynthesisTask.TaskId
uriParts := strings.Split(*resp.SynthesisTask.OutputUri, "/")
objectName = uriParts[len(uriParts)-1]
fmt.Println(taskId)
objectName := uriParts[len(uriParts)-1]
fmt.Println(*resp.SynthesisTask.TaskId)
fmt.Println(objectName)
return taskId, objectName, nil
return *resp.SynthesisTask.TaskId, objectName, nil
}
4 changes: 2 additions & 2 deletions backend/text_to_speech_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func TestGetTargetVoice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := GetTargetVoice(tt.args.language)
if (err != nil) != tt.wantErr {
t.Errorf("GetTargetVoice() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("got error %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetTargetVoice() got = %v, want %v", got, tt.want)
t.Errorf(" got error %v, want %v", got, tt.want)
}
})
}
Expand Down
7 changes: 4 additions & 3 deletions backend/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backend
import (
context "context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/translate"
"log"
)
Expand All @@ -16,9 +17,9 @@ func TranslateText(text, language string) (string, error) {
sourceLanguageCode := "auto"

params := &translate.TranslateTextInput{
Text: &text,
SourceLanguageCode: &sourceLanguageCode,
TargetLanguageCode: &language,
Text: aws.String(text),
SourceLanguageCode: aws.String(sourceLanguageCode),
TargetLanguageCode: aws.String(language),
}

ctx := context.TODO()
Expand Down
22 changes: 11 additions & 11 deletions backend/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/translate"
"testing"
)
Expand All @@ -13,12 +14,11 @@ func (m mockTranslateTextAPI) TranslateText(ctx context.Context, params *transla
}

func TestTranslateTextFromTranslate(t *testing.T) {
translatedText := "Buenas noches"
cases := []struct {
client func(t *testing.T) TranslateTranslateTextAPI
language string
text string
expect string
client func(t *testing.T) TranslateTranslateTextAPI
language string
text string
expectTranslatedText string
}{
{
client: func(t *testing.T) TranslateTranslateTextAPI {
Expand All @@ -38,24 +38,24 @@ func TestTranslateTextFromTranslate(t *testing.T) {
}

return &translate.TranslateTextOutput{
TranslatedText: &translatedText,
TranslatedText: aws.String("Buenas noches"),
}, nil
})
},
language: "es",
text: "Good evening",
expect: "Buenas noches",
language: "es",
text: "Good evening",
expectTranslatedText: "Buenas noches",
},
}

for _, tt := range cases {
t.Run("Test translation", func(t *testing.T) {
ctx := context.TODO()
content, err := TranslateTextFromTranslate(ctx, tt.client(t), tt.text, tt.language)
gotTranslatedText, err := TranslateTextFromTranslate(ctx, tt.client(t), tt.text, tt.language)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := tt.expect, content; e != a {
if e, a := tt.expectTranslatedText, gotTranslatedText; e != a {
t.Errorf("expect %v, got %v", e, a)
}
})
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import "zobot/backend"

func main() {
// placeholder
backend.TranslateText("hello world", "en")
backend.TranslateText("hello world", "es")
locationURL, _ := backend.CreateBucket("")
backend.GetBucketName(locationURL)
backend.GetTargetVoice("es")
backend.GetTargetVoice("fr")
backend.GetSpeechSynthesisTaskId("Testing speech to voice functionality", "", "es", "")
}

0 comments on commit 15f83b4

Please sign in to comment.