Skip to content

Commit aef5247

Browse files
committed
* Fixed failing CI issues.
* Added unit test for progress callback. * Updated `README.md` with documentation for the new progress callback additions.
1 parent de61b7f commit aef5247

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ conn->SetProxy("37.187.100.23:3128");
207207
RestClient::Response res = conn->get("/get");
208208
```
209209

210+
## Progress callback
211+
212+
Two simple wrapper functions are provided to setup progress callback for uploads/downloads;
213+
Calling `conn->SetFileProgressCallback(callback)` with a callback parameter matching the prototype `int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)` will setup the progress callback.
214+
Calling `conn->SetFileProgressCallbackData(data)` is optional. This will set the data pointer which is the first parameter fed back to the progress callback - `clientp`. If this isn't set then `clientp` will default to the connection object `conn`.
215+
216+
```cpp
217+
// set CURLOPT_NOPROGRESS
218+
// set CURLOPT_PROGRESSFUNCTION
219+
conn->SetFileProgressCallback(progressFunc);
220+
// set CURLOPT_PROGRESSDATA
221+
conn->SetFileProgressCallbackData(data);
222+
```
223+
210224
## Dependencies
211225
- [libcurl][]
212226

include/restclient-cpp/connection.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class Connection {
7979
* Member 'timeout' contains the configured timeout
8080
* @var Info::followRedirects
8181
* Member 'followRedirects' contains whether or not to follow redirects
82+
* @var Info::progressFn
83+
* Member 'progressFn' file progress callback function
84+
* @var Info::progressFnData
85+
* Member 'progressFnData' file progress callback data
8286
* @var Info::basicAuth
8387
* Member 'basicAuth' contains information about basic auth
8488
* @var basicAuth::username

source/connection.cc

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ RestClient::Connection::SetNoSignal(bool no) {
194194
*
195195
*/
196196
void
197-
RestClient::Connection::SetFileProgressCallback(curl_progress_callback progressFn) {
198-
this->progressFn = progressFn;
197+
RestClient::Connection::SetFileProgressCallback(curl_progress_callback
198+
progressFn) {
199+
this->progressFn = progressFn;
199200
}
200201

201202
/**
@@ -206,7 +207,7 @@ RestClient::Connection::SetFileProgressCallback(curl_progress_callback progressF
206207
*/
207208
void
208209
RestClient::Connection::SetFileProgressCallbackData(void* data) {
209-
this->progressFnData = data;
210+
this->progressFnData = data;
210211
}
211212

212213
/**
@@ -365,15 +366,19 @@ RestClient::Connection::performCurlRequest(const std::string& uri) {
365366
}
366367

367368
// set file progress callback
368-
if (this->progressFn)
369-
{
369+
if (this->progressFn) {
370370
curl_easy_setopt(this->curlHandle, CURLOPT_NOPROGRESS, 0);
371-
curl_easy_setopt(this->curlHandle, CURLOPT_PROGRESSFUNCTION, this->progressFn);
371+
curl_easy_setopt(this->curlHandle,
372+
CURLOPT_PROGRESSFUNCTION,
373+
this->progressFn);
372374
if (this->progressFnData) {
373-
curl_easy_setopt(this->curlHandle, CURLOPT_PROGRESSDATA, this->progressFnData);
374-
}
375-
else {
376-
curl_easy_setopt(this->curlHandle, CURLOPT_PROGRESSDATA, this);
375+
curl_easy_setopt(this->curlHandle,
376+
CURLOPT_PROGRESSDATA,
377+
this->progressFnData);
378+
} else {
379+
curl_easy_setopt(this->curlHandle,
380+
CURLOPT_PROGRESSDATA,
381+
this);
377382
}
378383
}
379384

test/test_connection.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ TEST_F(ConnectionTest, TestNoSignal)
217217
EXPECT_EQ(200, res.code);
218218
}
219219

220+
TEST_F(ConnectionTest, TestSetProgress)
221+
{
222+
static double totalToDownload = 0;
223+
static double totalDownloaded = 0;
224+
225+
auto progressCallback = [](void* pData, double downloadTotal, double downloaded, double uploadTotal, double uploaded) -> int {
226+
totalToDownload = downloadTotal;
227+
totalDownloaded = downloaded;
228+
return 0;
229+
};
230+
231+
conn->SetFileProgressCallback(progressCallback);
232+
conn->SetFileProgressCallbackData(nullptr);
233+
234+
RestClient::Response res = conn->get("/anything/{test_data}");
235+
EXPECT_EQ(200, res.code);
236+
EXPECT_EQ(totalToDownload, 267);
237+
EXPECT_EQ(totalDownloaded, 267);
238+
}
239+
220240
TEST_F(ConnectionTest, TestProxy)
221241
{
222242
conn->SetProxy("127.0.0.1:3128");

0 commit comments

Comments
 (0)