-
Notifications
You must be signed in to change notification settings - Fork 10
/
firebaseexamples.cpp
111 lines (82 loc) · 2.42 KB
/
firebaseexamples.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "firebaseexamples.h"
#include <QUrl>
#include <firebase.h>
#include <QtDebug>
#include <QJsonObject>
#include <QJsonDocument>
FirebaseExamples::FirebaseExamples(int action
, const QString &firebaseURL
, const QString &authToken)
: fbUrl(firebaseURL), authToken(authToken)
{
switch (action)
{
case 1:
patch();
break;
case 2:
get();
break;
case 3:
useToken();
break;
case 4:
listen();
break;
};
}
void FirebaseExamples::patch()
{
QJsonObject jsonObj;
jsonObj["user"] = "hii";
jsonObj["password"] = "password";
jsonObj["email"] = "[email protected]";
QJsonDocument uploadDoc(jsonObj);
QString path="lll/users/fred/";
Firebase *firebaseSet = new Firebase(fbUrl, path);
firebaseSet->setValue(uploadDoc, "PATCH");
connect(firebaseSet,SIGNAL(eventResponseReady(QByteArray))
,this,SLOT(onResponseReady(QByteArray)));
}
void FirebaseExamples::get()
{
Firebase *firebaseGet=new Firebase(fbUrl, "lll/users/.json");
qDebug()<<"URL:" << firebaseGet->getPath();
firebaseGet->getValue();
connect(firebaseGet,SIGNAL(eventResponseReady(QByteArray))
,this,SLOT(onResponseReady(QByteArray)));
}
void FirebaseExamples::useToken()
{
QJsonObject jsonRule;
jsonRule[".read"] = true;
QJsonObject jsonRules;
jsonRules["rules"] = jsonRule;
QByteArray ba;
QJsonDocument uploadDoc(jsonRules);
ba = uploadDoc.toJson(QJsonDocument::Compact);
Firebase *firebaseSet = new Firebase(fbUrl, ".settings/rules");
qDebug()<<"URL:" << firebaseSet->getPath(authToken);
firebaseSet->setValue(uploadDoc, "PUT", authToken);
connect(firebaseSet,SIGNAL(eventResponseReady(QNetworkReply))
,this,SLOT(onResponseReady(QNetworkReply)));
}
void FirebaseExamples::listen()
{
Firebase *firebaseGet=new Firebase(fbUrl, "lll/.json");
firebaseGet->listenEvents();
connect(firebaseGet,SIGNAL(eventResponseReady(QByteArray))
,this,SLOT(onResponseReady(QByteArray)));
connect(firebaseGet,SIGNAL(eventDataChanged(QString))
,this,SLOT(onDataChanged(QString)));
}
void FirebaseExamples::onResponseReady(QByteArray data)
{
qDebug()<<"onResponseReady";
qDebug()<<data;
}
void FirebaseExamples::onDataChanged(QString data)
{
qDebug()<<"onDataChanged";
qDebug()<<data;
}