therealbush / translator

A simple and free Google Translate library for Kotlin/JVM and Java.
MIT License
61 stars 5 forks source link

KTOR 3.0.0 breaks it #4

Open gigitalz opened 1 month ago

gigitalz commented 1 month ago

It looks like this breaks with the new KTOR:


Caused by: java.lang.ClassNotFoundException: io.ktor.client.plugins.UserAgent
        at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)

Any quick fix other than rolling back KTOR version before 3.0.0?

Code:
`                val translator by lazy { Translator() }
                val ret = translator.translate("שר החוץ האמריקני טוני בלינקן ושר ההגנה האמריקני לוייד אוסטין שיגרו אתמול (שני) מכתב לשרים דרמר וגלנט ובו הם דורשים כי ישראל תבצע תוך 30 יום צעדים לשיפור המצב ההומניטארי בעזה כדי להימנע מהשלכות על אספקת הנשק האמריקני לישראל. זה המכתב המלא (עליו דיווחה לראשונה)", Language.ENGLISH)`
therealbush commented 1 month ago

I believe if you pass your own HttpClient to the Translator constructor it should fix the issue. I set a user agent in the default HttpClient in the constructor.

I might not fix this in a timely manner however you are welcome to submit a pull request fixing the issue.

therealbush commented 1 month ago
Translator(HttpClient(CIO) {
        HttpResponseValidator {
            // This is run for all responses
            validateResponse { response ->
                if (!response.status.isSuccess()) {
                    throw TranslationException("Error caught from HTTP request: ${response.status}")
                }
            }
            // This is run only when an exception is thrown, including our custom ones
            handleResponseExceptionWithRequest { cause, _ ->
                if (cause !is TranslationException) {
                    throw TranslationException("Exception caught from HTTP request", cause)
                }
            }
        })
therealbush commented 1 month ago

the above just removes the user agent setup. I don't remember if the user agent was necessary, if it is you would have to figure out how to set the user agent on this new ktor version.

therealbush commented 1 week ago

I can't reproduce this issue. Are you still having troubles?