source: src/lib/siwebserviceclient.cpp@ d2faced

Last change on this file since d2faced was 17ef75d, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

command line client is now able to register stations. All entered data could be reviewed by the user and edited again, before sending it

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 MA 02110-1301 USA.
18*/
19
20#include "siwebserviceclient.h"
21#include "moc_siwebserviceclient.cpp"
22
23#include "silibconfig.h"
24#include "silibrary.h"
25#include "siweatherstation.h"
26
27#include <QNetworkAccessManager>
28#include <QNetworkRequest>
29#include <QNetworkReply>
30#include <QJsonObject>
31#include <QJsonDocument>
32
33using namespace SiWeather::Library;
34
35SiWebserviceClient::SiWebserviceClient(QUrl baseUrl, QObject* parent)
36 : QObject(parent)
37{
38 _baseUrl = baseUrl;
39 _networkManager = new QNetworkAccessManager(this);
40}
41
42QString SiWebserviceClient::apiVersion()
43{
44 return QString(WEBSERVICE_API_VERSION);
45}
46
47SiWebserviceClient::ReplyType SiWebserviceClient::stationsRegister(const SiWeatherStation &station, QString &error)
48{
49 if(!_networkManager)
50 return Error;
51
52 if(station.name().isEmpty() || station.city().isEmpty() || station.eMail().isEmpty() || station.type().isEmpty()){
53 error = tr("Station has empty name, city or eMail address. All of these are needed!");
54 return Error;
55 }
56
57 QJsonObject jsonStation;
58 jsonStation["name"] = station.name();
59 jsonStation["city"] = station.city();
60 jsonStation["longitude"] = Library::geoCoordinateToString(station.longitude());
61 jsonStation["latitude"] = Library::geoCoordinateToString(station.latitude());
62 jsonStation["email"] = station.eMail();
63 jsonStation["type"] = station.type();
64 jsonStation["typeUrl"] = station.typeUrl().toString();
65 QJsonDocument jsonDocument(jsonStation);
66
67 QNetworkRequest request;
68 request.setUrl(QString("%1%2").arg(appendSlashToUrl(_baseUrl.toString())).arg("index.php/stations/register"));
69 request.setRawHeader("User-Agent", QString("SiWeather Library %1").arg(Library::version()).toUtf8());
70 request.setHeader(QNetworkRequest::ContentTypeHeader,
71 "application/json");
72 QNetworkReply *reply = _networkManager->post(request, jsonDocument.toJson(QJsonDocument::Indented));
73 connect(reply, SIGNAL(finished()),
74 this, SLOT(replyStationsRegister()));
75 return StationsRegister;
76}
77
78void SiWebserviceClient::replyStationsRegister()
79{
80 QNetworkReply *reply = getNetworkReply(sender(), StationsRegister);
81 if(!reply)
82 return;
83
84 QByteArray data = reply->readAll();
85 reply->deleteLater();
86
87 QJsonDocument jsonDocument = QJsonDocument::fromJson(data);
88 QJsonObject json = jsonDocument.object();
89 if(!json.contains("result")){
90 emit finishedStationsRegister(false, "", tr("No result in response found"));
91 emit finished(StationsRegister, false, data);
92 return;
93 }
94 if(json["result"].toString() != "success"){
95 emit finishedStationsRegister(false, "", tr("Operation failed for reason: %1").arg(json["message"].toString()));
96 emit finished(StationsRegister, false, data);
97 return;
98 }
99 if(!json.contains("uuid")){
100 emit finishedStationsRegister(false, "", tr("No uuid in response found"));
101 emit finished(StationsRegister, false, data);
102 return;
103 }
104
105 emit finishedStationsRegister(true, QUuid(json["uuid"].toString()), json["message"].toString());
106 emit finished(StationsRegister, false, data);
107}
108
109QNetworkReply* SiWebserviceClient::getNetworkReply(QObject* sender, ReplyType type) const
110{
111 if(!sender){
112 emit error(Error, tr("No sender object found"));
113 return NULL;
114 }
115 QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender);
116 if(!reply){
117 emit error(Error, tr("No reply found"));
118 return NULL;
119 }
120
121 if(reply->error() != QNetworkReply::NoError){
122 emit error(type, reply->errorString());
123 reply->deleteLater();
124 return NULL;
125 }
126
127 return reply;
128}
129
130QString SiWebserviceClient::appendSlashToUrl(QString url)
131{
132 if(url.right(1) != "/")
133 url += "/";
134 return url;
135}
Note: See TracBrowser for help on using the repository browser.