HydroCoupleOgc  0.1.0
OGC web service clients (WMS, WMTS, WCS, WFS) over Qt Network (0.1.0)
Loading...
Searching...
No Matches
httpclient.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// Copyright (C) 2026 HydroCouple
3
15
16#ifndef HYDROCOUPLEOGC_HTTPCLIENT_H
17#define HYDROCOUPLEOGC_HTTPCLIENT_H
18
21
22#include <QByteArray>
23#include <QHash>
24#include <QObject>
25#include <QString>
26#include <QUrl>
27
28#include <functional>
29
30class QNetworkAccessManager;
31class QNetworkReply;
32
33namespace HydroCouple::Ogc
34{
39 {
41 bool ok = false;
42
44 int statusCode = 0;
45
46 QString contentType;
47
57 QByteArray body;
58
60 QString error;
61
63 QUrl url;
64 };
65
77 class HYDROCOUPLEOGC_EXPORT HttpClient : public QObject
78 {
79 Q_OBJECT
80
81 public:
83 using Callback = std::function<void(const HttpResponse &)>;
84
86 using RequestId = quint64;
87
96 explicit HttpClient(QObject *parent = nullptr);
97
106 void setCacheDirectory(const QString &path, qint64 maximumBytes = 0);
107
109 void setUserAgent(const QString &userAgent);
110
112 void setTransferTimeout(int milliseconds);
113
123 RequestId get(const QUrl &url, const ServiceCredentials &credentials,
124 Callback callback);
125
127 RequestId get(const QUrl &url, Callback callback);
128
135 void cancel(RequestId id);
136
138 void cancelAll();
139
141 [[nodiscard]] int pendingCount() const;
142
149 [[nodiscard]] int inFlightCount() const;
150
151 private:
152 struct Waiter
153 {
154 RequestId id = 0;
155 QByteArray key;
156 Callback callback;
157 };
158
159 struct InFlight
160 {
161 QNetworkReply *reply = nullptr;
162 QList<RequestId> waiters;
163 };
164
165 void deliver(const QByteArray &key, const HttpResponse &response);
166
167 QNetworkAccessManager *m_manager = nullptr;
168 QString m_userAgent;
169 int m_transferTimeout = 30000;
170 RequestId m_nextId = 1;
171
172 QHash<RequestId, Waiter> m_waiters;
173 QHash<QByteArray, InFlight> m_inFlight;
174 };
175
176} // namespace HydroCouple::Ogc
177
178#endif // HYDROCOUPLEOGC_HTTPCLIENT_H
int pendingCount() const
How many callers are waiting.
Definition httpclient.cpp:255
RequestId get(const QUrl &url, const ServiceCredentials &credentials, Callback callback)
Asks for url, calling callback when it is known.
Definition httpclient.cpp:81
void setUserAgent(const QString &userAgent)
Sent on every request; identifies the client to a server's logs.
Definition httpclient.cpp:66
void setTransferTimeout(int milliseconds)
How long a request may stall before it is given up on.
Definition httpclient.cpp:71
std::function< void(const HttpResponse &)> Callback
Called on the thread the client lives on, once, per get().
Definition httpclient.h:83
void cancelAll()
Gives up on everything outstanding.
Definition httpclient.cpp:243
void setCacheDirectory(const QString &path, qint64 maximumBytes=0)
Where responses are cached between sessions.
Definition httpclient.cpp:43
void cancel(RequestId id)
Gives up on id.
Definition httpclient.cpp:201
HttpClient(QObject *parent=nullptr)
Constructs a client with nothing outstanding.
Definition httpclient.cpp:31
int inFlightCount() const
How many requests are actually on the wire.
Definition httpclient.cpp:260
quint64 RequestId
What get() returns, and cancel() takes. Never 0 for a live ask.
Definition httpclient.h:86
Symbol visibility for the shared library.
#define HYDROCOUPLEOGC_EXPORT
Definition export.h:20
Definition axisorder.h:30
What a request has to carry to be let through, and how to say so in a log without saying it.
What came back.
Definition httpclient.h:39
QString contentType
Definition httpclient.h:46
QString error
Why not, when ok is false.
Definition httpclient.h:60
bool ok
Whether the body is the thing that was asked for.
Definition httpclient.h:41
QUrl url
What was finally fetched, after any redirects.
Definition httpclient.h:63
int statusCode
The HTTP status, or 0 when the request never reached a server.
Definition httpclient.h:44
QByteArray body
The body, delivered whether or not the request succeeded.
Definition httpclient.h:57