Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qt5 client library #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ We have libraries for the following languages:
* [Ruby](#ruby)
* [Rust](#rust)
* [Haskell](#haskell)
* [Qt5] (#qt5)

Alternatively, you can use Botan API via [plain HTTP calls](#http).

Expand Down Expand Up @@ -203,6 +204,19 @@ data AnyMessage = AnyMessage
} deriving (Show, Generic, ToJSON)
```


## <a name="qt5"></a>Qt5 example

```qt5
QVariantMap params;
params["int_metric"] = 1234;
params["double_metric"] = 12.34;
params["string_metric"] = "1234";
params["bool_metric"] = true;
//call asynchronously "track" request
QBotanioTrackApi::newInstance()->track("SOME_PRIVATE_KEY", 1234, "TEST_EVENT", params);
```

## <a name="http"></a>HTTP API
### <a name="http_track"></a>Track message
The base url is: https://api.botan.io/track
Expand Down
39 changes: 39 additions & 0 deletions qt/example/track/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <QCoreApplication>
#include <QDebug>

//QBotanioAPI
#include <qbotaniotrackapi.h>

//Insert here your "botan.io" application key
static const QString BOTANIO_APP_KEY = QStringLiteral("SOME_PRIVATE_KEY");
static const int UNIQUE_SET_ID = 1234;//e.g. telegram user id
static const QString EVENT_NAME = QStringLiteral("TEST_EVENT");

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

//create a new "Botanio track API" instance on the heap
QBotanioTrackApi* pBotaino = QBotanioTrackApi::newInstance();

//prints result after "track" request is finished
QObject::connect(pBotaino,&QBotanioTrackApi::signalFinished,[](bool bResult, const QString& sResultDetails){
Q_UNUSED(bResult);qDebug()<<"result" << sResultDetails;
});

//close application after "track" request is finished
QObject::connect(pBotaino,&QBotanioTrackApi::signalFinished,&a,&QCoreApplication::quit);

//some metrics
QVariantMap params;
params["int_metric"] = 1234;
params["double_metric"] = 12.34;
params["string_metric"] = "1234";
params["bool_metric"] = true;
//call asynchronously "track" request
pBotaino->track(BOTANIO_APP_KEY, UNIQUE_SET_ID, EVENT_NAME, params);
//client don't neede to delete the pBotaino instance, because it is self destructed after HTTP request is finished
//delete pBotaino;

return a.exec();
}
41 changes: 41 additions & 0 deletions qt/example/track/qtbotaniotrack.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
QT += core network
QT -= gui
CONFIG += c++11
TARGET = qtbotaniotrack
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app

SOURCES += main.cpp

INCLUDEPATH += ./../../qbotanio

QMAKE_CLEAN += Makefile
QMAKE_CLEAN += Makefile.$${TARGET}
QMAKE_CLEAN += Makefile.$${TARGET}.Release
QMAKE_CLEAN += Makefile.$${TARGET}.Debug
QMAKE_CLEAN += Makefile.Release
QMAKE_CLEAN += Makefile.Debug

CONFIG(release, debug|release) {
DESTDIR = ./Release
MOC_DIR += $${DESTDIR}/.moc
OBJECTS_DIR += $${DESTDIR}/.obj
UI_DIR += $${DESTDIR}/.ui
RCC_DIR += $${DESTDIR}/.rcc
LIBS += -L../../qbotanio/Release -lqbotanio
}
else{
DESTDIR = ./Debug
MOC_DIR += $${DESTDIR}/.moc
OBJECTS_DIR += $${DESTDIR}/.obj
UI_DIR += $${DESTDIR}/.ui
RCC_DIR += $${DESTDIR}/.rcc
TARGET = $$join(TARGET,,,d)
LIBS += -L../../qbotanio/Debug -lqbotaniod
}
unix{
QMAKE_CXXFLAGS +=-std=c++11
target.path = /usr/bin
INSTALLS += target
}
32 changes: 32 additions & 0 deletions qt/example/urlshortener/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <QCoreApplication>
#include <QDebug>

//QBotanioAPI
#include <qbotaniourlshortenerapi.h>

//Insert here your "botan.io" application key
static const QString BOTANIO_APP_KEY = QStringLiteral("YOUR_PRIVATE_KEY");
static const int UNIQUE_SET_ID = 1234;//Telegram user id
static const QString URL = QStringLiteral("https://github.com/botanio/sdk");

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

//create a new "Botanio URL Shortener API" instance on the heap
QBotanioUrlShortenerAPI* pBotaino = QBotanioUrlShortenerAPI::newInstance();

//prints result after "shorten URL" request is finished
QObject::connect(pBotaino,&QBotanioUrlShortenerAPI::signalFinished,[](bool bResult, const QString& sShortenURL,const QString& sResultDetails){
qDebug()<<"result" << (bResult? sShortenURL : sResultDetails);
});

//close application after "shorten URL" request is finished
QObject::connect(pBotaino,&QBotanioUrlShortenerAPI::signalFinished,&a,&QCoreApplication::quit);

//call asynchronously "shortenURL" request
pBotaino->shortenUrlAsynch(BOTANIO_APP_KEY, UNIQUE_SET_ID, URL);
//client don't need to delete the pBotaino instance, because it is self destructed after HTTP request is finished
//delete pBotaino;
return a.exec();
}
41 changes: 41 additions & 0 deletions qt/example/urlshortener/qtbotaniourlshortener.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
QT += core network
QT -= gui
CONFIG += c++11
TARGET = qtbotaniourlshortener
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app

SOURCES += main.cpp

INCLUDEPATH += ./../../qbotanio

QMAKE_CLEAN += Makefile
QMAKE_CLEAN += Makefile.$${TARGET}
QMAKE_CLEAN += Makefile.$${TARGET}.Release
QMAKE_CLEAN += Makefile.$${TARGET}.Debug
QMAKE_CLEAN += Makefile.Release
QMAKE_CLEAN += Makefile.Debug

CONFIG(release, debug|release) {
DESTDIR = ./Release
MOC_DIR += $${DESTDIR}/.moc
OBJECTS_DIR += $${DESTDIR}/.obj
UI_DIR += $${DESTDIR}/.ui
RCC_DIR += $${DESTDIR}/.rcc
LIBS += -L../../qbotanio/Release -lqbotanio
}
else{
DESTDIR = ./Debug
MOC_DIR += $${DESTDIR}/.moc
OBJECTS_DIR += $${DESTDIR}/.obj
UI_DIR += $${DESTDIR}/.ui
RCC_DIR += $${DESTDIR}/.rcc
TARGET = $$join(TARGET,,,d)
LIBS += -L../../qbotanio/Debug -lqbotaniod
}
unix{
QMAKE_CXXFLAGS +=-std=c++11
target.path = /usr/bin
INSTALLS += target
}
139 changes: 139 additions & 0 deletions qt/qbotanio/private/qbotaniotrackapi_p.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include "stdafx.h"

//Botanio API base URL
static const QString BOTANIO_BASE_URL = QStringLiteral("https://api.botan.io/track");
//Botanio base URL fragments names
static const QString BOTANIO_BASE_TOKEN_FRAGMENT_NAME = QStringLiteral("token");
static const QString BOTANIO_BASE_UID_FRAGMENT_NAME = QStringLiteral("uid");
static const QString BOTANIO_BASE_NAME_FRAGMENT_NAME = QStringLiteral("name");

//------------------------------------------------------------------------------------
//@author max walter @date 09.12.2016
QBotanioTrackApiPrivate::QBotanioTrackApiPrivate(QObject *parent)
: QObject(parent)
{
m_TimeoutTimer.setSingleShot(true);

connect(&m_TimeoutTimer , &QTimer::timeout, [this](){
if(m_pReply && m_pReply->isRunning()){
m_pReply->close();
}
});
}
//------------------------------------------------------------------------------------
//@author max walter @date 06.12.2016
QBotanioTrackApiPrivate::~QBotanioTrackApiPrivate()
{

}
//------------------------------------------------------------------------------------
//@author max walter @date 06.12.2016
bool QBotanioTrackApiPrivate::track(const QString& sApiKey, int UID, const QString& sEventName, const QJsonDocument& params, int timeoutMs)
{
bool bResult = false;
if (!m_pReply)
{
//setup URL
QString sFullUrl = QString("%1?%2=%3&%4=%5&%6=%7")
.arg(BOTANIO_BASE_URL)
.arg(BOTANIO_BASE_TOKEN_FRAGMENT_NAME)
.arg(sApiKey)
.arg(BOTANIO_BASE_UID_FRAGMENT_NAME)
.arg(QString::number(UID))
.arg(BOTANIO_BASE_NAME_FRAGMENT_NAME)
.arg(sEventName);

//pack JSON params
QByteArray ba = params.toJson();
QNetworkRequest request(sFullUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(QStringLiteral("application/json")));
request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(ba.size()));

//POST request
m_pReply = m_NAM.post(request, ba);
if (m_pReply)
{
connect(m_pReply, SIGNAL(finished()), this, SLOT(slotRequestFinished()));
bResult = true;
//timeout
if(timeoutMs > 0)
m_TimeoutTimer.start(timeoutMs);
}
}
return bResult;
}
//------------------------------------------------------------------------------------
//@author max walter @date 06.12.2016
bool QBotanioTrackApiPrivate::track(const QString &sApiKey, int UID, const QString &sEventName, int timeoutMs)
{
return track(sApiKey, UID, sEventName,QJsonDocument(),timeoutMs);
}
//------------------------------------------------------------------------------------
//@author max walter @date 06.12.2016
bool QBotanioTrackApiPrivate::track(const QString &sApiKey, int UID, const QString &sEventName, const QVariantMap &params, int timeoutMs)
{
return track(sApiKey, UID, sEventName,QJsonDocument(QJsonObject::fromVariantMap(params)),timeoutMs);
}

//------------------------------------------------------------------------------------
//@author max walter @date 06.12.2016
bool QBotanioTrackApiPrivate::parseResult(QByteArray jsonResult, QString& sResult)
{
bool bResult = false;
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonResult);
if (!jsonDoc.isNull() && jsonDoc.isObject())
{
QJsonObject obj = jsonDoc.object();
if (obj.contains("status") )
{
//Status available?
QString sStatus = obj.value("status").toString();
if(sStatus == QStringLiteral("accepted"))
{
sResult = sStatus;
bResult = true;
}
else
if(sStatus == QStringLiteral("failed") || sStatus == QStringLiteral("bad request")){
sResult = sStatus;
bResult = false;
}

//Info availabvle?
if(obj.contains("info"))
sResult.append(QStringLiteral(" %1").arg(obj.value("info").toString()));
}
else
sResult = tr("Invalid json structure");
}
else
sResult = tr("Invalid json document");

return bResult;
}
//------------------------------------------------------------------------------------
//@author max walter @date 06.12.2016
void QBotanioTrackApiPrivate::slotRequestFinished()
{
//stop timeout timer
m_TimeoutTimer.stop();
if (m_pReply)
{
bool bResult = false;
QNetworkReply::NetworkError error = m_pReply->error();
QString sResultDetails;
if (error == QNetworkReply::NoError)
{
QByteArray baResult = m_pReply->readAll();
bResult = parseResult(baResult, sResultDetails);
}
else
sResultDetails = tr("http request failed! %1").arg(m_pReply->errorString());

m_pReply->deleteLater();
m_pReply = nullptr;
//inform client
emit signalFinished(bResult, sResultDetails);
deleteLater();
}
}
42 changes: 42 additions & 0 deletions qt/qbotanio/private/qbotaniotrackapi_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef QBOTANIOTRACKAPIPRIVATE_H
#define QBOTANIOTRACKAPIPRIVATE_H

#include <QObject>
#include <QJsonDocument>
#include <QVariantMap>
#include <QByteArray>
#include <QNetworkAccessManager>
#include <QTimer>
#include <QNetworkReply>
#include <QPointer>

class QBotanioTrackApiPrivate : public QObject
{
Q_OBJECT
public:
QBotanioTrackApiPrivate(QObject *parent);
~QBotanioTrackApiPrivate();
public://method
//track event
bool track(const QString& sApiKey, int UID, const QString& sEventName,int timeoutMs = 30000);
//track event with params as varaint map
bool track(const QString& sApiKey, int UID, const QString& sEventName,const QVariantMap& params,int timeoutMs = 30000);
//track event with params as json document
bool track(const QString& sApiKey, int UID, const QString& sEventName,const QJsonDocument& params,int timeoutMs = 30000);
signals:
//Informs clients about request is finished
void signalFinished(bool bResult, const QString& sResultDetails);
protected:
bool parseResult(QByteArray jsonResult, QString &sResult);
protected slots:
void slotRequestFinished();
private:
//Network access manager
QNetworkAccessManager m_NAM;
//reply for current request
QPointer<QNetworkReply> m_pReply = nullptr;
//timeout timer
QTimer m_TimeoutTimer;

};
#endif // QBOTANIOTRACKAPIPRIVATE_H
Loading