-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_Translator.cpp
64 lines (56 loc) · 1.97 KB
/
http_Translator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "http_Translator.h"
http_Translator::http_Translator(QObject *parent) : QObject(parent)
{
networkManager = new QNetworkAccessManager(this);
connect(networkManager,&QNetworkAccessManager::finished,this,&http_Translator::replyFromServer);
}
QString http_Translator::getJSONResult()
{
return p_JSONResult;
}
void http_Translator::setJSONResult(const QString &json)
{
if (json == p_JSONResult)
return;
p_JSONResult = json;
emit changedJSONResult(p_JSONResult);
}
void http_Translator::getRequest(QString languageFrom,QString languageTo, QString word)
{
QString url;
url = "https://systran-systran-platform-for-language-processing-v1.p.rapidapi.com/translation/text/translate?source=" +languageFrom +"&target="+ languageTo +"&input=" + word;
QUrl p_url = QUrl(url);
QNetworkRequest request(p_url);
request.setRawHeader(QByteArray("x-rapidapi-key"),QByteArray("f797859160mshf4a2683496b53fap10090bjsn90bb50a74921"));
networkManager->get(request);
}
http_Translator::~http_Translator()
{
//networkManager->destroyed(networkManager);
}
void http_Translator::replyFromServer(QNetworkReply* reply)
{
if (reply->isReadable())
{
QByteArray arrayOfRep = reply->readAll();
// get the root object
QJsonDocument itemDoc = QJsonDocument::fromJson(arrayOfRep);
QJsonObject rootObject = itemDoc.object();
// get the outputs object
QJsonValue response = rootObject.value("outputs");
QJsonValue outputValue;
if(response.isArray())
{
QJsonArray array = response.toArray();
QJsonValue jsonValueFromArray = array.at(0);
QJsonObject objectOutput = jsonValueFromArray.toObject();
outputValue = objectOutput.value("output");
setJSONResult(outputValue.toString());
}
else
setJSONResult("Cannot set data");
qDebug() << getJSONResult();
}
else
qDebug() << "Error cannot read" ;
}