Skip to content

Commit

Permalink
fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
ejacques committed Oct 27, 2020
1 parent c4ea591 commit f98b8de
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 95 deletions.
19 changes: 13 additions & 6 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,34 @@ librestclient_cpp_la_LDFLAGS=-version-info 2:1:1

dist_doc_DATA = README.md

.PHONY: test check clean-coverage-files coverage-html include/restclient-cpp/version.h lint ci docker-services
.PHONY: test check clean-coverage-files coverage-html include/restclient-cpp/version.h lint ci docker-services clean-docker-services

include/restclient-cpp/version.h:
m4 -I ${top_srcdir}/m4 -DM4_RESTCLIENT_VERSION=$(PACKAGE_VERSION) version.h.m4 > ${top_srcdir}/$@



test: check
test: check docker-services
./test-program

valgrind: check
valgrind --leak-check=full --error-exitcode=1 ./test-program

lint:
cpplint --filter=-legal/copyright include/restclient-cpp/*.h source/*.cc
cpplint --filter=-legal/copyright --root=$(CURDIR) include/restclient-cpp/*.h source/*.cc

docker-services:
docker inspect --format="{{ .State.Running }}" restclient-proxy &> /dev/null || docker run -d --name restclient-proxy -p 3128:3128 chrisdaish/squid
docker ps -a
[ -n "$$(docker ps --quiet --filter name=restclient-cpp-httpbin)" ] || \
docker run --detach -p 8998:80 --name restclient-cpp-httpbin kennethreitz/httpbin
[ -n "$$(docker ps --quiet --filter name=restclient-cpp-squid)" ] || \
docker run --detach -p 3128:3128 --name restclient-cpp-squid chrisdaish/squid
docker ps --all --filter 'name=^restclient-cpp-'

ci: lint docker-services test valgrind
clean-docker-services:
docker rm --force restclient-cpp-httpbin 2>/dev/null || true
docker rm --force restclient-cpp-squid 2>/dev/null || true

ci: lint test valgrind

clean-local:
find . -name "*.gcda" -print0 | xargs -0 rm
Expand Down
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,47 @@ conn->SetFileProgressCallback(progressFunc);
conn->SetFileProgressCallbackData(data);
```
### Write callback
A write callback function can be provided for processing data as it's received from a GET call (for instance the [Kubernetes Watch API](https://kubernetes.io/docs/reference/using-api/api-concepts/#efficient-detection-of-changes)).
Calling `conn->SetWriteFunction(callback)` with a function parameter matching the prototype `size_t write_function(void *data, size_t size, size_t nmemb, void *userdata)int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)` will setup the write function.
Here is an example of a write callback function, processing result data line by line.
```cpp
auto writeCallback = [](void *data, size_t size, size_t nmemb, void *userdata) -> size_t
{
size_t bytes = size * nmemb;
try
{
// Add to the buffer
auto res = reinterpret_cast<RestClient::Response *>(userdata);
res->body.append(static_cast<char*>(data), bytes);
// If the last character is not a new line, wait for the rest.
if ('\n' != *(res->body.end() - 1))
{
return bytes;
}
// Process data one line at a time.
std::stringstream stream(res->body);
std::string line;
while (std::getline(stream, line))
{
// Do something with the line here...
}
// Done processing the line
res->body.clear();
}
catch(std::exception e)
{
// Log caught exception here
return 0;
}
return bytes;
};
```

## Error handling
When restclient-cpp encounters an error, generally the error (or "status") code is returned in the `Response` (see
[Response struct in restclient.h](https://github.com/mrtazz/restclient-cpp/blob/master/include/restclient-cpp/restclient.h)). This error code can be either
Expand Down
11 changes: 4 additions & 7 deletions include/restclient-cpp/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@ class Connection {
// set CURLOPT_SSLKEY. Default format is PEM
void SetKeyPath(const std::string& keyPath);

// set CURLOPT_SSLKEYTYPE
void SetKeyType(const std::string& keyType);

// set CURLOPT_KEYPASSWD.
void SetKeyPassword(const std::string& keyPassword);

Expand Down Expand Up @@ -236,8 +233,8 @@ class Connection {
RestClient::Response options(const std::string& uri);

// GET with custom response structure
RestClient::Response&
get(const std::string& uri, RestClient::Response& response);
RestClient::Response*
get(const std::string& uri, RestClient::Response* response);

private:
CURL* getCurlHandle();
Expand All @@ -260,13 +257,13 @@ class Connection {
std::string certPath;
std::string certType;
std::string keyPath;
std::string keyType;
std::string keyPassword;
std::string uriProxy;
std::string unixSocketPath;
char curlErrorBuf[CURL_ERROR_SIZE];
RestClient::WriteCallback writeCallback;
RestClient::Response& performCurlRequest(const std::string& uri, RestClient::Response& resp);
RestClient::Response*
performCurlRequest(const std::string& uri, RestClient::Response* resp);
RestClient::Response performCurlRequest(const std::string& uri);
};
}; // namespace RestClient
Expand Down
54 changes: 21 additions & 33 deletions source/connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,6 @@ RestClient::Connection::SetKeyPath(const std::string& keyPath) {
this->keyPath = keyPath;
}

/**
* @brief set key type
*
* @param certificate type (e.g. "PEM" or "DER")
*
*/
void
RestClient::Connection::SetKeyType(const std::string& keyType) {
this->keyType = keyType;
}

/**
* @brief set key password
*
Expand Down Expand Up @@ -334,14 +323,16 @@ RestClient::Connection::SetUnixSocketPath(const std::string& unixSocketPath) {
}

/**
* @brief set callback for writing received data.
* See https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
* @brief set callback for writing received data. Last parameter
* to callback (userdata) points to a RestClient::Response. For details,
* see https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
*
* @param write_callback - callback to handle received data
*
*/
void
RestClient::Connection::SetWriteFunction(RestClient::WriteCallback writeCallback) {
RestClient::Connection::SetWriteFunction(RestClient::WriteCallback
writeCallback) {
this->writeCallback = writeCallback;
}

Expand All @@ -361,7 +352,7 @@ RestClient::Response
RestClient::Connection::performCurlRequest(const std::string& uri) {
// init return type
RestClient::Response ret = {};
performCurlRequest(uri, ret);
performCurlRequest(uri, &ret);
return ret;
}

Expand All @@ -378,12 +369,13 @@ RestClient::Connection::performCurlRequest(const std::string& uri) {
*
* @return reference to response struct for chaining
*/
RestClient::Response&
RestClient::Connection::performCurlRequest(const std::string& uri, RestClient::Response& ret) {
RestClient::Response*
RestClient::Connection::performCurlRequest(const std::string& uri,
RestClient::Response* ret) {
// init return type
ret.body.clear();
ret.code = 0;
ret.headers.clear();
ret->body.clear();
ret->code = 0;
ret->headers.clear();

std::string url = std::string(this->baseUrl + uri);
std::string headerString;
Expand All @@ -396,12 +388,12 @@ RestClient::Connection::performCurlRequest(const std::string& uri, RestClient::R
curl_easy_setopt(getCurlHandle(), CURLOPT_WRITEFUNCTION,
this->writeCallback);
/** set data object to pass to callback function */
curl_easy_setopt(getCurlHandle(), CURLOPT_WRITEDATA, &ret);
curl_easy_setopt(getCurlHandle(), CURLOPT_WRITEDATA, ret);
/** set the header callback function */
curl_easy_setopt(getCurlHandle(), CURLOPT_HEADERFUNCTION,
Helpers::header_callback);
/** callback object for headers */
curl_easy_setopt(getCurlHandle(), CURLOPT_HEADERDATA, &ret);
curl_easy_setopt(getCurlHandle(), CURLOPT_HEADERDATA, ret);
/** set http headers */
for (HeaderFields::const_iterator it = this->headerFields.begin();
it != this->headerFields.end(); ++it) {
Expand Down Expand Up @@ -485,11 +477,6 @@ RestClient::Connection::performCurlRequest(const std::string& uri, RestClient::R
curl_easy_setopt(getCurlHandle(), CURLOPT_SSLKEY,
this->keyPath.c_str());
}
// set key type
if (!this->keyType.empty()) {
curl_easy_setopt(getCurlHandle(), CURLOPT_SSLKEYTYPE,
this->keyType.c_str());
}
// set key password
if (!this->keyPassword.empty()) {
curl_easy_setopt(getCurlHandle(), CURLOPT_KEYPASSWD,
Expand Down Expand Up @@ -517,12 +504,12 @@ RestClient::Connection::performCurlRequest(const std::string& uri, RestClient::R
if (retCode > 99) {
retCode = -1;
}
ret.code = retCode;
ret.body = curl_easy_strerror(res);
ret->code = retCode;
ret->body = curl_easy_strerror(res);
} else {
int64_t http_code = 0;
curl_easy_getinfo(getCurlHandle(), CURLINFO_RESPONSE_CODE, &http_code);
ret.code = static_cast<int>(http_code);
ret->code = static_cast<int>(http_code);
}

this->lastRequest.curlError = std::string(this->curlErrorBuf);
Expand Down Expand Up @@ -569,9 +556,10 @@ RestClient::Connection::get(const std::string& url) {
*
* @return response struct ref for chaining
*/
RestClient::Response&
RestClient::Connection::get(const std::string& url, RestClient::Response& response) {
return this->performCurlRequest(url, response);
RestClient::Response*
RestClient::Connection::get(const std::string& url,
RestClient::Response* response) {
return this->performCurlRequest(url, response);
}
/**
* @brief HTTP POST method
Expand Down
Loading

0 comments on commit f98b8de

Please sign in to comment.