Functional batch HTTP mixed mode multipart requests.

This commit is contained in:
Martin Rotter 2017-10-19 14:05:02 +02:00
parent b25fd514d0
commit a735c9f1e9
3 changed files with 40 additions and 4 deletions

View File

@ -183,8 +183,8 @@ int main(int argc, char* argv[]) {
multi->setContentType(QHttpMultiPart::ContentType::MixedType);
p1.setRawHeader("Content-Type", "application/http");
p2.setRawHeader("Content-Type", "application/http");
p1.setBody("GET /gmail/v1/users/me/messages\r\n");
p2.setBody("GET /gmail/v1/users/me/labels\r\n");
p1.setBody("GET /gmail/v1/users/me/messages/15f3427a9f6d2728\r\n");
p2.setBody("GET /gmail/v1/users/me/messages/15f259a2c353589b\r\n");
multi->append(p1);
multi->append(p2);
QNetworkRequest req;
@ -210,7 +210,8 @@ int main(int argc, char* argv[]) {
QObject::connect(srv, &OAuth2Service::tokensRetrieveError, [](QString err, QString desc) {
int a = 5;
});
srv->login();*/
srv->login();
*/
// Enter global event loop.
return Application::exec();

View File

@ -10,8 +10,10 @@
#define GMAIL_API_LABELS_LIST "https://www.googleapis.com/gmail/v1/users/me/labels"
#define GMAIL_API_MSGS_LIST "https://www.googleapis.com/gmail/v1/users/me/messages"
#define GMAIL_DEFAULT_BATCH_SIZE 100
#define GMAIL_DEFAULT_BATCH_SIZE 50
#define GMAIL_MAX_BATCH_SIZE 999
#define GMAIL_MIN_BATCH_SIZE 20
#define GMAIL_CONTENT_TYPE_HTTP "application/http"
#endif // GMAIL_DEFINITIONS_H

View File

@ -16,6 +16,7 @@
#include "services/gmail/gmailfeed.h"
#include "services/gmail/gmailserviceroot.h"
#include <QHttpMultiPart>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
@ -329,6 +330,38 @@ void GmailNetworkFactory::onAuthFailed() {
}
bool GmailNetworkFactory::obtainAndDecodeFullMessages(const QList<Message>& lite_messages) {
QHttpMultiPart* multi = new QHttpMultiPart();
multi->setContentType(QHttpMultiPart::ContentType::MixedType);
foreach (const Message& msg, lite_messages) {
QHttpPart part;
part.setRawHeader(HTTP_HEADERS_CONTENT_TYPE, GMAIL_CONTENT_TYPE_HTTP);
QString full_msg_endpoint = QString("GET /gmail/v1/users/me/messages/%1\r\n").arg(msg.m_customId);
part.setBody(full_msg_endpoint.toUtf8());
multi->append(part);
}
QEventLoop loop;
QNetworkRequest req;
auto bearer = m_oauth2->bearer();
req.setRawHeader(QString("Authorization").toLocal8Bit(), bearer.toLocal8Bit());
req.setUrl(QUrl::fromUserInput("https://www.googleapis.com/batch"));
auto* repl = SilentNetworkAccessManager::instance()->post(req, multi);
connect(repl, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
auto resp = repl->readAll();
auto aa = repl->error();
multi->deleteLater();
repl->deleteLater();
IOFactory::writeTextFile("b.html", resp);
return false;
}