added api key entry at thing setup
parent
ac6dcf0765
commit
b4ef247875
|
|
@ -38,18 +38,61 @@ IntegrationPluginCoinMarketCap::IntegrationPluginCoinMarketCap()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void IntegrationPluginCoinMarketCap::startPairing(ThingPairingInfo *info)
|
||||||
|
{
|
||||||
|
NetworkAccessManager *network = hardwareManager()->networkManager();
|
||||||
|
QNetworkReply *reply = network->get(QNetworkRequest(QUrl("https://pro-api.coinmarketcap.com")));
|
||||||
|
connect(reply, &QNetworkReply::finished, this, [reply, info] {
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
if (reply->error() == QNetworkReply::NetworkError::HostNotFoundError) {
|
||||||
|
info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("CoinMarketCap server is not reachable."));
|
||||||
|
} else {
|
||||||
|
info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter your API token."));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginCoinMarketCap::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &secret)
|
||||||
|
{
|
||||||
|
Q_UNUSED(username)
|
||||||
|
|
||||||
|
QNetworkRequest request(QUrl("https://pro-api.coinmarketcap.com/v1/key/info"));
|
||||||
|
request.setRawHeader("X-CMC_PRO_API_KEY", secret.toUtf8());
|
||||||
|
request.setRawHeader("Accept", "application/json");
|
||||||
|
|
||||||
|
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
||||||
|
connect(reply, &QNetworkReply::finished, info, [this, reply, info, secret](){
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
|
||||||
|
// check HTTP status code
|
||||||
|
if (status != 200) {
|
||||||
|
//: Error setting up device with invalid token
|
||||||
|
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("This token is not valid."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pluginStorage()->beginGroup(info->thingId().toString());
|
||||||
|
pluginStorage()->setValue("apiKey", secret);
|
||||||
|
pluginStorage()->endGroup();
|
||||||
|
info->finish(Thing::ThingErrorNoError);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void IntegrationPluginCoinMarketCap::setupThing(ThingSetupInfo *info)
|
void IntegrationPluginCoinMarketCap::setupThing(ThingSetupInfo *info)
|
||||||
{
|
{
|
||||||
Thing *thing = info->thing();
|
Thing *thing = info->thing();
|
||||||
|
|
||||||
if (thing->thingClassId() == currentPricesThingClassId) {
|
if (thing->thingClassId() == currentPricesThingClassId) {
|
||||||
|
|
||||||
|
pluginStorage()->beginGroup(info->thing()->id().toString());
|
||||||
|
QByteArray apiKey = pluginStorage()->value("apiKey").toByteArray();
|
||||||
|
pluginStorage()->endGroup();
|
||||||
getPriceCall(thing);
|
getPriceCall(thing);
|
||||||
|
m_apiKeys.insert(thing->id(), apiKey);
|
||||||
if(!m_pluginTimer) {
|
|
||||||
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
|
|
||||||
connect(m_pluginTimer, &PluginTimer::timeout, this, &IntegrationPluginCoinMarketCap::onPluginTimer);
|
|
||||||
}
|
|
||||||
|
|
||||||
info->finish(Thing::ThingErrorNoError);
|
info->finish(Thing::ThingErrorNoError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -59,10 +102,14 @@ void IntegrationPluginCoinMarketCap::setupThing(ThingSetupInfo *info)
|
||||||
|
|
||||||
void IntegrationPluginCoinMarketCap::thingRemoved(Thing *thing)
|
void IntegrationPluginCoinMarketCap::thingRemoved(Thing *thing)
|
||||||
{
|
{
|
||||||
while (m_httpRequests.values().contains(thing)) {
|
if (thing->thingClassId() == currentPricesThingClassId) {
|
||||||
QNetworkReply *reply = m_httpRequests.key(thing);
|
m_apiKeys.remove(thing->id());
|
||||||
m_httpRequests.remove(reply);
|
|
||||||
reply->deleteLater();
|
while (m_httpRequests.values().contains(thing)) {
|
||||||
|
QNetworkReply *reply = m_httpRequests.key(thing);
|
||||||
|
m_httpRequests.remove(reply);
|
||||||
|
reply->deleteLater();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (myThings().empty()) {
|
if (myThings().empty()) {
|
||||||
|
|
@ -71,6 +118,16 @@ void IntegrationPluginCoinMarketCap::thingRemoved(Thing *thing)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginCoinMarketCap::postSetupThing(Thing *thing)
|
||||||
|
{
|
||||||
|
Q_UNUSED(thing)
|
||||||
|
|
||||||
|
if(!m_pluginTimer) {
|
||||||
|
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
|
||||||
|
connect(m_pluginTimer, &PluginTimer::timeout, this, &IntegrationPluginCoinMarketCap::onPluginTimer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void IntegrationPluginCoinMarketCap::onPluginTimer()
|
void IntegrationPluginCoinMarketCap::onPluginTimer()
|
||||||
{
|
{
|
||||||
foreach (Thing *thing, myThings()) {
|
foreach (Thing *thing, myThings()) {
|
||||||
|
|
@ -168,9 +225,11 @@ void IntegrationPluginCoinMarketCap::onPriceCallFinished()
|
||||||
void IntegrationPluginCoinMarketCap::getPriceCall(Thing *thing)
|
void IntegrationPluginCoinMarketCap::getPriceCall(Thing *thing)
|
||||||
{
|
{
|
||||||
QUrl url;
|
QUrl url;
|
||||||
url.setUrl(QString("https://api.coinmarketcap.com/v1/ticker/?convert=%1&limit=30").arg(QString(thing->paramValue(currentPricesThingFiatParamTypeId).toString()).toLower()));
|
url.setUrl(QString("https://pro-api.coinmarketcap.com/v1/ticker/?convert=%1&limit=30").arg(QString(thing->paramValue(currentPricesThingFiatParamTypeId).toString()).toLower()));
|
||||||
QNetworkRequest request;
|
QNetworkRequest request;
|
||||||
request.setUrl(url);
|
request.setUrl(url);
|
||||||
|
request.setRawHeader("X-CMC_PRO_API_KEY", m_apiKeys.value(thing->id()));
|
||||||
|
request.setRawHeader("Accept", "application/json");
|
||||||
request.setRawHeader("User-Agent", "nymea 1.0");
|
request.setRawHeader("User-Agent", "nymea 1.0");
|
||||||
|
|
||||||
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
||||||
|
|
|
||||||
|
|
@ -47,11 +47,15 @@ class IntegrationPluginCoinMarketCap : public IntegrationPlugin
|
||||||
public:
|
public:
|
||||||
explicit IntegrationPluginCoinMarketCap();
|
explicit IntegrationPluginCoinMarketCap();
|
||||||
|
|
||||||
|
void startPairing(ThingPairingInfo *info) override;
|
||||||
|
void confirmPairing(ThingPairingInfo *info, const QString &username, const QString &secret) override;
|
||||||
void setupThing(ThingSetupInfo *info) override;
|
void setupThing(ThingSetupInfo *info) override;
|
||||||
void thingRemoved(Thing *thing) override;
|
void thingRemoved(Thing *thing) override;
|
||||||
|
void postSetupThing(Thing *thing) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PluginTimer *m_pluginTimer = nullptr;
|
PluginTimer *m_pluginTimer = nullptr;
|
||||||
|
QHash<ThingId, QByteArray> m_apiKeys;
|
||||||
|
|
||||||
QHash<QUrl, Thing *> m_priceRequests;
|
QHash<QUrl, Thing *> m_priceRequests;
|
||||||
QHash<QNetworkReply *, Thing *> m_httpRequests;
|
QHash<QNetworkReply *, Thing *> m_httpRequests;
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
"displayName": "Crypto Prices",
|
"displayName": "Crypto Prices",
|
||||||
"createMethods": ["user"],
|
"createMethods": ["user"],
|
||||||
"interfaces": ["connectable"],
|
"interfaces": ["connectable"],
|
||||||
|
"setupMethod": "displaypin",
|
||||||
"paramTypes": [
|
"paramTypes": [
|
||||||
{
|
{
|
||||||
"id": "92747d75-d18a-4915-bd48-0edd5cc5f19a",
|
"id": "92747d75-d18a-4915-bd48-0edd5cc5f19a",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue