changing request to have a callback function passed

This commit is contained in:
Wayne Chen 2019-01-09 22:50:22 -08:00
parent 6bc1164d58
commit ed39b43d3b
5 changed files with 56 additions and 91 deletions

View file

@ -130,9 +130,8 @@ void LoginDialog::login(const QString& username, const QString& password) const
void LoginDialog::loginThroughOculus() {
qDebug() << "Attempting to login through Oculus";
if (auto oculusPlatformPlugin = PluginManager::getInstance()->getOculusPlatformPlugin()) {
oculusPlatformPlugin->requestNonceAndUserID(LoginState::LOGIN);
connect(oculusPlatformPlugin.get(), &OculusPlatformPlugin::loginReady, this, [&] (QString nonce, QString userID) {
onLoginThroughOculusReady(nonce, userID);
oculusPlatformPlugin->requestNonceAndUserID([this] (QString nonce, QString userID) {
DependencyManager::get<AccountManager>()->requestAccessTokenWithOculus(nonce, userID);
});
}
}
@ -140,17 +139,58 @@ void LoginDialog::loginThroughOculus() {
void LoginDialog::linkOculus() {
qDebug() << "Attempting to link Oculus account";
if (auto oculusPlatformPlugin = PluginManager::getInstance()->getOculusPlatformPlugin()) {
oculusPlatformPlugin->requestNonceAndUserID(LoginState::LINK_ACCOUNT);
oculusPlatformPlugin->requestNonceAndUserID([this] (QString nonce, QString userID) {
if (nonce.isEmpty() || userID.isEmpty()) {
emit handleLoginFailed();
return;
}
JSONCallbackParameters callbackParams;
callbackParams.callbackReceiver = this;
callbackParams.jsonCallbackMethod = "linkCompleted";
callbackParams.errorCallbackMethod = "linkFailed";
const QString LINK_OCULUS_PATH = "api/v1/user/oculus/link";
QJsonObject payload;
payload.insert("oculus_nonce", QJsonValue::fromVariant(QVariant(nonce)));
payload.insert("oculus_user_id", QJsonValue::fromVariant(QVariant(userID)));
auto accountManager = DependencyManager::get<AccountManager>();
accountManager->sendRequest(LINK_OCULUS_PATH, AccountManagerAuth::Required,
QNetworkAccessManager::PostOperation, callbackParams,
QJsonDocument(payload).toJson());
});
}
}
void LoginDialog::createAccountFromOculus(QString username) {
qDebug() << "Attempting to create account from Oculus info";
if (auto oculusPlatformPlugin = PluginManager::getInstance()->getOculusPlatformPlugin()) {
oculusPlatformPlugin->requestNonceAndUserID(LoginState::CREATE_ACCOUNT);
connect(oculusPlatformPlugin.get(), &OculusPlatformPlugin::createAccountReady, this, [&] (QString nonce, QString userID) {
onCreateAccountThroughOculusReady(nonce, userID, username);
});
oculusPlatformPlugin->requestNonceAndUserID([this, username] (QString nonce, QString userID) {
if (nonce.isEmpty() || userID.isEmpty()) {
emit handleLoginFailed();
return;
}
JSONCallbackParameters callbackParams;
callbackParams.callbackReceiver = this;
callbackParams.jsonCallbackMethod = "createCompleted";
callbackParams.errorCallbackMethod = "createFailed";
const QString CREATE_ACCOUNT_FROM_OCULUS_PATH = "api/v1/user/oculus/create";
QJsonObject payload;
payload.insert("oculus_nonce", QJsonValue::fromVariant(QVariant(nonce)));
payload.insert("oculus_user_id", QJsonValue::fromVariant(QVariant(userID)));
if (!username.isEmpty()) {
payload.insert("username", QJsonValue::fromVariant(QVariant(username)));
}
auto accountManager = DependencyManager::get<AccountManager>();
accountManager->sendRequest(CREATE_ACCOUNT_FROM_OCULUS_PATH, AccountManagerAuth::None,
QNetworkAccessManager::PostOperation, callbackParams,
QJsonDocument(payload).toJson());
});
}
}
@ -276,60 +316,6 @@ bool LoginDialog::getLoginDialogPoppedUp() const {
return qApp->getLoginDialogPoppedUp();
}
void LoginDialog::onLoginThroughOculusReady(QString nonce, QString userID) {
DependencyManager::get<AccountManager>()->requestAccessTokenWithOculus(nonce, userID);
}
void LoginDialog::onLinkOculusReady(QString nonce, QString userID) {
if (nonce.isEmpty() || userID.isEmpty()) {
emit handleLoginFailed();
return;
}
JSONCallbackParameters callbackParams;
callbackParams.callbackReceiver = this;
callbackParams.jsonCallbackMethod = "linkCompleted";
callbackParams.errorCallbackMethod = "linkFailed";
const QString LINK_OCULUS_PATH = "api/v1/user/oculus/link";
QJsonObject payload;
payload.insert("oculus_nonce", QJsonValue::fromVariant(QVariant(nonce)));
payload.insert("oculus_user_id", QJsonValue::fromVariant(QVariant(userID)));
auto accountManager = DependencyManager::get<AccountManager>();
accountManager->sendRequest(LINK_OCULUS_PATH, AccountManagerAuth::Required,
QNetworkAccessManager::PostOperation, callbackParams,
QJsonDocument(payload).toJson());
}
void LoginDialog::onCreateAccountThroughOculusReady(QString nonce, QString userID, QString username) {
if (nonce.isEmpty() || userID.isEmpty()) {
emit handleLoginFailed();
return;
}
JSONCallbackParameters callbackParams;
callbackParams.callbackReceiver = this;
callbackParams.jsonCallbackMethod = "createCompleted";
callbackParams.errorCallbackMethod = "createFailed";
const QString CREATE_ACCOUNT_FROM_OCULUS_PATH = "api/v1/user/oculus/create";
QJsonObject payload;
payload.insert("oculus_nonce", QJsonValue::fromVariant(QVariant(nonce)));
payload.insert("oculus_user_id", QJsonValue::fromVariant(QVariant(userID)));
if (!username.isEmpty()) {
payload.insert("username", QJsonValue::fromVariant(QVariant(username)));
}
auto accountManager = DependencyManager::get<AccountManager>();
accountManager->sendRequest(CREATE_ACCOUNT_FROM_OCULUS_PATH, AccountManagerAuth::None,
QNetworkAccessManager::PostOperation, callbackParams,
QJsonDocument(payload).toJson());
}
QString errorStringFromAPIObject(const QJsonValue& apiObject) {
if (apiObject.isArray()) {
return apiObject.toArray()[0].toString();

View file

@ -83,10 +83,6 @@ protected slots:
Q_INVOKABLE bool getLoginDialogPoppedUp() const;
void onLoginThroughOculusReady(QString nonce, QString userID);
void onLinkOculusReady(QString nonce, QString userID);
void onCreateAccountThroughOculusReady(QString nonce, QString userID, QString username);
private:
bool getIsLogIn() const { return _isLogIn; }
void setIsLogIn(const bool isLogIn) { _isLogIn = isLogIn; }

View file

@ -7,27 +7,20 @@
//
#pragma once
#include <QObject>
#include <QtCore/QString>
#include <functional>
enum LoginState {
INVALID_STATE = 0,
LOGIN,
LINK_ACCOUNT,
CREATE_ACCOUNT
};
using NonceUserIDCallback = std::function<void(QString, QString)>;
class OculusPlatformPlugin : public QObject {
Q_OBJECT
class OculusPlatformPlugin {
public:
OculusPlatformPlugin();
virtual ~OculusPlatformPlugin();
virtual const QString getName() const = 0;
virtual void requestNonceAndUserID(LoginState state = LoginState::INVALID_STATE) = 0;
virtual void requestNonceAndUserID(NonceUserIDCallback callback) = 0;
virtual void handleOVREvents() = 0;

View file

@ -24,8 +24,8 @@ OculusAPIPlugin::~OculusAPIPlugin() {
hifi::ovr::releaseRenderSession(_session);
}
void OculusAPIPlugin::requestNonceAndUserID(LoginState loginState) {
_loginState = loginState;
void OculusAPIPlugin::requestNonceAndUserID(NonceUserIDCallback callback) {
_nonceUserIDCallback = callback;
ovr_User_GetUserProof();
ovr_User_GetLoggedInUser();
}
@ -91,17 +91,7 @@ void OculusAPIPlugin::handleOVREvents() {
}
if (_nonceChanged) {
switch (_loginState) {
case LoginState::LOGIN:
emit loginReady(_nonce, _user);
break;
case LoginState::LINK_ACCOUNT:
emit linkAccountReady(_nonce, _user);
break;
case LoginState::CREATE_ACCOUNT:
emit createAccountReady(_nonce, _user);
break;
}
_nonceUserIDCallback(_nonce, _user);`
_loginState = LoginState::INVALID_STATE;
_nonce = _user = "";
_nonceChanged = false;

View file

@ -20,13 +20,13 @@ public:
virtual ~OculusAPIPlugin();
const QString getName() const { return NAME; }
virtual void requestNonceAndUserID(LoginState loginState);
virtual void requestNonceAndUserID(NonceUserIDCallback callback);
virtual void handleOVREvents();
private:
static const char* NAME;
LoginState _loginState{ LoginState::INVALID_STATE };
NonceUserIDCallback _nonceUserIDCallback;
QString _nonce;
bool _nonceChanged{ false };
QString _user;