src/HTTPHandler.h
branchv_0
changeset 5 165f6162524d
parent 4 602462d04c57
child 6 59c9ca066322
--- a/src/HTTPHandler.h	Sat Mar 12 02:03:44 2022 +0100
+++ b/src/HTTPHandler.h	Sat Mar 12 20:48:25 2022 +0100
@@ -1,6 +1,6 @@
 /**
  * Relational pipes
- * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -36,6 +36,7 @@
 #include <relpipe/cli/RelpipeCLIException.h>
 
 #include "Configuration.h"
+#include "HTTPClient.h"
 
 namespace relpipe {
 namespace tr {
@@ -51,29 +52,6 @@
 	size_t currentAttributeIndex = 0;
 	size_t currentRecordNumber = 1;
 
-	void writeCallback(std::string value) {
-		// TODO: write this attribute even if this method was not called
-		// TODO: support also binary data or other encodings, not only UTF-8 text
-		relationalWriter->writeAttribute(convertor.from_bytes(value));
-	}
-
-	static uint writeCurlCallback(char* buffer, size_t size, size_t nmemb, HTTPHandler* instance) {
-		size_t r = size * nmemb;
-		instance->writeCallback(std::string(buffer, r));
-		return r;
-	}
-
-	void headersCallback(std::string value) {
-		// TODO: parse name=value and store for later use
-		std::cerr << std::endl << "HTTP response headers:" << std::endl << ">>>" << value << "<<<" << std::endl << "--------" << std::endl;
-	}
-
-	static uint headersCurlCallback(char* buffer, size_t size, size_t nmemb, HTTPHandler* instance) {
-		size_t r = size * nmemb;
-		instance->headersCallback(std::string(buffer, r));
-		return r;
-	}
-
 public:
 
 	HTTPHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
@@ -96,34 +74,30 @@
 		relationalWriter->startRelation(name + L"_curl_response",{
 			{L"url", relpipe::writer::TypeId::STRING},
 			{L"body", relpipe::writer::TypeId::STRING},
-			{L"response_code", relpipe::writer::TypeId::INTEGER}
+			{L"response_code", relpipe::writer::TypeId::INTEGER},
+			// {L"success", relpipe::writer::TypeId::BOOLEAN},
 		}, true);
 
 	}
 
 	void attribute(const relpipe::common::type::StringX& value) override {
 
-		relationalWriter->writeAttribute(value);
-
-		std::string url = convertor.to_bytes(value);
-		relpipe::common::type::Integer responseCode = 0;
+		std::shared_ptr<HTTPClient> http(HTTPClient::open());
 
-		CURL* curl = curl_easy_init();
-		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
-
-		curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
-		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCurlCallback);
+		HTTPClient::Request request;
+		request.method = HTTPClient::Method::GET;
+		request.url = convertor.to_bytes(value);
 
-		curl_easy_setopt(curl, CURLOPT_HEADERDATA, this);
-		curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headersCurlCallback);
-
-		curl_easy_perform(curl);
+		HTTPClient::Response response = http->exchange(request);
+		relpipe::common::type::Integer responseCode = response.responseCode;
 
-		curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
+		// std::cerr << "url = >>>" << convertor.to_bytes(value) << "<<<" << std::endl;
+		// std::cerr << "body = >>>" << response.body << "<<<" << std::endl;
 
-		curl_easy_cleanup(curl);
-
+		relationalWriter->writeAttribute(value);
+		relationalWriter->writeAttribute(convertor.from_bytes(response.body));
 		relationalWriter->writeAttribute(&responseCode, typeid (responseCode));
+		// relationalWriter->writeAttribute(&response.success, typeid (response.success));
 	}
 
 	void endOfPipe() {