Skip to content

Commit

Permalink
chore: improve error handling for audio and translation services (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwittstruck authored Jan 23, 2024
1 parent 211a909 commit c259f9b
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions lib/qrstorage/services/gcp/google_api_service_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,49 @@ defmodule Qrstorage.Services.Gcp.GoogleApiServiceImpl do

@behaviour GoogleApiService

require Logger

@impl GoogleApiService
def text_to_audio(text, language, voice) do
request = build_synthesize_speech_request(text, language, voice)

{:ok, response} =
GoogleApi.TextToSpeech.V1.Api.Text.texttospeech_text_synthesize(authenticated_connection(),
body: request
)
response_body =
case GoogleApi.TextToSpeech.V1.Api.Text.texttospeech_text_synthesize(authenticated_connection(),
body: request
) do
{:ok, response} ->
response.audioContent

_ ->
Logger.warning("Exception caught while transforming text to audio.")
# Return empty string:
""
end

decoded_file = Base.decode64!(response.audioContent)
decoded_file = Base.decode64!(response_body)
{:ok, decoded_file}
end

@impl GoogleApiService
def translate(text, target_language) do
# https://hexdocs.pm/google_api_translate/GoogleApi.Translate.V2.Api.Translations.html#language_translations_list/5

{:ok, response} =
GoogleApi.Translate.V2.Api.Translations.language_translations_list(
authenticated_connection(),
[text],
target_language,
format: "text"
)
translated_text =
case GoogleApi.Translate.V2.Api.Translations.language_translations_list(
authenticated_connection(),
[text],
target_language,
format: "text"
) do
{:ok, response} ->
List.first(response.translations).translatedText

_ ->
Logger.info("Exception caught while translating text")
# return an empty string:
""
end

translated_text = List.first(response.translations).translatedText
{:ok, translated_text}
end

Expand Down

0 comments on commit c259f9b

Please sign in to comment.