author | František Kučera <franta-hg@frantovo.cz> |
Sun, 06 Mar 2022 19:51:37 +0100 | |
branch | v_0 |
changeset 2 | dccedac46e7e |
parent 1 | ddc12e789d00 |
child 3 | 9c710397ced6 |
permissions | -rw-r--r-- |
0
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <memory> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <string> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <vector> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <codecvt> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <regex> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <stdexcept> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
|
1
ddc12e789d00
link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
26 |
#include <curl/curl.h> |
ddc12e789d00
link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
27 |
|
0
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
#include <relpipe/common/type/typedefs.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
#include <relpipe/reader/TypeId.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
#include <relpipe/writer/Factory.h> |
1
ddc12e789d00
link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
34 |
#include <relpipe/writer/TypeId.h> |
0
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
#include <relpipe/cli/RelpipeCLIException.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
#include "Configuration.h" |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
namespace relpipe { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
namespace tr { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
namespace http { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
class HTTPHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
private: |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
shared_ptr<relpipe::writer::RelationalWriter> relationalWriter; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
Configuration configuration; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
size_t currentAttributeIndex = 0; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
size_t currentRecordNumber = 1; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
|
2
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
54 |
void writeCallback(std::string value) { |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
55 |
relationalWriter->writeAttribute(L"got response: " + convertor.from_bytes(value)); |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
56 |
} |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
57 |
|
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
58 |
static uint writeCurlCallback(char* in, uint size, uint nmemb, HTTPHandler* instance) { |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
59 |
uint r = size * nmemb; |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
60 |
instance->writeCallback(std::string(in, r)); |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
61 |
return r; |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
62 |
} |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
63 |
|
0
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
public: |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
HTTPHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
} |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
virtual ~HTTPHandler() { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
} |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
|
1
ddc12e789d00
link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
|
2
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
75 |
relationalWriter->startRelation(name + L"_curl",{ |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
76 |
{L"value", relpipe::writer::TypeId::STRING} |
1
ddc12e789d00
link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
}, true); |
ddc12e789d00
link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
|
2
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
79 |
relationalWriter->writeAttribute(L"curl version = " + convertor.from_bytes(curl_version())); |
1
ddc12e789d00
link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
|
0
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
} |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
void attribute(const relpipe::common::type::StringX& value) override { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
|
2
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
85 |
std::string url = convertor.to_bytes(value); |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
86 |
|
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
87 |
CURL* curl = curl_easy_init(); |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
88 |
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
89 |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, this); |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
90 |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCurlCallback); |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
91 |
curl_easy_perform(curl); |
dccedac46e7e
do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
92 |
curl_easy_cleanup(curl); |
0
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
} |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
void endOfPipe() { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
} |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
}; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
} |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
} |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
} |