QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsserverogcapihandler.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsserverogcapihandler.h - QgsServerOgcApiHandler
3 
4  ---------------------
5  begin : 10.7.2019
6  copyright : (C) 2019 by Alessandro Pasotti
7  email : elpaso at itopen dot it
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QGSSERVEROGCAPIHANDLER_H
17 #define QGSSERVEROGCAPIHANDLER_H
18 
19 #include <QRegularExpression>
20 #include "qgis_server.h"
22 #include "qgsserverogcapi.h"
23 #include "nlohmann/json_fwd.hpp"
24 #include "inja/inja.hpp"
25 
26 #ifndef SIP_RUN
27 using namespace nlohmann;
28 #endif
29 
31 
94 class SERVER_EXPORT QgsServerOgcApiHandler
95 {
96 
97  public:
98 
99  virtual ~QgsServerOgcApiHandler();
100 
101  // /////////////////////////////////////////////
102  // MAIN Section (operational)
103 
113  virtual QRegularExpression path() const = 0;
114 
116  virtual std::string operationId() const = 0;
117 
124  virtual QList<QgsServerQueryStringParameter> parameters( const QgsServerApiContext &context ) const { Q_UNUSED( context ); return { }; }
125 
126  // /////////////////////////////////////////////
127  // METADATA Sections (informative)
128 
130  virtual std::string summary() const = 0;
131 
133  virtual std::string description() const = 0;
134 
136  virtual std::string linkTitle() const = 0;
137 
139  virtual QgsServerOgcApi::Rel linkType() const = 0;
140 
142  virtual QStringList tags() const { return {}; }
143 
150  virtual QgsServerOgcApi::ContentType defaultContentType() const;
151 
158  QList<QgsServerOgcApi::ContentType> contentTypes() const SIP_SKIP;
159 
168  virtual void handleRequest( const QgsServerApiContext &context ) const SIP_THROW( QgsServerApiBadRequestException ) SIP_VIRTUALERRORHANDLER( serverapi_badrequest_exception_handler );
169 
189  virtual QVariantMap values( const QgsServerApiContext &context ) const SIP_THROW( QgsServerApiBadRequestException );
190 
195  QString contentTypeForAccept( const QString &accept ) const;
196 
197  // /////////////////////////////////////////////////////
198  // Utility methods: override should not be required
199 
200 #ifndef SIP_RUN // Skip SIP
201 
225  void write( json &data, const QgsServerApiContext &context, const json &htmlMetadata = nullptr ) const;
226 
233  void jsonDump( json &data, const QgsServerApiContext &context, const QString &contentType = QStringLiteral( "application/json" ) ) const;
234 
241  void htmlDump( const json &data, const QgsServerApiContext &context ) const;
242 
250  virtual json schema( const QgsServerApiContext &context ) const;
251 
261  json link( const QgsServerApiContext &context,
262  const QgsServerOgcApi::Rel &linkType = QgsServerOgcApi::Rel::self,
263  const QgsServerOgcApi::ContentType contentType = QgsServerOgcApi::ContentType::JSON,
264  const std::string &title = "" ) const;
265 
274  json links( const QgsServerApiContext &context ) const;
275 
276 
286  QgsVectorLayer *layerFromContext( const QgsServerApiContext &context ) const;
287 
288 #endif // SIP skipped
289 
311  void write( QVariant &data, const QgsServerApiContext &context, const QVariantMap &htmlMetadata = QVariantMap() ) const SIP_THROW( QgsServerApiBadRequestException );
312 
320  std::string href( const QgsServerApiContext &context, const QString &extraPath = QString(), const QString &extension = QString() ) const;
321 
330  virtual const QString templatePath( const QgsServerApiContext &context ) const;
331 
337  virtual const QString staticPath( const QgsServerApiContext &context ) const;
338 
348  QgsServerOgcApi::ContentType contentTypeFromRequest( const QgsServerRequest *request ) const;
349 
353  static QString parentLink( const QUrl &url, int levels = 1 );
354 
359  static QgsVectorLayer *layerFromCollectionId( const QgsServerApiContext &context, const QString &collectionId );
360 
366  static json defaultResponse() SIP_SKIP;
367 
375  json jsonTags( ) const SIP_SKIP;
376 
377  protected:
378 
382  void setContentTypesInt( const QList<int> &contentTypes ) SIP_PYNAME( setContentTypes );
383 
388  void setContentTypes( const QList<QgsServerOgcApi::ContentType> &contentTypes ) SIP_SKIP;
389 
390  private:
391 
393  QList<QgsServerOgcApi::ContentType> mContentTypes = { QgsServerOgcApi::ContentType::JSON, QgsServerOgcApi::ContentType::HTML };
394 
395 
396 };
397 
398 #endif // QGSSERVEROGCAPIHANDLER_H
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsServerOgcApiHandler::tags
virtual QStringList tags() const
Tags.
Definition: qgsserverogcapihandler.h:142
QgsServerApiContext
The QgsServerApiContext class encapsulates the resources for a particular client request: the request...
Definition: qgsserverapicontext.h:39
QgsServerOgcApiHandler::summary
virtual std::string summary() const =0
Summary.
QgsServerRequest
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
Definition: qgsserverrequest.h:39
qgsserverquerystringparameter.h
QgsServerOgcApi
QGIS Server OGC API endpoint.
Definition: qgsserverogcapi.h:49
QgsServerOgcApiHandler::description
virtual std::string description() const =0
Description.
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:189
QgsServerOgcApiHandler::linkTitle
virtual std::string linkTitle() const =0
Title for the handler link.
qgsserverogcapi.h
QgsServerOgcApi::Rel
Rel
Rel link types.
Definition: qgsserverogcapi.h:58
QgsServerOgcApiHandler
The QgsServerOgcApiHandler abstract class represents a OGC API handler to be registered in QgsServerO...
Definition: qgsserverogcapihandler.h:95
QgsServerOgcApiHandler::parameters
virtual QList< QgsServerQueryStringParameter > parameters(const QgsServerApiContext &context) const
Returns a list of query string parameters.
Definition: qgsserverogcapihandler.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsServerOgcApi::ContentType
ContentType
Media types used for content negotiation, insert more specific first.
Definition: qgsserverogcapi.h:80
QgsServerOgcApiHandler::operationId
virtual std::string operationId() const =0
Returns the operation id for template file names and other internal references.
QgsServerOgcApiHandler::path
virtual QRegularExpression path() const =0
URL pattern for this handler, named capture group are automatically extracted and returned by values(...
QgsServerApiBadRequestException
Bad request error API exception.
Definition: qgsserverexception.h:245
QgsServerOgcApiHandler::linkType
virtual QgsServerOgcApi::Rel linkType() const =0
Main role for the resource link.
SIP_VIRTUALERRORHANDLER
#define SIP_VIRTUALERRORHANDLER(name)
Definition: qgis_sip.h:182