Stellarium 0.15.2
AbstractAPIService.hpp
1 /*
2  * Stellarium Remote Control plugin
3  * Copyright (C) 2015 Florian Schaukowitsch
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef ABSTRACTAPISERVICE_HPP_
21 #define ABSTRACTAPISERVICE_HPP_
22 
23 #include <QByteArray>
24 #include <QMap>
25 #include <QObject>
26 
27 //Uncomment to force each service to run in the HTTP handling threads themselves, like the initial versions
28 //#define FORCE_THREADED_SERVICES
29 
30 class HttpResponse;
31 class APIController;
32 
35 
39 {
40 public:
42  APIServiceResponse() : status(-1)
43  {
44 
45  }
46 
48  void setHeader(const QByteArray& name, const QByteArray& val);
50  void setHeader(const QByteArray& name, const int val);
52  void setStatus(int status, const QByteArray& text);
53 
55  void setData(const QByteArray& data);
57  void appendData(const QByteArray& data);
58 
60  void writeRequestError(const QByteArray& msg);
63  void writeJSON(const QJsonDocument& doc);
64 
65 private:
66  int status;
67  QByteArray statusText;
68  QMap<QByteArray,QByteArray> headers;
69  QByteArray responseData;
70 
73  void applyResponse(HttpResponse* response) const;
74 
75  static int metaTypeId;
76  static int parametersMetaTypeId;
77  friend class APIController;
78 
79 };
80 
82 typedef QMultiMap<QByteArray, QByteArray> APIParameters;
83 
84 Q_DECLARE_METATYPE(APIServiceResponse)
85 Q_DECLARE_METATYPE(APIParameters)
86 
89 class AbstractAPIService : public QObject
90 {
91  Q_OBJECT
92 public:
94  AbstractAPIService(const QByteArray& serviceName, QObject* parent = 0) : QObject(parent), m_serviceName(serviceName)
95  {
96 
97  }
98 
99  virtual ~AbstractAPIService() { }
100 
102  QByteArray serviceName() { return m_serviceName; }
103 
110  virtual bool supportsThreadedOperation() const;
111 
114  virtual void update(double deltaTime);
115 
118  Q_INVOKABLE APIServiceResponse get(const QByteArray &operation, const APIParameters &parameters);
121  Q_INVOKABLE APIServiceResponse post(const QByteArray &operation, const APIParameters &parameters, const QByteArray& data);
122 
123 protected:
131  virtual void getImpl(const QByteArray& operation, const APIParameters& parameters, APIServiceResponse& response);
140  virtual void postImpl(const QByteArray& operation, const APIParameters& parameters, const QByteArray& data, APIServiceResponse& response);
141 
144  static const Qt::ConnectionType SERVICE_DEFAULT_INVOKETYPE;
145 
153  QString wrapHtml(QString& text, const QString& title) const;
154 private:
155  QByteArray m_serviceName;
156 };
157 
159 
160 #endif
void writeRequestError(const QByteArray &msg)
Sets the HTTP status to 400, and sets the response data to the message.
void writeJSON(const QJsonDocument &doc)
Sets the Content-Type to "application/json" and serializes the given document into JSON text format...
Thread-safe version of HttpResponse that can be passed around through QMetaObject::invokeMethod.
APIServiceResponse()
Constructs an invalid response.
void appendData(const QByteArray &data)
Appends to the current return data.
void setHeader(const QByteArray &name, const QByteArray &val)
Sets a specific HTTP header to the specified value.
AbstractAPIService(const QByteArray &serviceName, QObject *parent=0)
Abstract constructor. The service name is used by the APIController for request path mapping...
static const Qt::ConnectionType SERVICE_DEFAULT_INVOKETYPE
This defines the connection type QMetaObject::invokeMethod has to use inside a service: either Qt::Di...
Abstract base class for all Remote Control Plug-in service implementations.
QMultiMap< QByteArray, QByteArray > APIParameters
Defines the HTTP request parameters for the service.
void update(double deltaTime)
Should be called each frame from the main thread, like from StelModule::update.
void setData(const QByteArray &data)
Replaces the current return data.
QByteArray serviceName()
Returns the service name, used for request path mapping by the APIController.
This class handles the API-specific requests and dispatches them to the correct AbstractAPISerice imp...
void setStatus(int status, const QByteArray &text)
Sets the HTTP status type and status text.
This object represents a HTTP response, used to return something to the web client.
Definition: httpresponse.h:35