Skip to content

Commit

Permalink
[UPD] Update logging for reverso api
Browse files Browse the repository at this point in the history
  • Loading branch information
freQuensy23-coder committed Mar 29, 2023
1 parent 7804560 commit 8edcc55
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
1 change: 0 additions & 1 deletion lib/src/main/kotlin/reverso/LanguageCode.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package reverso

class LanguageCode(languageCodeString: String) {

companion object {
val AVAILABLE_CODES: List<String> get() = listOf("ru", "en", "de", "ar", "es", "fr", "he", "it", "ja", "ko", "nl", "pl", "pt", "ro", "sv", "tr", "zh", "uk")
}
Expand Down
16 changes: 10 additions & 6 deletions lib/src/main/kotlin/reverso/Translator.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package reverso

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
Expand All @@ -8,23 +10,22 @@ import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import reverso.LanguageCode


class ReversoTranslatorAPI {
private val jsonUnsafe = Json { ignoreUnknownKeys = true }
private val client = okhttp3.OkHttpClient()
private val logger = KotlinLogging.logger {}

fun translate(text: String, fromLang: LanguageCode, toLang: LanguageCode): TranslationResponse {
logger.debug { "Translating text $text from $fromLang to $toLang" }
val request = createRequest(createRequestBody(text, fromLang.code, toLang.code))
val response = client.newCall(request).execute()
if (!response.isSuccessful) {
logger.error { "Request failed with code ${response.code}" }
throw Exception("Request failed with code ${response.code}")
}
val responseBody = response.body!!.string()
logger.info { "Request successful with code ${response.code}.Response body $responseBody" }
logger.debug { "Request successful with code ${response.code}.Response body $responseBody" }
println("Request successful with code ${response.code}.Response body $responseBody")
// Convert response.body to TranslationResponse using Json and skip any additional fields
return jsonUnsafe.decodeFromString(responseBody)
Expand All @@ -33,18 +34,21 @@ class ReversoTranslatorAPI {
private fun createRequestBody(text: String, fromLang: String, toLang: String): RequestBody{
val serializeFormat = Json { encodeDefaults = true }
val JSON = "application/json; charset=utf-8".toMediaType()
val translationRequest = TranslationRequest(fromLang, toLang, sourceText=text)
logger.debug { "Translation request: $translationRequest" }
return serializeFormat
.encodeToString<TranslationRequest>(TranslationRequest(fromLang, toLang, sourceText=text)).toRequestBody(JSON)
.encodeToString<TranslationRequest>(translationRequest).toRequestBody(JSON)
}

private fun createRequest(requestBody: RequestBody): Request{
return Request.Builder()
val request = Request.Builder()
.url("https://context.reverso.net/bst-query-service")
.addHeader("Origin", "https://context.reverso.net")
.addHeader("Accept-Language", "en-US,en;q=0.5")
.addHeader("X-Requested-With", "XMLHttpRequest")
.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:77.0) Gecko/20100101 Firefox/77.0")
.post(requestBody)
.build()
logger.debug { "Created request: $request" }
return request.build()
}
}
1 change: 0 additions & 1 deletion lib/src/test/kotlin/reverso/ReversoTranslatorAPITest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package reverso

import ReversoTranslatorAPI
import okhttp3.RequestBody
import okio.Buffer
import org.junit.jupiter.api.Assertions.*
Expand Down

0 comments on commit 8edcc55

Please sign in to comment.