1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <qtgrpcglobal_p.h>
5
6#include "qgrpcchanneloptions.h"
7
8QT_BEGIN_NAMESPACE
9
10using namespace Qt::StringLiterals;
11
12/*!
13 \class QGrpcChannelOptions
14 \inmodule QtGrpc
15 \brief The QGrpcChannelOptions is an storage class used to set additional channel options.
16
17 QGrpcChannelOptions provides a set of functions to set and access the channel and default call
18 options that are used by gRPC channels to communicate with the services.
19*/
20
21struct QGrpcChannelOptionsPrivate
22{
23public:
24 QGrpcChannelOptionsPrivate(const QUrl &_host) : host(_host) { }
25
26 QUrl host;
27 std::optional<std::chrono::milliseconds> deadline;
28 QGrpcMetadata metadata;
29 std::optional<QStringList> credentialList;
30#if QT_CONFIG(ssl)
31 std::optional<QSslConfiguration> sslConfiguration;
32#endif
33};
34
35/*!
36 Constructs an QGrpcChannelOptions object with \a host value.
37*/
38QGrpcChannelOptions::QGrpcChannelOptions(const QUrl &host)
39 : dPtr(std::make_unique<QGrpcChannelOptionsPrivate>(args: host))
40{
41}
42
43/*!
44 Construct a copy of QGrpcChannelOptions with \a other object.
45*/
46QGrpcChannelOptions::QGrpcChannelOptions(const QGrpcChannelOptions &other)
47 : dPtr(std::make_unique<QGrpcChannelOptionsPrivate>(args&: *other.dPtr))
48{
49}
50
51/*!
52 Assigns \a other to this QGrpcChannelOptions and returns a reference to this
53 QGrpcChannelOptions.
54*/
55QGrpcChannelOptions &QGrpcChannelOptions::operator=(const QGrpcChannelOptions &other)
56{
57 *dPtr = *other.dPtr;
58 return *this;
59}
60
61/*!
62 Destroys the QGrpcChannelOptions object.
63*/
64QGrpcChannelOptions::~QGrpcChannelOptions() = default;
65
66/*!
67 Sets host value with \a host and returns updated QGrpcChannelOptions object.
68*/
69QGrpcChannelOptions &QGrpcChannelOptions::withHost(const QUrl &host)
70{
71 dPtr->host = host;
72 return *this;
73}
74
75/*!
76 Sets deadline value with \a deadline and returns updated QGrpcChannelOptions object.
77*/
78QGrpcChannelOptions &QGrpcChannelOptions::withDeadline(std::chrono::milliseconds deadline)
79{
80 dPtr->deadline = deadline;
81 return *this;
82}
83
84/*!
85 Sets \a metadata for a call and returns updated QGrpcCallOptions object.
86
87 For HTTP2-based channels, \a metadata is converted into HTTP/2 headers, that
88 added to each HTTP/2 request.
89*/
90QGrpcChannelOptions &QGrpcChannelOptions::withMetadata(const QGrpcMetadata &metadata)
91{
92 dPtr->metadata = metadata;
93 return *this;
94}
95
96/*!
97 Returns deadline value for every call on the channel.
98
99 Deadline value controls the maximum execution time of any call or stream
100 executed on the channel.
101
102 If value was not set returns empty std::optional.
103*/
104std::optional<std::chrono::milliseconds> QGrpcChannelOptions::deadline() const
105{
106 return dPtr->deadline;
107}
108
109/*!
110 Returns host value for every call on the channel.
111*/
112QUrl QGrpcChannelOptions::host() const
113{
114 return dPtr->host;
115}
116
117/*!
118 Returns metadata used for a call.
119
120 If value was not set returns empty QGrpcMetadata.
121*/
122QGrpcMetadata QGrpcChannelOptions::metadata() const
123{
124 return dPtr->metadata;
125}
126
127#if QT_CONFIG(ssl)
128/*!
129 Sets SSL configuration with \a sslConfiguration and returns updated QGrpcChannelOptions object.
130*/
131QGrpcChannelOptions &QGrpcChannelOptions::withSslConfiguration(
132 const QSslConfiguration &sslConfiguration)
133{
134 dPtr->sslConfiguration = sslConfiguration;
135 return *this;
136}
137
138/*!
139 Returns SSL configuration for the channel.
140
141 If value was not set returns empty std::optional.
142*/
143std::optional<QSslConfiguration> QGrpcChannelOptions::sslConfiguration() const
144{
145 return dPtr->sslConfiguration;
146}
147#endif
148
149QT_END_NAMESPACE
150

source code of qtgrpc/src/grpc/qgrpcchanneloptions.cpp