~voidraven/cryptomonitor

ref: e42397d8030afd37479f9623e4902ef62048e20a cryptomonitor/CryptoMonitorGog/http/coinmarketcap.cpp -rw-r--r-- 1.3 KiB
e42397d8voidraven first commit, again 1 year, 23 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <QDebug>
#include "coinmarketcap.h"
#include "httplib.h"
#include "nlohmann/json.hpp"

CoinMarketCap::CoinMarketCap(){}

void CoinMarketCap::Init(QVector<QString>* priceVec) const {
    httplib::Client hclient("sandbox-api.coinmarketcap.com");
    httplib::Headers headers = {
        { "X-CMC_PRO_API_KEY", "fb685c4f-81c1-408d-8e8a-59c439c52987" },
        { "Accept", "application/json"},
    };

    hclient.set_follow_location(true);
    std::shared_ptr<httplib::Response> resNow;
    std::shared_ptr<httplib::Response> resHist;
    try{
        resNow = hclient.Get("/v1/cryptocurrency/listings/latest?start=1&limit=10&convert=USD", headers);
        //resNow = hclient.Get("/v1/cryptocurrency/listings/historical?start=2&limit=7&convert=USD", headers);
    } catch(...) {
        qDebug() << "it asploded..";
    }
    using json = nlohmann::json;
    if (resNow) {
        json parsedJson = json::parse(resNow->body);
        auto coinsObj = parsedJson["data"];
        for (auto& coin : coinsObj) {
            float coinQuote = coin["quote"]["USD"]["price"];
            QString quoteString = QString::number(coinQuote);
            const QString quoteStringPlusDollar = "$" + quoteString;
            priceVec->append(quoteStringPlusDollar);
        }
    }
    else {
        qDebug() << "bad json response";
    }
}