Files
memgraph/src/requests/requests.cpp
Matija Santl 4c27596fdd Implement kafka transform functionality
Summary:
First iteration in implementing kafka.
Currently, memgraph streams won't use the transform script provided in the
`CREATE STREAM` query.

There is a manual test that serves a POC purpose which we'll use to fully wire
kafka in memgraph.

Since streams need to download the script, I moved curl init from
telemetry.

Reviewers: teon.banek, mferencevic

Reviewed By: mferencevic

Subscribers: ipaljak, pullbot, buda

Differential Revision: https://phabricator.memgraph.io/D1491
2018-07-19 12:52:25 +02:00

111 lines
3.3 KiB
C++

#include "requests/requests.hpp"
#include <cstdio>
#include <curl/curl.h>
#include <fmt/format.h>
#include <gflags/gflags.h>
#include <glog/logging.h>
namespace requests {
namespace {
size_t CurlWriteCallback(char *ptr, size_t size, size_t nmemb, void *userdata) {
return nmemb;
}
} // namespace
void Init() { curl_global_init(CURL_GLOBAL_ALL); }
bool RequestPostJson(const std::string &url, const nlohmann::json &data,
int timeout_in_seconds) {
CURL *curl = nullptr;
CURLcode res = CURLE_UNSUPPORTED_PROTOCOL;
long response_code = 0;
struct curl_slist *headers = NULL;
std::string payload = data.dump();
std::string user_agent = fmt::format("memgraph/{}", gflags::VersionString());
curl = curl_easy_init();
if (!curl) return false;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteCallback);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout_in_seconds);
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
DLOG(WARNING) << "Couldn't perform request: " << curl_easy_strerror(res);
return false;
}
if (response_code != 200) {
DLOG(WARNING) << "Request response code isn't 200 (received "
<< response_code << ")!";
return false;
}
return true;
}
bool CreateAndDownloadFile(const std::string &url, const std::string &path,
int timeout_in_seconds) {
CURL *curl = nullptr;
CURLcode res = CURLE_UNSUPPORTED_PROTOCOL;
long response_code = 0;
std::string user_agent = fmt::format("memgraph/{}", gflags::VersionString());
curl = curl_easy_init();
if (!curl) return false;
FILE *file = std::fopen(path.c_str(), "w");
if (!file) return false;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout_in_seconds);
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_cleanup(curl);
std::fclose(file);
if (res != CURLE_OK) {
DLOG(WARNING) << "Couldn't perform request: " << curl_easy_strerror(res);
return false;
}
if (response_code != 200) {
DLOG(WARNING) << "Request response code isn't 200 (received "
<< response_code << ")!";
return false;
}
return true;
}
} // namespace requests