First implementation

It seems to work but I think the ticker value returned
by the remote call is wrong. Maybe I'm calling the wrong
endpoint ¯\_(ツ)_/¯ I don't know trying to work it out
This commit is contained in:
King_DuckZ 2022-05-12 23:40:27 +02:00
commit 1d61cc7f2f
21 changed files with 1474 additions and 0 deletions

135
src/main.cpp Normal file
View file

@ -0,0 +1,135 @@
/* Copyright 2022, Michele Santullo
* This file is part of duckticker.
*
* Wrenpp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wrenpp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with duckticker. If not, see <http://www.gnu.org/licenses/>.
*/
#include "nap/quick_rest.hpp"
#include <simdjson.h>
#include <wrenpp/vm_fun.hpp>
#include <wrenpp/def_configuration.hpp>
#include <utility>
namespace {
constexpr char g_script[] =
R"script(
foreign class TickerPrice {
construct new() { }
foreign price
foreign currency
}
class Bitcoinity {
foreign static ticker(currency)
}
class App {
construct new(currency) {
_currency = currency
}
start() {
var price = Bitcoinity.ticker(_currency)
System.print("Ticker: %(price.price) %(price.currency)")
}
}
var the_app = App.new("USD")
)script";
class TickerPrice {
public:
TickerPrice() = default;
TickerPrice (double price, std::string&& currency) :
m_price(price),
m_currency(std::move(currency))
{}
void set_price (double price) { m_price = price; }
void set_currency (std::string&& currency) { m_currency = std::move(currency); }
double price() const noexcept { return m_price; }
const char* currency() const noexcept { return m_currency.c_str(); }
private:
double m_price{};
std::string m_currency;
};
TickerPrice ticker_price (std::string_view currency) {
nap::QuickRest qrest{simdjson::SIMDJSON_PADDING};
qrest.set_user_agent("duckticker");
qrest.add_post("currency", currency);
auto response = qrest.fetch("https://bitcoinity.org/simple_switch_currency");
//std::cout << response.body << '\n';
simdjson::ondemand::parser parser;
simdjson::padded_string_view body{response.body, response.body.size() + simdjson::SIMDJSON_PADDING};
simdjson::ondemand::document doc = parser.iterate(body);
const double price_out = doc["price"];
const std::string_view currency_out = doc["currency"];
return {price_out, std::string{currency_out}};
}
void ticker_price_wren (wren::VM& vm) {
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "main");
*tp = ticker_price(wren::get<const char*>(vm, 1));
}
class MyWrenConfiguration : public wren::DefConfiguration {
public:
wren::foreign_method_t foreign_method_fn(
wren::VM* vm,
std::string_view module,
std::string_view class_name,
bool is_static,
std::string_view signature
) {
if ("main" == module) {
if (is_static and "Bitcoinity" == class_name and "ticker(_)" == signature) {
return &ticker_price_wren;
}
else if ("TickerPrice" == class_name) {
if (not is_static and "price" == signature)
return wren::make_method_bindable<&TickerPrice::price>();
else if (not is_static and "currency" == signature)
return wren::make_method_bindable<&TickerPrice::currency>();
}
}
return nullptr;
}
wren::foreign_class_t foreign_class_fn(
wren::VM* vm,
std::string_view module,
std::string_view class_name
) {
if (module == "main" and class_name == "TickerPrice")
return wren::make_foreign_class<TickerPrice>();
else
return {nullptr, nullptr};
}
};
} //unnamed namespace
int main() {
MyWrenConfiguration config;
wren::VM vm(&config, nullptr);
vm.interpret("main", g_script);
wren::call<void>(vm, {"main", "the_app"}, "start");
return 0;
}

29
src/meson.build Normal file
View file

@ -0,0 +1,29 @@
curlcpp_dep = dependency('curlcpp', version: '>=1.4',
fallback: ['curlcpp', 'curlcpp_dep'],
default_options: [
'default_library=static',
],
)
wrenpp_dep = dependency('wrenpp', version: '>=0.1.1',
fallback: ['wrenpp', 'wrenpp_dep'],
default_options: ['wren_with_rand=true'],
)
simdjson_dep = dependency('simdjson', version: '>=0.5.0',
fallback: ['simdjson', 'simdjson_dep'],
)
executable(meson.project_name(),
'main.cpp',
'nap/http_header_parse.cpp',
'nap/page_fetch.cpp',
'nap/quick_rest.cpp',
install: true,
dependencies: [
curlcpp_dep,
wrenpp_dep,
simdjson_dep,
],
cpp_args: compiler_opts,
)

View file

@ -0,0 +1,115 @@
/* Copyright 2020, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#include "private/http_header_parse.hpp"
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
namespace x3 = boost::spirit::x3;
namespace ascii = boost::spirit::x3::ascii;
namespace boost::spirit::x3::traits {
template <typename Char, typename Trait>
struct is_range<std::basic_string_view<Char, Trait>> : boost::mpl::true_ {};
} //namespace boost::spirit::x3::traits
BOOST_FUSION_ADAPT_STRUCT(nap::ParsedHeader,
version, code, message, fields
);
namespace nap {
namespace {
namespace parser {
//HTTP/1.1 200 OK
//X-RateLimit-Remaining: 5
//Retry-After: 86400
//Server: soapui
//X-RateLimit-Reset: 1592918516
//X-RateLimit-Limit: 6
//Date: Mon, 22 Jun 2020 13:21:55 GMT
//Set-Cookie: __cfduid=dac1342771e458af0ce6c7b462db1e18d1592832115; expires=Wed, 22-Jul-20 13:21:55 GMT; path=/; domain=.originsro.org; HttpOnly; SameSite=Lax
//Content-Type: application/json
//Content-Encoding: gzip
//Content-Length: 116629
using x3::lit;
using x3::digit;
using x3::no_skip;
using x3::string;
using x3::uint_;
using x3::lexeme;
using x3::char_;
using x3::eol;
template <typename Subject>
struct raw_directive : x3::raw_directive<Subject> {
using x3::raw_directive<Subject>::raw_directive;
template <typename Iterator, typename Context, typename RContext, typename Attribute>
bool parse(Iterator& first, Iterator const& last, Context const& context, RContext& rcontext, Attribute& attr) const {
x3::skip_over(first, last, context);
Iterator saved = first;
if (this->subject.parse(first, last, context, rcontext, x3::unused)) {
attr = { saved, typename Attribute::size_type(first - saved) };
return true;
}
return false;
}
};
struct raw_gen {
template <typename Subject>
raw_directive<typename x3::extension::as_parser<Subject>::value_type> operator[](Subject subject) const {
return { x3::as_parser(std::move(subject)) };
}
};
auto const raw = raw_gen{};
x3::rule<class HeaderEntry, std::pair<std::string_view, std::string_view>> header_entry = "header_entry";
auto const header_entry_def =
raw[lexeme[+(char_ - ':')]] >> ':' >> raw[lexeme[+(char_ - eol)]]
;
x3::rule<class HttpHeaderRule, ParsedHeader> http_header = "http_header";
auto const http_header_def =
no_skip[lit("HTTP/") >> raw[+digit >> -(string(".") >> +digit)]] > uint_ >
-raw[lexeme[+(char_ - eol)]] > eol >>
(header_entry % eol)
;
BOOST_SPIRIT_DEFINE(http_header, header_entry);
} //namespace parser
} //unnamed namespace
ParsedHeader header_parse (std::string_view text) {
ParsedHeader retval;
auto beg = text.begin();
const bool result = x3::phrase_parse(
beg,
text.end(),
parser::http_header,
x3::space - x3::eol,
retval
);
return retval;
}
} //namespace nap

37
src/nap/http_response.hpp Normal file
View file

@ -0,0 +1,37 @@
/* Copyright 2020, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <string_view>
#include <vector>
#include <utility>
namespace nap {
struct HttpResponse {
std::unique_ptr<char[]> raw;
std::vector<std::pair<std::string_view, std::string_view>> header_list;
std::string_view header;
std::string_view body;
std::string_view http_ver;
std::string_view code_desc;
unsigned int code;
};
} //namespace nap

148
src/nap/page_fetch.cpp Normal file
View file

@ -0,0 +1,148 @@
/* Copyright 2020-2022, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#include "private/page_fetch.hpp"
#include "private/http_header_parse.hpp"
#include <curl_easy.h>
#include <curl_pair.h>
#include <curl_ios.h>
#include <curl_header.h>
#include <sstream>
#include <algorithm>
#include <cassert>
#include <tuple>
#include <numeric>
namespace nap {
namespace {
bool is_https (std::string_view parUrl) {
const char protocol[] = "https://";
const size_t protocolLen = sizeof(protocol) / sizeof(protocol[0]) - 1;
if (parUrl.size() < protocolLen)
return false;
return std::equal(protocol, protocol + protocolLen, parUrl.begin());
}
std::tuple<std::unique_ptr<char[]>, std::string_view, std::string_view> make_raw_string (
const std::ostringstream& head,
const std::ostringstream& body,
std::size_t body_padding
) {
//TODO: use .view() with c++20
auto head_str = head.str();
auto body_str = body.str();
const std::size_t size = head_str.size() + 1 + body_str.size() + body_padding;
auto ret = std::make_unique<char[]>(size);
std::copy(head_str.begin(), head_str.end(), ret.get());
ret[head_str.size()] = '\n';
std::copy(body_str.begin(), body_str.end(), ret.get() + head_str.size() + 1);
std::fill(ret.get() + size - body_padding, ret.get() + size, ' ');
const char* const buff = ret.get();
return std::make_tuple(
std::move(ret),
std::string_view(buff, head_str.size()),
std::string_view(buff + head_str.size() + 1, body_str.size())
);
}
std::string join (const std::vector<std::string>& lines, std::string_view str) {
const std::size_t final_size = std::accumulate(
lines.cbegin(),
lines.cend(),
std::size_t{0},
[](std::size_t val, const std::string& s) -> std::size_t {
return val + s.size();
}
) + (std::min<std::size_t>(1, lines.size()) - 1) * str.size();
std::string retval;
retval.reserve(final_size);
if (not lines.empty())
retval += lines.front();
for (std::size_t z = 1; z < lines.size(); ++z) {
retval += str;
retval += lines[z];
}
return retval;
}
} //unnamed namespace
HttpResponse page_fetch (
const std::string& url,
const std::string& user_agent,
const PageFetchLines& headers,
const PageFetchLines& post,
std::size_t body_padding
) {
using curl::curl_pair;
std::ostringstream body_oss;
std::ostringstream header_oss;
curl::curl_ios<std::ostringstream> body(body_oss);
curl::curl_ios<std::ostringstream> header(header_oss);
curl::curl_easy easy;
easy.add<CURLOPT_WRITEFUNCTION>(header.get_function());
easy.add<CURLOPT_HEADERDATA>(header.get_stream());
easy.add<CURLOPT_WRITEDATA>(body.get_stream());
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_URL, url));
easy.add<CURLOPT_FOLLOWLOCATION>(1L);
if (is_https(url)) {
easy.add<CURLOPT_SSL_VERIFYPEER>(true);
easy.add<CURLOPT_SSL_VERIFYHOST>(true);
}
easy.add<CURLOPT_ACCEPT_ENCODING>("gzip");
easy.add<CURLOPT_HTTP_CONTENT_DECODING>(1L);
easy.add<CURLOPT_VERBOSE>(0L);
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_USERAGENT, user_agent));
curl::curl_header ch;
ch.add(headers.begin(), headers.end());
easy.add<CURLOPT_HTTPHEADER>(ch.get());
{
const std::string body = join(post, "\n");
easy.add<CURLOPT_POSTFIELDS>(body.c_str());
easy.add<CURLOPT_POSTFIELDSIZE>(body.size());
}
easy.perform();
HttpResponse resp;
resp.code = easy.get_info<CURLINFO_RESPONSE_CODE>().get();
{
auto [raw, head_tmp, body] = make_raw_string(header_oss, body_oss, body_padding);
resp.raw = std::move(raw);
resp.header = head_tmp;
resp.body = body;
}
auto parsed_header = header_parse(resp.header);
resp.header_list = std::move(parsed_header.fields);
assert(resp.code == parsed_header.code);
resp.http_ver = parsed_header.version;
resp.code_desc = parsed_header.message;
return resp;
}
} //namespace nap

View file

@ -0,0 +1,33 @@
/* Copyright 2020, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string_view>
#include <vector>
#include <utility>
namespace nap {
struct ParsedHeader {
std::string_view version;
unsigned int code;
std::string_view message;
std::vector<std::pair<std::string_view, std::string_view>> fields;
};
ParsedHeader header_parse (std::string_view text);
} //namespace nap

View file

@ -0,0 +1,38 @@
/* Copyright 2020-2022, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "nap/http_response.hpp"
#include <string_view>
#include <string>
#include <vector>
#include <cstddef>
namespace nap {
typedef std::vector<std::string> PageFetchLines;
HttpResponse page_fetch (
const std::string& url,
const std::string& user_agent,
const PageFetchLines& headers,
const PageFetchLines& post,
std::size_t body_padding
);
} //namespace nap

64
src/nap/quick_rest.cpp Normal file
View file

@ -0,0 +1,64 @@
/* Copyright 2020-2022, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#include "quick_rest.hpp"
#include "private/page_fetch.hpp"
#include <cassert>
namespace nap {
QuickRest::QuickRest (std::size_t body_padding) :
m_body_padding(body_padding)
{
}
void QuickRest::add_headers (std::initializer_list<HeaderPairView> headers) {
m_header_lines.reserve(m_header_lines.size() + headers.size());
for (const auto& entry : headers) {
std::string line;
line.reserve(entry.first.size() + 2 + entry.second.size());
line.append(entry.first);
line.append(": ");
line.append(entry.second);
m_header_lines.push_back(std::move(line));
}
}
void QuickRest::add_post (std::string_view name, std::string_view value) {
std::string new_line;
new_line.reserve(name.size() + 1 + value.size());
new_line = name;
new_line += '=';
new_line += value;
m_post_lines.push_back(std::move(new_line));
}
void QuickRest::set_user_agent (std::string&& name) {
m_user_agent = std::move(name);
}
HttpResponse QuickRest::fetch (std::string_view url) {
return page_fetch(
std::string(url),
m_user_agent,
m_header_lines,
m_post_lines,
m_body_padding
);
}
} //namespace nap

48
src/nap/quick_rest.hpp Normal file
View file

@ -0,0 +1,48 @@
/* Copyright 2020-2022, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "http_response.hpp"
#include <string_view>
#include <string>
#include <initializer_list>
#include <vector>
#include <utility>
#include <cstddef>
namespace nap {
class QuickRest {
public:
typedef std::pair<std::string_view, std::string_view> HeaderPairView;
explicit QuickRest(std::size_t body_padding=0);
void add_headers (std::initializer_list<HeaderPairView> headers);
void add_post (std::string_view name, std::string_view value);
void set_user_agent (std::string&& name);
HttpResponse fetch (std::string_view url);
private:
std::string m_user_agent;
std::vector<std::string> m_header_lines;
std::vector<std::string> m_post_lines;
std::size_t m_body_padding;
};
} //namespace nap