File indexing completed on 2024-11-10 12:47:30

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KGRAPHQL_H
0008 #define KGRAPHQL_H
0009 
0010 #include <QExplicitlySharedDataPointer>
0011 
0012 #include <functional>
0013 #include <memory>
0014 
0015 class QJsonObject;
0016 class QJsonValue;
0017 class QNetworkAccessManager;
0018 class QNetworkReply;
0019 class QNetworkRequest;
0020 class QObject;
0021 class QUrl;
0022 
0023 class KGraphQLRequest;
0024 class KGraphQLReply;
0025 
0026 /** GraphQL query functions. */
0027 namespace KGraphQL
0028 {
0029     /** Perform a GraphQL query. */
0030     void query(const KGraphQLRequest &request, QNetworkAccessManager *nam, const std::function<void(const KGraphQLReply&)> &callback, QObject *replyParent = nullptr);
0031 }
0032 
0033 class KGraphQLRequestPrivate;
0034 
0035 /** A GraphQL query request. */
0036 class KGraphQLRequest
0037 {
0038 public:
0039     explicit KGraphQLRequest();
0040     explicit KGraphQLRequest(const QUrl &url);
0041     KGraphQLRequest(const KGraphQLRequest&);
0042     KGraphQLRequest(KGraphQLRequest&&) noexcept;
0043     ~KGraphQLRequest();
0044     KGraphQLRequest& operator=(const KGraphQLRequest&);
0045 
0046     /** Sets the endpoint to perform the query on. */
0047     void setUrl(const QUrl &url);
0048 
0049     /** Set GraphQL query to perform. */
0050     void setQuery(const QString &query);
0051     /** Read GraphQL query from a file. */
0052     void setQueryFromFile(const QString &fileName);
0053 
0054     /** Sets query variable @p name to @p value. */
0055     void setVariable(const QString &name, const QJsonValue &value);
0056 
0057     /** Access to the underlying HTTP request. */
0058     QNetworkRequest& networkRequest();
0059     const QNetworkRequest& networkRequest() const;
0060 
0061     /** @internal */
0062     QByteArray rawData() const;
0063 
0064 private:
0065     QExplicitlySharedDataPointer<KGraphQLRequestPrivate> d;
0066 };
0067 
0068 class KGraphQLReplyPrivate;
0069 
0070 /** A GraphQL reply. */
0071 class KGraphQLReply
0072 {
0073 public:
0074     KGraphQLReply(KGraphQLReply&&) noexcept;
0075     KGraphQLReply(const KGraphQLReply&) = delete;
0076     KGraphQLReply& operator=(const KGraphQLReply) = delete;
0077     ~KGraphQLReply();
0078 
0079     /** Error type. */
0080     enum Error {
0081         NoError,
0082         NetworkError,
0083         QueryError
0084     };
0085     /** Result status. */
0086     Error error() const;
0087     /** Error message.
0088      *  Returns an empty string if error() return NoError.
0089      */
0090     QString errorString() const;
0091 
0092     /** Result data, valid in case of error() returning NoError. */
0093     QJsonObject data() const;
0094 
0095     /** The underlying QNetworkReply instance. */
0096     QNetworkReply* networkReply() const;
0097 
0098     /** @internal */
0099     QByteArray rawData() const;
0100 
0101 private:
0102     friend void KGraphQL::query(const KGraphQLRequest &request, QNetworkAccessManager *nam, const std::function<void(const KGraphQLReply&)> &callback, QObject *replyParent);
0103     KGraphQLReply(QNetworkReply *reply);
0104 
0105     std::unique_ptr<KGraphQLReplyPrivate> d;
0106 };
0107 
0108 #endif // KGRAPHQL_H