1
#include <api/client.h>
3
#include <core/net/error.h>
4
#include <core/net/http/client.h>
5
#include <core/net/http/content_type.h>
6
#include <core/net/http/response.h>
10
namespace http = core::net::http;
11
namespace net = core::net;
16
Client::Client(Config::Ptr config) :
17
config_(config), cancelled_(false) {
21
void Client::get(const net::Uri::Path &path,
22
const net::Uri::QueryParameters ¶meters, QJsonDocument &root) {
23
// Create a new HTTP client
24
auto client = http::make_client();
26
// Start building the request configuration
27
http::Request::Configuration configuration;
29
// Build the URI from its components
30
net::Uri uri = net::make_uri(config_->apiroot, path, parameters);
31
configuration.uri = client->uri_to_string(uri);
33
// Give out a user agent string
34
configuration.header.add("User-Agent", config_->user_agent);
36
// Build a HTTP request object from our configuration
37
auto request = client->head(configuration);
40
// Synchronously make the HTTP request
41
// We bind the cancellable callback to #progress_report
42
auto response = request->execute(
43
bind(&Client::progress_report, this, placeholders::_1));
45
// Check that we got a sensible HTTP status code
46
if (response.status != http::Status::ok) {
47
throw domain_error(response.body);
49
// Parse the JSON from the response
50
root = QJsonDocument::fromJson(response.body.c_str());
54
} catch (net::Error &) {
59
/// \brief Client::query_feeds
66
Client::Feeds Client::query_feeds(const string& query, std::string const& lang_sys) {
70
// Build a URI and get the contents
71
// The fist parameter forms the path part of the URI.
72
// The second parameter forms the CGI parameters.
73
get( { "feed", "find" }, {{ "q", query }, {"v", "1.0"}, { "hl", lang_sys }}, root);
74
// e.g. https://ajax.googleapis.com/ajax/services/feed/find?q=Ubuntu&v=1.0
78
QVariantMap variant = root.toVariant().toMap();
81
QVariantMap responseData = variant["responseData"].toMap();
83
for (const QVariant &i : responseData["entries"].toList()) {
84
QVariantMap item = i.toMap();
85
// Add a result to the list
86
result.feed.emplace_back(
87
Feed { item["title"].toString().toStdString(),
88
item["contentSnippet"].toString().toStdString(),
89
item["url"].toString().toStdString(),
90
item["link"].toString().toStdString(),
98
/// \brief Client::query_feedouts
103
Client::Feedouts Client::query_feedouts(const string& linky, std::string const& lang_sys) {
107
get( { "feed", "load" }, {{ "q", linky }, {"v", "1.0"}, { "hl", lang_sys }}, root);
108
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
113
QVariantMap variant = root.toVariant().toMap();
116
QVariantMap responseData = variant["responseData"].toMap();
118
QVariantMap feed = responseData["feed"].toMap();
119
// Iterate through the data
120
for (const QVariant &i : feed["entries"].toList()) {
121
QVariantMap item = i.toMap();
122
// Add a result to the list
123
result.feedout.emplace_back(
124
Feedout { item["title"].toString().toStdString(),
125
item["link"].toString().toStdString(),
126
item["publishedDate"].toString().toStdString(),
127
item["author"].toString().toStdString(),
128
item["content"].toString().toStdString(),
141
/// \brief Client::add_loads
146
Client::Omgs Client::add_omgs(const string& add, std::string const& lang_sys) {
149
// Build a URI and get the contents
150
// The fist parameter forms the path part of the URI.
151
// The second parameter forms the CGI parameters.
152
get( { "feed", "load" }, {{ "q", add }, {"v", "1.0"}, { "hl", lang_sys }}, root);
153
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
157
QVariantMap variant = root.toVariant().toMap();
160
QVariantMap responseData = variant["responseData"].toMap();
162
QVariantMap feed = responseData["feed"].toMap();
163
// Iterate through the data
164
for (const QVariant &i : feed["entries"].toList()) {
165
QVariantMap item = i.toMap();
166
// Add a result to the list
167
result.omg.emplace_back(
168
Omg { item["title"].toString().toStdString(),
169
item["link"].toString().toStdString(),
170
item["publishedDate"].toString().toStdString(),
171
item["author"].toString().toStdString(),
172
item["content"].toString().toStdString(),
182
/// \brief Client::add_loads
187
Client::Loads Client::add_loads(const string& add, std::string const& lang_sys) {
190
// Build a URI and get the contents
191
// The fist parameter forms the path part of the URI.
192
// The second parameter forms the CGI parameters.
193
get( { "feed", "load" }, {{ "q", add }, {"v", "1.0"}, { "hl", lang_sys }}, root);
194
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
198
QVariantMap variant = root.toVariant().toMap();
201
QVariantMap responseData = variant["responseData"].toMap();
203
QVariantMap feed = responseData["feed"].toMap();
204
// Iterate through the data
205
for (const QVariant &i : feed["entries"].toList()) {
206
QVariantMap item = i.toMap();
207
// Add a result to the list
208
result.load.emplace_back(
209
Load { item["title"].toString().toStdString(),
210
item["link"].toString().toStdString(),
211
item["publishedDate"].toString().toStdString(),
212
item["author"].toString().toStdString(),
213
item["content"].toString().toStdString()
222
/// \brief Client::add_buntus
227
Client::Buntus Client::add_buntus(const string& add, std::string const& lang_sys) {
230
// Build a URI and get the contents
231
// The fist parameter forms the path part of the URI.
232
// The second parameter forms the CGI parameters.
233
get( { "feed", "load" }, {{ "q", add }, {"v", "1.0"}, { "hl", lang_sys }}, root);
234
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
238
QVariantMap variant = root.toVariant().toMap();
241
QVariantMap responseData = variant["responseData"].toMap();
243
QVariantMap feed = responseData["feed"].toMap();
244
// Iterate through the data
245
for (const QVariant &i : feed["entries"].toList()) {
246
QVariantMap item = i.toMap();
247
// Add a result to the list
248
result.buntu.emplace_back(
249
Buntu { item["title"].toString().toStdString(),
250
item["link"].toString().toStdString(),
251
item["author"].toString().toStdString(),
252
item["publishedDate"].toString().toStdString(),
253
item["content"].toString().toStdString(),
262
/// \brief Client::add_launchs
267
Client::Plans Client::add_plans(const string& add, std::string const& lang_sys) {
270
// Build a URI and get the contents
271
// The fist parameter forms the path part of the URI.
272
// The second parameter forms the CGI parameters.
273
get( { "feed", "load" }, {{ "q", add }, {"v", "1.0"}, { "hl", lang_sys }}, root);
274
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
278
QVariantMap variant = root.toVariant().toMap();
281
QVariantMap responseData = variant["responseData"].toMap();
283
QVariantMap feed = responseData["feed"].toMap();
284
// Iterate through the data
285
for (const QVariant &i : feed["entries"].toList()) {
286
QVariantMap item = i.toMap();
287
// Add a result to the list
288
result.plan.emplace_back(
289
Plan { item["title"].toString().toStdString(),
290
item["link"].toString().toStdString(),
291
item["author"].toString().toStdString(),
292
item["publishedDate"].toString().toStdString(),
293
item["content"].toString().toStdString(),
302
/// \brief Client::add_launchs
307
Client::Launchs Client::add_launchs(const string& add, std::string const& lang_sys) {
310
// Build a URI and get the contents
311
// The fist parameter forms the path part of the URI.
312
// The second parameter forms the CGI parameters.
313
get( { "feed", "load" }, {{ "q", add }, {"v", "1.0"}, { "hl", lang_sys }}, root);
314
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
318
QVariantMap variant = root.toVariant().toMap();
321
QVariantMap responseData = variant["responseData"].toMap();
323
QVariantMap feed = responseData["feed"].toMap();
324
// Iterate through the data
325
for (const QVariant &i : feed["entries"].toList()) {
326
QVariantMap item = i.toMap();
327
// Add a result to the list
328
result.launch.emplace_back(
329
Launch { item["title"].toString().toStdString(),
330
item["link"].toString().toStdString(),
331
item["author"].toString().toStdString(),
332
item["publishedDate"].toString().toStdString(),
333
item["content"].toString().toStdString(),
342
/// \brief Client::add_customs
347
Client::Customs Client::add_customs(std::string const& cust_sys, std::string const& lang_sys) {
350
// Build a URI and get the contents
351
// The fist parameter forms the path part of the URI.
352
// The second parameter forms the CGI parameters.
353
get( { "feed", "load" }, {{ "q", "http://" + cust_sys }, {"v", "1.0"}, { "hl", lang_sys }}, root);
354
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
358
QVariantMap variant = root.toVariant().toMap();
361
QVariantMap responseData = variant["responseData"].toMap();
363
QVariantMap feed = responseData["feed"].toMap();
364
// Iterate through the data
365
for (const QVariant &i : feed["entries"].toList()) {
366
QVariantMap item = i.toMap();
367
// QVariantMap images = item["image"].toMap();
368
// Add a result to the list
369
result.custom.emplace_back(
370
Custom { item["title"].toString().toStdString(),
371
item["link"].toString().toStdString(),
372
item["author"].toString().toStdString(),
373
item["publishedDate"].toString().toStdString(),
374
item["content"].toString().toStdString(),
383
/// \brief Client::add_customs
388
Client::Custom1s Client::add_custom1s(std::string const& cust_sys1, std::string const& lang_sys) {
391
// Build a URI and get the contents
392
// The fist parameter forms the path part of the URI.
393
// The second parameter forms the CGI parameters.
394
get( { "feed", "load" }, {{ "q", "http://" + cust_sys1 }, {"v", "1.0"}, { "hl", lang_sys }}, root);
395
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
399
QVariantMap variant = root.toVariant().toMap();
402
QVariantMap responseData = variant["responseData"].toMap();
404
QVariantMap feed = responseData["feed"].toMap();
405
// Iterate through the data
406
for (const QVariant &i : feed["entries"].toList()) {
407
QVariantMap item = i.toMap();
408
// QVariantMap images = item["image"].toMap();
409
// Add a result to the list
410
result.custom1.emplace_back(
411
Custom1 { item["title"].toString().toStdString(),
412
item["link"].toString().toStdString(),
413
item["author"].toString().toStdString(),
414
item["publishedDate"].toString().toStdString(),
415
item["content"].toString().toStdString(),
425
/// \brief Client::add_customs
430
Client::Custom2s Client::add_custom2s(std::string const& cust_sys2, std::string const& lang_sys) {
433
// Build a URI and get the contents
434
// The fist parameter forms the path part of the URI.
435
// The second parameter forms the CGI parameters.
436
get( { "feed", "load" }, {{ "q", "http://" + cust_sys2 }, {"v", "1.0"}, { "hl", lang_sys }}, root);
437
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
441
QVariantMap variant = root.toVariant().toMap();
444
QVariantMap responseData = variant["responseData"].toMap();
446
QVariantMap feed = responseData["feed"].toMap();
447
// Iterate through the data
448
for (const QVariant &i : feed["entries"].toList()) {
449
QVariantMap item = i.toMap();
450
// QVariantMap images = item["image"].toMap();
451
// Add a result to the list
452
result.custom2.emplace_back(
453
Custom2 { item["title"].toString().toStdString(),
454
item["link"].toString().toStdString(),
455
item["author"].toString().toStdString(),
456
item["publishedDate"].toString().toStdString(),
457
item["content"].toString().toStdString(),
466
/// \brief Client::add_customs
471
Client::Custom3s Client::add_custom3s(std::string const& cust_sys3, std::string const& lang_sys) {
474
// Build a URI and get the contents
475
// The fist parameter forms the path part of the URI.
476
// The second parameter forms the CGI parameters.
477
get( { "feed", "load" }, {{ "q", "http://" + cust_sys3 }, {"v", "1.0"}, { "hl", lang_sys }}, root);
478
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
482
QVariantMap variant = root.toVariant().toMap();
485
QVariantMap responseData = variant["responseData"].toMap();
487
QVariantMap feed = responseData["feed"].toMap();
488
// Iterate through the data
489
for (const QVariant &i : feed["entries"].toList()) {
490
QVariantMap item = i.toMap();
491
// QVariantMap images = item["image"].toMap();
492
// Add a result to the list
493
result.custom3.emplace_back(
494
Custom3 { item["title"].toString().toStdString(),
495
item["link"].toString().toStdString(),
496
item["author"].toString().toStdString(),
497
item["publishedDate"].toString().toStdString(),
498
item["content"].toString().toStdString(),
507
/// \brief Client::add_customs
512
Client::Custom4s Client::add_custom4s(std::string const& cust_sys4, std::string const& lang_sys) {
515
// Build a URI and get the contents
516
// The fist parameter forms the path part of the URI.
517
// The second parameter forms the CGI parameters.
518
get( { "feed", "load" }, {{ "q", "http://" + cust_sys4 }, {"v", "1.0"}, { "hl", lang_sys }}, root);
519
// e.g. https://ajax.googleapis.com/ajax/services/feed/load?q=Ubuntu&v=1.0
523
QVariantMap variant = root.toVariant().toMap();
526
QVariantMap responseData = variant["responseData"].toMap();
528
QVariantMap feed = responseData["feed"].toMap();
529
// Iterate through the data
530
for (const QVariant &i : feed["entries"].toList()) {
531
QVariantMap item = i.toMap();
532
// QVariantMap images = item["image"].toMap();
533
// Add a result to the list
534
result.custom4.emplace_back(
535
Custom4 { item["title"].toString().toStdString(),
536
item["link"].toString().toStdString(),
537
item["author"].toString().toStdString(),
538
item["publishedDate"].toString().toStdString(),
539
item["content"].toString().toStdString(),
547
/// \brief Client::progress_report
550
http::Request::Progress::Next Client::progress_report(
551
const http::Request::Progress&) {
554
http::Request::Progress::Next::abort_operation :
555
http::Request::Progress::Next::continue_operation;
558
void Client::cancel() {
562
Config::Ptr Client::config() {