#include "PhoenixServicesClient.h" #include <QDebug> #include <QByteArray> #include <QJsonDocument> #include <QJsonObject> #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> #include <phoenix.h> #include <QtCore/qjsondocument.h> namespace phoenix { typedef QSharedPointer<QNetworkRequest> QNetworkRequestPtr; typedef QSharedPointer<QByteArray> QByteArrayPtr; // It's expected that the default server setup will point a hostname to our // web services application. // // This might be lame for some development environments, that typically // don't have much control over their local DNS settings, so this can be // configured, but I don't expect it to be used in production. QString DEFAULT_PHOENIX_PREFIX_PATH = "/p4_phoenix_services/v1"; class PhoenixServicesClient::Impl : public QObject { Q_OBJECT public: Impl(PhoenixServicesClient *parent) : mClient(parent), mManager(parent), mUrl("http://example.com"), mSession(nullptr), mPhoenixPrefixPath(DEFAULT_PHOENIX_PREFIX_PATH) { } QNetworkRequestPtr createRequest() { QNetworkRequestPtr request = QNetworkRequestPtr::create(); request->setRawHeader("User-Agent", (QString("p4_phoenix_services ") + phoenix::kMajorVersion + "." + phoenix::kMinorVersion + "." + phoenix::kPatchVersion).toUtf8()); request->setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); return request; } // Configures the url on the path with the indicated final path part. // The path should not be encoded. // // Note: will not set the query parameter. void setRequestUrlWithPath(QNetworkRequestPtr request, QString path) { QUrl url(mUrl); QString fullPath(mPhoenixPrefixPath); fullPath += path; url.setPath(fullPath); qDebug() << "setting request url " << url; request->setUrl(url); } QByteArray createSessionPostBody(const QString & user, const QString & password) { QJsonObject obj; obj["user"] = user; obj["password"] = password; QJsonDocument doc(obj); return doc.toJson(QJsonDocument::JsonFormat::Compact); } void logInFinished(QNetworkReply * reply, const QString & user); public slots: void handleLogInError(QNetworkReply::NetworkError code); void handleLogInSslErrors(const QList<QSslError> & errors); // Member state public: PhoenixServicesClient *mClient; QNetworkAccessManager mManager; QUrl mUrl; SessionPtr mSession; QString mPhoenixPrefixPath; }; void PhoenixServicesClient::Impl::logInFinished(QNetworkReply *reply, const QString & user) { if (reply->isFinished() && reply->error() == QNetworkReply::NetworkError::NoError) { auto data = reply->readAll(); RequestErrorPtr error = parseRequestError(data); SessionPtr session; if (!error) { // We should have a session response QString token(data); session.reset(new Session); session->setUrl(mUrl); session->setUser(user); session->setToken(token); // TODO session->setP4Ticket(); } emit mClient->logInDone(error, session); } } void PhoenixServicesClient::Impl::handleLogInError( QNetworkReply::NetworkError code) { RequestErrorPtr error(new RequestError()); // TODO covert the network error to something nice QString msg = QString("Network Error %1").arg(code); error->setMessageText(msg); error->setBaseMessageText(msg); error->setCode(RequestError::NETWORK_ERROR); error->setSeverity(RequestError::ERROR); emit mClient->logInDone(error, SessionPtr()); }; void PhoenixServicesClient::Impl::handleLogInSslErrors( const QList<QSslError> & errors) { RequestErrorPtr error(new RequestError()); // TODO convert the errors array to messages error->setMessageText("SSL Error"); error->setBaseMessageText("SSL Error"); error->setCode(RequestError::SSL_ERROR); error->setSeverity(RequestError::ERROR); emit mClient->logInDone(error, SessionPtr()); } PhoenixServicesClient::PhoenixServicesClient() : mImpl(new Impl(this)) { } PhoenixServicesClient::~PhoenixServicesClient() { delete mImpl; } void PhoenixServicesClient::setUrl(const QUrl & url) { mImpl->mUrl = url; } const QUrl & PhoenixServicesClient::url() const { return mImpl->mUrl; } bool PhoenixServicesClient::hasSession() const { return mImpl->mSession.isNull() == false; } const Session & PhoenixServicesClient::session() const { return *(mImpl->mSession); } void PhoenixServicesClient::setSession(const Session & session) { mImpl->mSession.reset(new Session(session)); } const QString & PhoenixServicesClient::phoenixPrefixPath() const { return mImpl->mPhoenixPrefixPath; } void PhoenixServicesClient::setPhoenixPrefixPath(const QString & path) { mImpl->mPhoenixPrefixPath = path; } void PhoenixServicesClient::logIn(const QString & user, const QString & password) { QNetworkRequestPtr request = mImpl->createRequest(); // TODO right now this is text plain only, we will likely need more. request->setRawHeader("Accept", "text/plain"); // set the right url... mImpl->setRequestUrlWithPath(request, "/sessions"); QByteArray data = mImpl->createSessionPostBody(user, password); QNetworkReply *reply = mImpl->mManager.post(*request, data); connect(reply, &QNetworkReply::finished, [=]() { mImpl->logInFinished(reply, user); }); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), mImpl, SLOT(handleLogInError(QNetworkReply::NetworkError))); connect(reply, SIGNAL(sslErrors(const QList<QSslError> &)), mImpl, SLOT(handleLogInSslErrors(const QList<QSslError> &))); } void PhoenixServicesClient::fetchProject(const QString & id) { } void PhoenixServicesClient::createProject(const PhoenixProject & project) { } void PhoenixServicesClient::fetchProjects() { } } #include "PhoenixServicesClient.moc"
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 13443 | tjuricek |
Reorganized the Qt libraries into a separate part of the tree, to make it easier for CMake configuration. Added 'qt_build' and 'qt_test' tasks to at least execute tests and fail the build if they don't pass. Started the implementation of the project library interaction. Change: 1018651 Date: 3/4/15 11:43 AM Client: tjuricek_dhcp-141-n100_5959 User: tjuricek Status: pending Type: public Description: Reorganized the Qt libraries into a separate part of the tree, to make it easier for CMake configuration. Added 'qt_build' and 'qt_test' tasks to at least execute tests and fail the build if they don't pass. Started the implementation of the project library interaction. JobStatus: Jobs: Files: //web-services/p4ws-main/Rakefile //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/CMakeLists.txt //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/PhoenixIntegrationTests.cpp //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/README //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/phoenix.h //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/phoenix/PhoenixProject.cpp //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/phoenix/PhoenixProject.h //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/phoenix/PhoenixServicesClient.cpp //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/phoenix/PhoenixServicesClient.h //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/phoenix/RequestError.cpp //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/phoenix/RequestError.h //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/phoenix/Session.cpp //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/phoenix/Session.h //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/test/PhoenixServicesClientTests.cpp //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/test/PhoenixServicesClientTests.h //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/test/SessionTests.cpp //web-services/p4ws-main/p4_phoenix_services/clients/qt/p4_phoenix_services_client/test/SessionTests.h //web-services/p4ws-main/p4_project_services/clients/qt/p4_project_services_client/CMakeLists.txt //web-services/p4ws-main/p4_project_services/clients/qt/p4_project_services_client/p4_project_services.h //web-services/p4ws-main/p4_project_services/clients/qt/p4_project_services_client/p4_project_services/Branch.cpp //web-services/p4ws-main/p4_project_services/clients/qt/p4_project_services_client/p4_project_services/Branch.h //web-services/p4ws-main/p4_project_services/clients/qt/p4_project_services_client/p4_project_services/Project.cpp //web-services/p4ws-main/p4_project_services/clients/qt/p4_project_services_client/p4_project_services/Project.h //web-services/p4ws-main/p4_project_services/clients/qt/p4_project_services_client/p4_project_services/View.cpp //web-services/p4ws-main/p4_project_services/clients/qt/p4_project_services_client/p4_project_services/View.h //web-services/p4ws-main/qt/CMakeLists.txt //web-services/p4ws-main/qt/p4_phoenix_services_client/CMakeLists.txt //web-services/p4ws-main/qt/p4_phoenix_services_client/PhoenixIntegrationTests.cpp //web-services/p4ws-main/qt/p4_phoenix_services_client/README //web-services/p4ws-main/qt/p4_phoenix_services_client/phoenix.h //web-services/p4ws-main/qt/p4_phoenix_services_client/phoenix/PhoenixProject.cpp //web-services/p4ws-main/qt/p4_phoenix_services_client/phoenix/PhoenixProject.h //web-services/p4ws-main/qt/p4_phoenix_services_client/phoenix/PhoenixServicesClient.cpp //web-services/p4ws-main/qt/p4_phoenix_services_client/phoenix/PhoenixServicesClient.h //web-services/p4ws-main/qt/p4_phoenix_services_client/phoenix/RequestError.cpp //web-services/p4ws-main/qt/p4_phoenix_services_client/phoenix/RequestError.h //web-services/p4ws-main/qt/p4_phoenix_services_client/phoenix/Session.cpp //web-services/p4ws-main/qt/p4_phoenix_services_client/phoenix/Session.h //web-services/p4ws-main/qt/p4_phoenix_services_client/test/PhoenixServicesClientTests.cpp //web-services/p4ws-main/qt/p4_phoenix_services_client/test/PhoenixServicesClientTests.h //web-services/p4ws-main/qt/p4_phoenix_services_client/test/SessionTests.cpp //web-services/p4ws-main/qt/p4_phoenix_services_client/test/SessionTests.h //web-services/p4ws-main/qt/p4_project_services_client/CMakeLists.txt //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeCache.txt //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/2.8.12/CMakeCCompiler.cmake //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/2.8.12/CMakeCXXCompiler.cmake //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/2.8.12/CMakeDetermineCompilerABI_C.bin //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/2.8.12/CMakeDetermineCompilerABI_CXX.bin //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/2.8.12/CMakeSystem.cmake //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/2.8.12/CompilerIdC/CMakeCCompilerId.c //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/2.8.12/CompilerIdC/a.out //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/2.8.12/CompilerIdCXX/CMakeCXXCompilerId.cpp //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/2.8.12/CompilerIdCXX/a.out //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/CMakeDirectoryInformation.cmake //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/CMakeOutput.log //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/Makefile.cmake //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/Makefile2 //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/TargetDirectories.txt //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/cmake.check_cache //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/p4_project_services_client.dir/DependInfo.cmake //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/p4_project_services_client.dir/build.make //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/p4_project_services_client.dir/cmake_clean.cmake //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/p4_project_services_client.dir/cmake_clean_target.cmake //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/p4_project_services_client.dir/depend.make //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/p4_project_services_client.dir/flags.make //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/p4_project_services_client.dir/link.txt //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/p4_project_services_client.dir/progress.make //web-services/p4ws-main/qt/p4_project_services_client/build/CMakeFiles/progress.marks //web-services/p4ws-main/qt/p4_project_services_client/build/Makefile //web-services/p4ws-main/qt/p4_project_services_client/build/cmake_install.cmake //web-services/p4ws-main/qt/p4_project_services_client/p4_project_services.h //web-services/p4ws-main/qt/p4_project_services_client/p4_project_services/Branch.cpp //web-services/p4ws-main/qt/p4_project_services_client/p4_project_services/Branch.h //web-services/p4ws-main/qt/p4_project_services_client/p4_project_services/Project.cpp //web-services/p4ws-main/qt/p4_project_services_client/p4_project_services/Project.h //web-services/p4ws-main/qt/p4_project_services_client/p4_project_services/View.cpp //web-services/p4ws-main/qt/p4_project_services_client/p4_project_services/View.h |
||
#2 | 13441 | tjuricek |
As an interim step, allow the application/json Accept type to return the p4d ticket for the server. In the future, there will likely be an adjustment to the POST request to store a separate host we attempt to generate a ticket for. Right now, these are host unlocked tickets, and not the production solution. |
||
#1 | 13439 | tjuricek |
Added a *very trivial* logIn implementation. This just makes the *current* POST request to /p4_phoenix_services/v1/sessions, which *does not* include the ability to create a host locked P4 ticket for the user. So it's not quite usable yet. But, I've made several tweaks to the API which should be stablizing. |