3474967700
- Support for api's that use keys - Removed idle instances. --------- Co-authored-by: Viktor Kalyniuk <@> Co-authored-by: Simon Rieger <simono41@brothertec.eu>
56 lines
1.9 KiB
Swift
56 lines
1.9 KiB
Swift
//
|
|
// TranslationParsing.swift
|
|
// LiTranslate
|
|
//
|
|
// Created by Viktor Kalyniuk on 15.08.2022.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
struct TranslationParsing {
|
|
static func parse(url: String, text: String, inputLanguage: Languages, outputLanguage: Languages, key: String, handler: @escaping(TranslationDecode) -> Void) {
|
|
if let url = URL(string: url) {
|
|
let session = URLSession.shared
|
|
let parameters: [String: String] = [
|
|
"q": "\(text)",
|
|
"source": "\(inputLanguage.rawValue)",
|
|
"target": "\(outputLanguage.rawValue)",
|
|
"format": "text",
|
|
"api_key": "\(key)"
|
|
]
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "POST"
|
|
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
|
|
do {
|
|
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
|
|
} catch let error {
|
|
print(error.localizedDescription)
|
|
}
|
|
|
|
let task = session.dataTask(with: request) { data, response, error in
|
|
if let error = error {
|
|
print("POST request error: \(error.localizedDescription)")
|
|
return
|
|
}
|
|
|
|
guard let responseData = data else {
|
|
print("nil Data received from the server")
|
|
return
|
|
}
|
|
|
|
do {
|
|
let translationDecode: TranslationDecode = try JSONDecoder().decode(TranslationDecode.self, from: responseData)
|
|
handler(translationDecode)
|
|
} catch let error {
|
|
print(error.localizedDescription)
|
|
}
|
|
}
|
|
if !text.isEmpty {
|
|
task.resume()
|
|
}
|
|
}
|
|
}
|
|
}
|