Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate API: Works in wget but not here #137

Open
danwdart opened this issue Jul 28, 2019 · 2 comments
Open

Translate API: Works in wget but not here #137

danwdart opened this issue Jul 28, 2019 · 2 comments

Comments

@danwdart
Copy link

I've got a translate request with trace:

[Client Request] {
  host      = translation.googleapis.com:443
  secure    = True
  method    = POST
  timeout   = ResponseTimeoutMicro 70000000
  redirects = 10
  path      = /language/translate/v2
  query     = ?pp=true&alt=json
  headers   = authorization: Bearer BLAH BLAH BLAH; accept: application/json;charset=utf-8; content-type: application/json;charset=utf-8
  body      = {"format":"text","q":["hallo"],"model":"nmt","source":"de","target":"en"}
}
[Client Response] {
  status  = 200 OK
  headers = content-type: application/json; charset=UTF-8; vary: Origin; vary: X-Origin; vary: Referer; content-encoding: gzip; date: Sun, 28 Jul 2019 11:37:19 GMT; server: ESF; cache-control: private; x-xss-protection: 0; x-frame-options: SAMEORIGIN; x-content-type-options: nosniff; alt-svc: quic=":443"; ma=2592000; v="46,43,39"; transfer-encoding: chunked
}

however it doesn't actually save anything into the variable:
TranslationsListResponse' {_tlrTranslations = Nothing}

Credentials are all good, I tried in wget and browser console and it was fine and returned proper response, but using this is just a response of Nothing...

Code:

{-# LANGUAGE OverloadedStrings, TemplateHaskell #-}

import Control.Monad
import Control.Lens
import Data.Function
import Data.Text
import Network.Google
import Network.Google.Translate
import System.Environment
import System.IO

main :: IO ()
main = do
    lgr <- newLogger Trace stdout
    setEnv "GOOGLE_APPLICATION_CREDENTIALS" "./google.json"
    env <- newEnv <&> (envLogger .~ lgr) . (envScopes .~ cloudTranslationScope)
    a <- runResourceT . runGoogle env $ send myR
    print $ a^.tlrTranslations

myT :: TranslateTextRequest
myT = translateTextRequest & ttrFormat .~ Just "text" & ttrQ .~ ["hallo"] & ttrSource .~ (Just "de") & ttrTarget .~ (Just "en") & ttrModel .~ (Just "nmt")

myR :: TranslationsTranslate
myR = myT & translationsTranslate & ttPp .~ True

Thanks

@andorp
Copy link

andorp commented Aug 27, 2019

Same for me.

@andorp
Copy link

andorp commented Aug 27, 2019

I found the root cause:

The response JSON seems to have an extra "data" tag:

body      = {"q":["Dursleys were proud to say that they were perfectly normal."],"source":"en","target":"es"}
}
[Client Response] {
  status  = 200 OK
  headers = content-type: application/json; charset=UTF-8; vary: Origin; vary: X-Origin; vary: Referer; content-encoding: gzip; date: Tue, 27 Aug 2019 22:38:59 GMT; server: ESF; cache-control: private; x-xss-protection: 0; x-frame-options: SAMEORIGIN; x-content-type-options: nosniff; alt-svc: quic=":443"; ma=2592000; v="46,43,39"; transfer-encoding: chunked
}
Object (fromList [("data",Object (fromList [("translations",Array [Object (fromList [("translatedText",String "Los Dursley estaban orgullosos de decir que eran perfectamente normales.")])])]))])

I made a monkey patch in the gogol-translate:

instance FromJSON TranslationsListResponse where
  parseJSON =
    withObject "" (\o1 -> do
      x <- o1 .: "data"
      withObject "" (\o ->
         TranslationsListResponse' <$>
           (o .:? "translations" .!= mempty)
        )
        x)

@brendanhay What is the best way to actually implement this fix in the gogol framework?
I am happy to do the legwork, but I need some guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants